This repository has been archived by the owner on Nov 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
worker.go
230 lines (201 loc) · 5.58 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
package que
import (
"bytes"
"fmt"
"log"
"os"
"runtime"
"strconv"
"sync"
"time"
)
// WorkFunc is a function that performs a Job. If an error is returned, the job
// is reenqueued with exponential backoff.
type WorkFunc func(j *Job) error
// WorkMap is a map of Job names to WorkFuncs that are used to perform Jobs of a
// given type.
type WorkMap map[string]WorkFunc
// Worker is a single worker that pulls jobs off the specified Queue. If no Job
// is found, the Worker will sleep for Interval seconds.
type Worker struct {
// Interval is the amount of time that this Worker should sleep before trying
// to find another Job.
Interval time.Duration
// Queue is the name of the queue to pull Jobs off of. The default value, "",
// is usable and is the default for both que-go and the ruby que library.
Queue string
c *Client
m WorkMap
mu sync.Mutex
done bool
ch chan struct{}
}
var defaultWakeInterval = 5 * time.Second
func init() {
if v := os.Getenv("QUE_WAKE_INTERVAL"); v != "" {
if newInt, err := strconv.Atoi(v); err == nil {
defaultWakeInterval = time.Duration(newInt) * time.Second
}
}
}
// NewWorker returns a Worker that fetches Jobs from the Client and executes
// them using WorkMap. If the type of Job is not registered in the WorkMap, it's
// considered an error and the job is re-enqueued with a backoff.
//
// Workers default to an Interval of 5 seconds, which can be overridden by
// setting the environment variable QUE_WAKE_INTERVAL. The default Queue is the
// nameless queue "", which can be overridden by setting QUE_QUEUE. Either of
// these settings can be changed on the returned Worker before it is started
// with Work().
func NewWorker(c *Client, m WorkMap) *Worker {
return &Worker{
Interval: defaultWakeInterval,
Queue: os.Getenv("QUE_QUEUE"),
c: c,
m: m,
ch: make(chan struct{}),
}
}
// Work pulls jobs off the Worker's Queue at its Interval. This function only
// returns after Shutdown() is called, so it should be run in its own goroutine.
func (w *Worker) Work() {
defer log.Println("worker done")
for {
// Try to work a job
if w.WorkOne() {
// Since we just did work, non-blocking check whether we should exit
select {
case <-w.ch:
return
default:
// continue in loop
}
} else {
// No work found, block until exit or timer expires
select {
case <-w.ch:
return
case <-time.After(w.Interval):
// continue in loop
}
}
}
}
func (w *Worker) WorkOne() (didWork bool) {
j, err := w.c.LockJob(w.Queue)
if err != nil {
log.Printf("attempting to lock job: %v", err)
return
}
if j == nil {
return // no job was available
}
defer j.Done()
defer recoverPanic(j)
didWork = true
wf, ok := w.m[j.Type]
if !ok {
msg := fmt.Sprintf("unknown job type: %q", j.Type)
log.Println(msg)
if err = j.Error(msg); err != nil {
log.Printf("attempting to save error on job %d: %v", j.ID, err)
}
return
}
if err = wf(j); err != nil {
j.Error(err.Error())
return
}
if err = j.Delete(); err != nil {
log.Printf("attempting to delete job %d: %v", j.ID, err)
}
log.Printf("event=job_worked job_id=%d job_type=%s", j.ID, j.Type)
return
}
// Shutdown tells the worker to finish processing its current job and then stop.
// There is currently no timeout for in-progress jobs. This function blocks
// until the Worker has stopped working. It should only be called on an active
// Worker.
func (w *Worker) Shutdown() {
w.mu.Lock()
defer w.mu.Unlock()
if w.done {
return
}
log.Println("worker shutting down gracefully...")
w.ch <- struct{}{}
w.done = true
close(w.ch)
}
// recoverPanic tries to handle panics in job execution.
// A stacktrace is stored into Job last_error.
func recoverPanic(j *Job) {
if r := recover(); r != nil {
// record an error on the job with panic message and stacktrace
stackBuf := make([]byte, 1024)
n := runtime.Stack(stackBuf, false)
buf := &bytes.Buffer{}
fmt.Fprintf(buf, "%v\n", r)
fmt.Fprintln(buf, string(stackBuf[:n]))
fmt.Fprintln(buf, "[...]")
stacktrace := buf.String()
log.Printf("event=panic job_id=%d job_type=%s\n%s", j.ID, j.Type, stacktrace)
if err := j.Error(stacktrace); err != nil {
log.Printf("attempting to save error on job %d: %v", j.ID, err)
}
}
}
// WorkerPool is a pool of Workers, each working jobs from the queue Queue
// at the specified Interval using the WorkMap.
type WorkerPool struct {
WorkMap WorkMap
Interval time.Duration
Queue string
c *Client
workers []*Worker
mu sync.Mutex
done bool
}
// NewWorkerPool creates a new WorkerPool with count workers using the Client c.
func NewWorkerPool(c *Client, wm WorkMap, count int) *WorkerPool {
return &WorkerPool{
c: c,
WorkMap: wm,
Interval: defaultWakeInterval,
workers: make([]*Worker, count),
}
}
// Start starts all of the Workers in the WorkerPool.
func (w *WorkerPool) Start() {
w.mu.Lock()
defer w.mu.Unlock()
for i := range w.workers {
w.workers[i] = NewWorker(w.c, w.WorkMap)
w.workers[i].Interval = w.Interval
w.workers[i].Queue = w.Queue
go w.workers[i].Work()
}
}
// Shutdown sends a Shutdown signal to each of the Workers in the WorkerPool and
// waits for them all to finish shutting down.
func (w *WorkerPool) Shutdown() {
w.mu.Lock()
defer w.mu.Unlock()
if w.done {
return
}
var wg sync.WaitGroup
wg.Add(len(w.workers))
for _, worker := range w.workers {
go func(worker *Worker) {
// If Shutdown is called before Start has been called,
// then these are nil, so don't try to close them
if worker != nil {
worker.Shutdown()
}
wg.Done()
}(worker)
}
wg.Wait()
w.done = true
}