-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathjob.go
101 lines (81 loc) · 2.07 KB
/
job.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package litejob
import (
"encoding/json"
"time"
"strings"
)
//任务状态
type JobStatus uint
const (
JobStatusWating JobStatus = 1 //任务已经添加成功,等待执行
JobStatusDoing JobStatus = 2 //任务正在执行
JobStatusFailed JobStatus = 3 //任务执行失败
JobStatusSuccess JobStatus = 4 //任务执行成功
JobStatusAgain JobStatus = 5 //任务重新执行,受限于max_reply
JobStatusKill JobStatus = 100
JobStatusUnknow JobStatus = 101
)
func (this JobStatus) String() string {
switch this {
case JobStatusWating:
return "waiting"
case JobStatusDoing:
return "doing"
case JobStatusFailed:
return "failed"
case JobStatusSuccess:
return "success"
case JobStatusAgain:
return "again"
case JobStatusKill:
return "kill"
case JobStatusUnknow:
return "unknow"
}
return "unknow"
}
//任务函数返回信息
type JobReturn struct {
Status JobStatus //任务状态
Msg string //任务执行结果
}
type JobState struct {
JobId string
Name string
Status string
Msg string
RunTime int
WaitTime int
ReplyCount int
}
func (this *JobState) MarshalBinary() ([]byte, error) {
return json.Marshal(this)
}
func (this *JobState) UnmarshalBinary(data []byte) error {
d := json.NewDecoder(strings.NewReader(string(data)))
d.UseNumber()
return d.Decode(this)
}
//任务函数定义
type JobHandler func(job *Job) JobReturn
//任务回调函数定义
type JobCallback func(job *Job)
//任务
//todo 优先队列的支持
type Job struct {
Id string //任务id,使用/dev/urandom 获取
Name string //任务名称,调度器注册的任务名称
Param interface{} //任务参数
CreateTime time.Time //任务创建时间
FlushTime time.Time //任务结束时间
Status JobStatus //任务状态
ReplyCount uint32 //重试次数
}
func (this *Job) MarshalBinary() ([]byte, error) {
return json.Marshal(this)
}
func (this *Job) UnmarshalBinary(data []byte) error {
d := json.NewDecoder(strings.NewReader(string(data)))
d.UseNumber()
return d.Decode(this)
}