-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.go
67 lines (58 loc) · 1.3 KB
/
common.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
package cleanjob
import (
"context"
"encoding/json"
"time"
)
//Job 脚本实体
type Job struct {
ExecuteKey string `json:"execute_key"` //执行器的key
Key string `json:"key"` //脚本主键
Data []byte `json:"data"` //脚本实体
CronExpr string `json:"cron_expr"` //触发表达式
}
//NewJob 创建脚本
func NewJob(execute, key string) *Job {
return &Job{ExecuteKey: execute, Key: key}
}
func (j *Job) WithData(data []byte) {
j.Data = data
}
func (j *Job) WithStructToData(data interface{}) {
b, _ := json.Marshal(data)
j.Data = b
}
func (j *Job) WithCronTime(t time.Time) {
j.CronExpr = t.Format("05 04 15 02 1 ? 2006")
}
func (j *Job) String() string {
return string(j.Data)
}
func (j *Job) Struct(bean interface{}) {
_ = json.Unmarshal(j.Data, bean)
return
}
type WatchFunc func(event string, job *Job)
var (
AddEvent = "add"
DeleteEvent = "delete"
KillEvent = "kill"
)
//IStore 存储
type IStore interface {
Save(job *Job) error
Delete(job *Job) error
Lock(job *ExecuteContext) error
UnLock(job *ExecuteContext) error
Load() ([]*Job, error)
Watch(WatchFunc)
}
type Execute interface {
Execute(ctx *ExecuteContext) error
}
type ExecuteContext struct {
Job *Job
CancelCtx context.Context
CancelFunc context.CancelFunc
IsLock bool
}