forked from holdno/gopherCron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.go
84 lines (68 loc) · 1.89 KB
/
interface.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package protocol
import (
"context"
"encoding/json"
"github.com/holdno/gopherCron/common"
"github.com/holdno/gopherCron/errors"
"github.com/holdno/gopherCron/pkg/etcd"
"github.com/holdno/gopherCron/utils"
clientv3 "go.etcd.io/etcd/client/v3"
)
type EtcdManager interface {
Client() *clientv3.Client
KV() clientv3.KV
Lease() clientv3.Lease
Watcher() clientv3.Watcher
GetTaskLocker(task *common.TaskInfo) *etcd.Locker
GetLocker(key string) *etcd.Locker
Inc(key string) (int64, error)
}
type comm struct {
etcd EtcdManager
}
type CommonInterface interface {
GetTask(projectID int64, taskID string) (*common.TaskInfo, error)
}
func NewComm(etcd EtcdManager) CommonInterface {
return &comm{etcd: etcd}
}
// GetTask 获取任务
func (a *comm) GetTask(projectID int64, taskID string) (*common.TaskInfo, error) {
var (
saveKey string
getResp *clientv3.GetResponse
task *common.TaskInfo
ctx context.Context
errObj errors.Error
err error
)
// build etcd save key
saveKey = common.BuildKey(projectID, taskID) // 保存的key同样也是获取的key
ctx, _ = utils.GetContextWithTimeout()
if getResp, err = a.etcd.KV().Get(ctx, saveKey); err != nil {
errObj = errors.ErrInternalError
errObj.Log = "[Etcd - GetTask] etcd client kv get one error:" + err.Error()
return nil, errObj
}
if getResp.Count > 1 {
errObj = errors.ErrInternalError
errObj.Log = "[Etcd - GetTask] etcd client kv get one task but result > 1"
return nil, errObj
} else if getResp.Count == 0 {
return nil, errors.ErrDataNotFound
}
task = &common.TaskInfo{}
if err = json.Unmarshal(getResp.Kvs[0].Value, task); err != nil {
errObj = errors.ErrInternalError
errObj.Log = "[Etcd - GetTask] task json.Unmarshal error:" + err.Error()
return nil, errObj
}
return task, nil
}
const (
version = "v2.4.6-alpha.1"
GrpcBufferSize = 1024 * 4
)
func GetVersion() string {
return version
}