forked from RichardKnop/machinery
-
Notifications
You must be signed in to change notification settings - Fork 2
/
worker.go
272 lines (226 loc) · 7.59 KB
/
worker.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package machinery
import (
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/GetStream/machinery/backends"
"github.com/GetStream/machinery/log"
"github.com/GetStream/machinery/retry"
"github.com/GetStream/machinery/tasks"
)
// Worker represents a single worker process
type Worker struct {
server *Server
ConsumerTag string
Concurrency int
}
// Launch starts a new worker process. The worker subscribes
// to the default queue and processes incoming registered tasks
func (worker *Worker) Launch() error {
cnf := worker.server.GetConfig()
broker := worker.server.GetBroker()
log.INFO.Printf("Launching a worker with the following settings:")
log.INFO.Printf("- Broker: %s", cnf.Broker)
log.INFO.Printf("- DefaultQueue: %s", cnf.DefaultQueue)
log.INFO.Printf("- ResultBackend: %s", cnf.ResultBackend)
if cnf.AMQP != nil {
log.INFO.Printf("- AMQP: %s", cnf.AMQP.Exchange)
log.INFO.Printf(" - Exchange: %s", cnf.AMQP.Exchange)
log.INFO.Printf(" - ExchangeType: %s", cnf.AMQP.ExchangeType)
log.INFO.Printf(" - BindingKey: %s", cnf.AMQP.BindingKey)
log.INFO.Printf(" - PrefetchCount: %d", cnf.AMQP.PrefetchCount)
}
errorsChan := make(chan error)
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
go func() {
for {
shouldRetry, err := broker.StartConsuming(worker.ConsumerTag, worker.Concurrency, worker)
if shouldRetry {
log.WARNING.Printf("Start consuming error: %s", err)
} else {
errorsChan <- err // stop the goroutine
return
}
}
}()
go func() {
err := fmt.Errorf("Signal received: %v. Quitting the worker", <-sig)
log.INFO.Print(err.Error())
worker.Quit()
errorsChan <- err
}()
return <-errorsChan
}
// Quit tears down the running worker process
func (worker *Worker) Quit() {
worker.server.GetBroker().StopConsuming()
}
// Process handles received tasks and triggers success/error callbacks
func (worker *Worker) Process(signature *tasks.Signature) error {
// If the task is not registered with this worker, do not continue
// but only return nil as we do not want to restart the worker process
if !worker.server.IsTaskRegistered(signature.Name) {
return nil
}
taskFunc, err := worker.server.GetRegisteredTask(signature.Name)
if err != nil {
return nil
}
// Update task state to RECEIVED
if err = worker.server.GetBackend().SetStateReceived(signature); err != nil {
return fmt.Errorf("Set state received error: %s", err)
}
// Prepare task for processing
task, err := tasks.New(taskFunc, signature.Args)
// if this failed, it means the task is malformed, probably has invalid
// signature, go directly to task failed without checking whether to retry
if err != nil {
worker.taskFailed(signature, err)
return err
}
// Update task state to STARTED
if err = worker.server.GetBackend().SetStateStarted(signature); err != nil {
return fmt.Errorf("Set state started error: %s", err)
}
// Call the task
results, err := task.Call()
if err != nil {
// Let's retry the task
if signature.RetryCount > 0 {
return worker.taskRetry(signature)
}
return worker.taskFailed(signature, err)
}
return worker.taskSucceeded(signature, results)
}
// retryTask decrements RetryCount counter and republishes the task to the queue
func (worker *Worker) taskRetry(signature *tasks.Signature) error {
// Update task state to RETRY
if err := worker.server.GetBackend().SetStateRetry(signature); err != nil {
return fmt.Errorf("Set state retry error: %s", err)
}
// Decrement the retry counter, when it reaches 0, we won't retry again
signature.RetryCount--
// Increase retry timeout
signature.RetryTimeout = retry.FibonacciNext(signature.RetryTimeout)
// Delay task by signature.RetryTimeout seconds
eta := time.Now().UTC().Add(time.Second * time.Duration(signature.RetryTimeout))
signature.ETA = &eta
log.WARNING.Printf("Task %s failed. Going to retry in %ds.", signature.UUID, signature.RetryTimeout)
// Send the task back to the queue
_, err := worker.server.SendTask(signature)
return err
}
// taskSucceeded updates the task state and triggers success callbacks or a
// chord callback if this was the last task of a group with a chord callback
func (worker *Worker) taskSucceeded(signature *tasks.Signature, taskResults []*tasks.TaskResult) error {
// Update task state to SUCCESS
if err := worker.server.GetBackend().SetStateSuccess(signature, taskResults); err != nil {
return fmt.Errorf("Set state success error: %s", err)
}
debugResults := make([]string, len(taskResults))
for i, taskResult := range taskResults {
debugResults[i] = fmt.Sprintf("%v", taskResult.Value)
}
log.INFO.Printf("Processed task %s. Results = [%v]", signature.UUID, strings.Join(debugResults, ", "))
// Trigger success callbacks
for _, successTask := range signature.OnSuccess {
if !signature.Immutable {
// Pass results of the task to success callbacks
for _, taskResult := range taskResults {
successTask.Args = append([]tasks.Arg{{
Type: taskResult.Type,
Value: taskResult.Value,
}}, successTask.Args...)
}
}
worker.server.SendTask(successTask)
}
// If the task was not part of a group, just return
if signature.GroupUUID == "" {
return nil
}
// Check if all task in the group has completed
groupCompleted, err := worker.server.GetBackend().GroupCompleted(
signature.GroupUUID,
signature.GroupTaskCount,
)
if err != nil {
return fmt.Errorf("Group completed error: %s", err)
}
// If the group has not yet completed, just return
if !groupCompleted {
return nil
}
// Defer purging of group meta queue if we are using AMQP backend
if worker.hasAMQPBackend() {
defer worker.server.GetBackend().PurgeGroupMeta(signature.GroupUUID)
}
// There is no chord callback, just return
if signature.ChordCallback == nil {
return nil
}
// Trigger chord callback
shouldTrigger, err := worker.server.GetBackend().TriggerChord(signature.GroupUUID)
if err != nil {
return fmt.Errorf("Trigger chord error: %s", err)
}
// Chord has already been triggered
if !shouldTrigger {
return nil
}
// Get task states
taskStates, err := worker.server.GetBackend().GroupTaskStates(
signature.GroupUUID,
signature.GroupTaskCount,
)
if err != nil {
return nil
}
// Append group tasks' return values to chord task if it's not immutable
for _, taskState := range taskStates {
if !taskState.IsSuccess() {
return nil
}
if !signature.ChordCallback.Immutable {
// Pass results of the task to the chord callback
for _, taskResult := range taskState.Results {
signature.ChordCallback.Args = append(signature.ChordCallback.Args, tasks.Arg{
Type: taskResult.Type,
Value: taskResult.Value,
})
}
}
}
// Send the chord task
_, err = worker.server.SendTask(signature.ChordCallback)
return err
}
// taskFailed updates the task state and triggers error callbacks
func (worker *Worker) taskFailed(signature *tasks.Signature, taskErr error) error {
// Update task state to FAILURE
if err := worker.server.GetBackend().SetStateFailure(signature, taskErr.Error()); err != nil {
return fmt.Errorf("Set state failure error: %s", err)
}
log.ERROR.Printf("Failed processing %s. Error = %v", signature.UUID, taskErr)
// Trigger error callbacks
for _, errorTask := range signature.OnError {
// Pass error as a first argument to error callbacks
args := append([]tasks.Arg{{
Type: "string",
Value: taskErr.Error(),
}}, errorTask.Args...)
errorTask.Args = args
worker.server.SendTask(errorTask)
}
return nil
}
// Returns true if the worker uses AMQP backend
func (worker *Worker) hasAMQPBackend() bool {
_, ok := worker.server.GetBackend().(*backends.AMQPBackend)
return ok
}