gocron/modules/rpc/client/client.go

81 lines
2.1 KiB
Go
Raw Normal View History

2017-05-25 14:32:40 +00:00
package client
import (
pb "github.com/ouqiang/gocron/modules/rpc/proto"
"golang.org/x/net/context"
2017-05-26 10:09:07 +00:00
"fmt"
2017-05-26 13:59:01 +00:00
"time"
2017-05-27 05:10:57 +00:00
"errors"
2017-05-28 15:13:22 +00:00
"github.com/ouqiang/gocron/modules/rpc/grpcpool"
"google.golang.org/grpc/codes"
"google.golang.org/grpc"
2017-05-29 07:30:59 +00:00
"github.com/ouqiang/gocron/modules/logger"
2017-05-25 14:32:40 +00:00
)
var (
errUnavailable = errors.New("无法连接远程服务器")
)
2017-06-08 13:25:42 +00:00
func ExecWithRetry(ip string, port int, taskReq *pb.TaskRequest) (string, error) {
tryTimes := 60
i := 0
for i < tryTimes {
2017-06-08 13:25:42 +00:00
output, err := Exec(ip, port, taskReq)
if err != errUnavailable {
return output, err
}
i++
time.Sleep(2 * time.Second)
}
return "", errUnavailable
}
2017-06-08 13:25:42 +00:00
func Exec(ip string, port int, taskReq *pb.TaskRequest) (string, error) {
2017-05-29 07:30:59 +00:00
defer func() {
if err := recover(); err != nil {
logger.Error("panic#rpc/client.go:Exec#", err)
}
} ()
addr := fmt.Sprintf("%s:%d", ip, port)
2017-05-28 15:13:22 +00:00
conn, err := grpcpool.Pool.Get(addr)
2017-05-25 14:32:40 +00:00
if err != nil {
2017-05-27 05:10:57 +00:00
return "", err
2017-05-25 14:32:40 +00:00
}
isConnClosed := false
2017-05-28 15:13:22 +00:00
defer func() {
if !isConnClosed {
grpcpool.Pool.Put(addr, conn)
}
2017-05-28 15:13:22 +00:00
}()
2017-05-25 14:32:40 +00:00
c := pb.NewTaskClient(conn)
2017-05-26 13:59:01 +00:00
if taskReq.Timeout <= 0 || taskReq.Timeout > 86400 {
taskReq.Timeout = 86400
}
timeout := time.Duration(taskReq.Timeout) * time.Second
2017-05-27 13:42:18 +00:00
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
2017-05-26 13:59:01 +00:00
resp, err := c.Run(ctx, taskReq)
2017-05-25 14:32:40 +00:00
if err != nil {
return parseGRPCError(err, conn, &isConnClosed)
2017-05-25 14:32:40 +00:00
}
2017-05-26 10:09:07 +00:00
2017-05-27 05:10:57 +00:00
if resp.Error == "" {
return resp.Output, nil
}
return resp.Output, errors.New(resp.Error)
}
func parseGRPCError(err error, conn *grpc.ClientConn, connClosed *bool) (string, error) {
switch grpc.Code(err) {
2017-06-08 13:25:42 +00:00
case codes.Unavailable, codes.Internal:
conn.Close()
*connClosed = true
return "", errUnavailable
case codes.DeadlineExceeded:
return "", errors.New("执行超时, 强制结束")
}
return "", err
}