forked from zimulala/goqless
-
Notifications
You must be signed in to change notification settings - Fork 2
/
job.go
207 lines (176 loc) · 5.1 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package goqless
import (
"github.com/garyburd/redigo/redis"
"reflect"
"strings"
"time"
)
var (
JOBSTATES = []string{"stalled", "running", "scheduled", "depends", "recurring"}
finishBytes = []byte(`{"finish":"yes"}`)
)
type History struct {
When int64
Q string
What string
Worker string
}
type Job struct {
Jid string
Klass string
State string
Queue string
Worker string
Tracked bool
Priority int
Expires int64
Retries int
Remaining int
Data interface{}
Tags StringSlice
History []History
Failure interface{}
Dependents StringSlice
Dependencies interface{}
cli *Client
}
func NewJob(cli *Client) *Job {
return &Job{
Expires: time.Now().Add(time.Hour * 60).UTC().Unix(), // hour from now
Dependents: nil, // []interface{}{},
Tracked: false,
Tags: nil,
Jid: generateJID(),
Retries: 5,
Data: nil,
Queue: "mock_queue",
State: "running",
Remaining: 5,
Failure: nil,
History: nil, // []interface{}{},
Dependencies: nil,
Klass: "Job",
Priority: 0,
Worker: "mock_worker",
cli: cli,
}
}
func (j *Job) Client() *Client {
return j.cli
}
func (j *Job) SetClient(cli *Client) {
j.cli = cli
}
// Move this from it's current queue into another
func (j *Job) Move(queueName string) (string, error) {
return redis.String(j.cli.Do("qless", 0, "put", timestamp(), queueName, j.Jid, j.Klass, marshal(j.Data), 0))
}
// Fail this job
// return success, error
func (j *Job) Fail(typ, message string) (bool, error) {
return Bool(j.cli.Do("qless", 0, "fail", timestamp(), j.Jid, j.Worker, typ, message, marshal(j.Data)))
}
// Heartbeats this job
// return success, error
func (j *Job) Heartbeat() (bool, error) {
return Bool(j.cli.Do("qless", 0, "heartbeat", timestamp(), j.Jid, j.Worker, marshal(j.Data)))
}
// Completes this job
// returns state, error
func (j *Job) Complete() (string, error) {
return redis.String(j.cli.Do("qless", 0, "complete", timestamp(), j.Jid, j.Worker, j.Queue, marshal(j.Data)))
}
//for big job, save memory in redis
func (j *Job) CompleteWithNoData() (string, error) {
return redis.String(j.cli.Do("qless", 0, "complete", timestamp(), j.Jid, j.Worker, j.Queue, finishBytes))
}
func (j *Job) HeartbeatWithNoData() (bool, error) {
return Bool(j.cli.Do("qless", 0, "heartbeat", timestamp(), j.Jid, j.Worker))
}
// Cancels this job
func (j *Job) Cancel() {
j.cli.Do("qless", 0, "cancel", timestamp(), j.Jid)
}
// Track this job
func (j *Job) Track() (bool, error) {
return Bool(j.cli.Do("qless", 0, "track", timestamp(), "track", j.Jid))
}
// Untrack this job
func (j *Job) Untrack() (bool, error) {
return Bool(j.cli.Do("qless", 0, "track", timestamp(), "untrack", j.Jid))
}
func (j *Job) Tag(tags ...interface{}) (string, error) {
args := []interface{}{0, "tag", timestamp(), "add", j.Jid}
args = append(args, tags...)
return redis.String(j.cli.Do("qless", args...))
}
func (j *Job) Untag(tags ...interface{}) (string, error) {
args := []interface{}{0, "tag", timestamp(), "remove", j.Jid}
args = append(args, tags...)
return redis.String(j.cli.Do("qless", args...))
}
func (j *Job) Retry(delay int) (int, error) {
return redis.Int(j.cli.Do("qless", 0, "retry", timestamp(), j.Jid, j.Queue, j.Worker, delay))
}
func (j *Job) Depend(jids ...interface{}) (string, error) {
args := []interface{}{0, "depends", timestamp(), j.Jid, "on"}
args = append(args, jids...)
return redis.String(j.cli.Do("qless", args...))
}
func (j *Job) Undepend(jids ...interface{}) (string, error) {
args := []interface{}{0, "depends", timestamp(), j.Jid, "off"}
args = append(args, jids...)
return redis.String(j.cli.Do("qless", args...))
}
type RecurringJob struct {
Tags StringSlice
Jid string
Retries int
Data interface{}
Queue string
Interval int
Count int
Klass string
Priority int
cli *Client
}
func NewRecurringJob(cli *Client) *RecurringJob {
return &RecurringJob{cli: cli}
}
// example: job.Update(map[string]interface{}{"priority": 5})
// options:
// priority int
// retries int
// interval int
// data interface{}
// klass string
func (r *RecurringJob) Update(opts map[string]interface{}) {
args := []interface{}{0, "recur", timestamp(), "update", r.Jid}
vOf := reflect.ValueOf(r).Elem()
for key, value := range opts {
key = strings.ToLower(key)
v := vOf.FieldByName(ucfirst(key))
if v.IsValid() {
setv := reflect.ValueOf(value)
if key == "data" {
setv = reflect.ValueOf(marshal(value))
}
v.Set(setv)
args = append(args, key, value)
}
}
r.cli.Do("qless", args...)
}
func (r *RecurringJob) Cancel() {
r.cli.Do("qless", 0, "recur", timestamp(), "off", r.Jid)
}
func (r *RecurringJob) Tag(tags ...interface{}) {
args := []interface{}{0, "recur", timestamp(), "tag", r.Jid}
args = append(args, tags...)
r.cli.Do("qless", args...)
}
func (r *RecurringJob) Untag(tags ...interface{}) {
args := []interface{}{0, "recur", timestamp(), "untag", r.Jid}
args = append(args, tags...)
r.cli.Do("qless", args...)
}