-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenqueue.go
192 lines (167 loc) · 4.71 KB
/
enqueue.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
package once
import (
"encoding/json"
"time"
"github.com/PlanitarInc/go-workers"
"github.com/gocql/gocql"
"github.com/gomodule/redigo/redis"
)
// Enqueue schedules the given task to the given queue, if no task of the same
// type is already scheduled to run yet. If there is a task of the same type but
// it is executing or was executed at least once (waiting for a retry, failed or
// succeeded), a new task is scheduled anyway basically overriding the existing
// one. It should not matter since the tasks are of the same type and hence
// should be identical.
func Enqueue(
queue, jobType string,
args interface{},
opts *Options,
) (string, error) {
return enqueueJobDesc(
NewJobDesc(generateJid(), queue, jobType, opts),
args,
)
}
// Enqueue schedules the given task to the given queue with the given delay, if
// no task of the same type is already scheduled to run yet. If there is a task
// of the same type but it is executing or was executed at least once (waiting
// for a retry, failed or succeeded), a new task is scheduled anyway basically
// overriding the existing one. It should not matter since the tasks are of the
// same type and hence should be identical.
func EnqueueIn(
queue, jobType string,
in time.Duration,
args interface{},
opts *Options,
) (string, error) {
if opts == nil {
opts = &Options{}
}
opts.At = workers.NowToSecondsWithNanoPrecision() + in.Seconds()
return enqueueJobDesc(
NewJobDesc(generateJid(), queue, jobType, opts),
args,
)
}
// Enqueue schedules the given task to the given queue. If a task of the same
// type is already scheduled, the given task will get scheduled anyway
// overriding the previous one.
func EnqueueForce(
queue, jobType string,
args interface{},
opts *Options,
) (string, error) {
return enqueueJobDesc(
NewJobDesc(generateJid(), queue, jobType, opts),
args,
true,
)
}
// Enqueue schedules the given task to the given queue with the given delay.
// If a task of the same type is already scheduled, the given task will get
// scheduled anyway overriding the previous one.
func EnqueueForceIn(
queue, jobType string,
in time.Duration,
args interface{},
opts *Options,
) (string, error) {
if opts == nil {
opts = &Options{}
}
opts.At = workers.NowToSecondsWithNanoPrecision() + in.Seconds()
return enqueueJobDesc(
NewJobDesc(generateJid(), queue, jobType, opts),
args,
true,
)
}
func enqueueJobDesc(desc *JobDesc, args interface{}, override ...bool) (string, error) {
conn := workers.Config.Pool.Get()
defer conn.Close()
key := workers.Config.Namespace + "once:q:" + desc.Queue + ":" + desc.JobType
msg := workers.PrepareEnqueuMsg(desc.Queue, "", args,
desc.Options.EnqueueOptions)
msg.Set("jid", desc.Jid)
msg.Set("x-once", desc)
descJson, _ := msg.Get("x-once").MarshalJSON()
if len(override) > 0 && override[0] {
err := setNewJobDesc(conn, key, desc.Options.InitWaitTime, descJson)
if err != nil {
return "", err
}
} else {
other, err := trySetNewDescJob(conn, key, desc.Options.InitWaitTime, descJson)
if err != nil {
return "", err
} else if other != nil {
return other.Jid, nil
}
}
err := workers.EnqueueMsg(msg)
if err != nil {
unsetJobDesc(conn, key, desc.Jid)
return "", err
}
return desc.Jid, nil
}
func setNewJobDesc(
conn redis.Conn,
key string,
expire int,
descJson []byte,
) error {
_, err := redis.String(conn.Do("SET", key, descJson, "EX", expire))
return err
}
func trySetNewDescJob(
conn redis.Conn,
key string,
expire int,
descJson []byte,
) (*JobDesc, error) {
// Enqueue retry loop
for {
// Check if a job of the same type is already scheduled
otherDescJson, err := redis.Bytes(conn.Do("GET", key))
if err != nil && err != redis.ErrNil {
return nil, err
}
// There is a scheduled job, inspect it
if err == nil {
otherDesc := JobDesc{}
err := json.Unmarshal(otherDescJson, &otherDesc)
if err == nil && !otherDesc.CanBeOverridden() {
return &otherDesc, err
}
// Either the job descriptor is bad, or it was already executed at
// least once -- reschedule
_, err = redis.String(conn.Do("SET", key, descJson, "EX", expire, "XX"))
if err != nil && err != redis.ErrNil {
return nil, err
}
if err == nil {
return nil, nil
}
// retry
continue
}
// No job is scheduled, try to add a new one
_, err = redis.String(conn.Do("SET", key, descJson, "EX", expire, "NX"))
if err != nil && err != redis.ErrNil {
return nil, err
}
if err == nil {
return nil, nil
}
// retry
}
}
func unsetJobDesc(conn redis.Conn, key, jid string) error {
// The script will set expires to 0 only if the JID value matches.
_, err := updateJobStatus(conn, key, jid, "", 0)
return err
}
func generateJid() string {
return gocql.TimeUUID().String()
}