-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjob-descriptor.go
127 lines (105 loc) · 2.97 KB
/
job-descriptor.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package once
import (
"time"
"github.com/PlanitarInc/go-workers"
"github.com/bitly/go-simplejson"
)
const (
StatusInitWaiting string = "init-waiting"
StatusExecuting = "executing"
StatusRetryWaiting = "retry-waiting"
StatusOK = "ok"
StatusFailed = "failed"
)
type JobDesc struct {
Jid string `json:"jid"`
Status string `json:"status"`
Queue string `json:"queue"`
JobType string `json:"job_type"`
CreatedMs int64 `json:"created_ms"`
UpdatedMs int64 `json:"updated_ms"`
Options *Options `json:"options"`
Result string `json:"result"`
}
type Options struct {
workers.EnqueueOptions
AtMostOnce bool `json:"at_most_once"`
OverrideStarted bool `json:"override_started"`
InitWaitTime int `json:"init_wait"`
RetryWaitTime int `json:"retry_wait"`
ExecWaitTime int `json:"exec_wait"`
SuccessRetention int `json:"success_retention"`
FailureRetention int `json:"failure_retention"`
}
func optionsFromJson(obj *simplejson.Json) *Options {
opts := Options{}
opts.AtMostOnce, _ = obj.Get("at_most_once").Bool()
opts.InitWaitTime, _ = obj.Get("init_wait").Int()
opts.RetryWaitTime, _ = obj.Get("retry_wait").Int()
opts.ExecWaitTime, _ = obj.Get("exec_wait").Int()
opts.SuccessRetention, _ = obj.Get("success_retention").Int()
opts.FailureRetention, _ = obj.Get("failure_retention").Int()
return optionsMergeDefaults(&opts)
}
func optionsMergeDefaults(opts *Options) *Options {
if opts == nil {
opts = &Options{}
}
if opts.InitWaitTime == 0 {
opts.InitWaitTime = 30
}
if opts.RetryWaitTime == 0 {
opts.RetryWaitTime = 60
}
if opts.ExecWaitTime == 0 {
opts.ExecWaitTime = 90
}
if opts.SuccessRetention == 0 {
opts.SuccessRetention = 5
}
if opts.FailureRetention == 0 {
opts.FailureRetention = 5
}
return opts
}
func NewJobDesc(jid, queue, jobType string, opts *Options) *JobDesc {
nowMs := time2ms(time.Now())
return &JobDesc{
Jid: jid,
Status: StatusInitWaiting,
Queue: queue,
JobType: jobType,
CreatedMs: nowMs,
UpdatedMs: nowMs,
Options: optionsMergeDefaults(opts),
}
}
func (d JobDesc) CanBeOverridden() bool {
// If OverrideStarted is set, we can override the task already started.
// Otherwise we have to wait until the task is removed from Redis.
return d.Options != nil && d.Options.OverrideStarted && d.Status != StatusInitWaiting
}
func (d JobDesc) IsInitWaiting() bool {
return d.Status == StatusInitWaiting
}
func (d JobDesc) IsDone() bool {
return d.Status == StatusOK || d.Status == StatusFailed
}
func (d JobDesc) IsFailed() bool {
return d.Status == StatusFailed
}
func (d JobDesc) IsOK() bool {
return d.Status == StatusOK
}
func (d JobDesc) CreatedAt() time.Time {
return ms2time(d.CreatedMs)
}
func (d JobDesc) UpdatedAt() time.Time {
return ms2time(d.UpdatedMs)
}
func time2ms(t time.Time) int64 {
return t.UnixNano() / 1e6
}
func ms2time(ms int64) time.Time {
return time.Unix(ms/1000, (ms%1000)*1e6)
}