-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
193 lines (171 loc) · 3.07 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
package jobq
import (
"context"
"errors"
"fmt"
"sync"
"time"
)
var (
ErrWorkCanceled = errors.New("work has been canceled")
)
type Worker interface {
ID() int
IsWorking() bool
Start()
Stop()
Resume()
Pause()
}
type worker struct {
id int
store Store
jobName string
job Job
working bool
awaiting bool
runch chan bool
okch chan bool
stopch chan bool
opts JobOptions
sync.RWMutex
}
type WorkerFactory interface {
WithJob(name string, job Job) WorkerFactory
WithStore(store Store) WorkerFactory
WithOptions(opts JobOptions) WorkerFactory
Make() Worker
}
type workerFactory struct {
n int
jobName string
job Job
store Store
opts JobOptions
}
func (f *workerFactory) WithJob(name string, job Job) WorkerFactory {
f.jobName = name
f.job = job
return f
}
func (f *workerFactory) WithStore(store Store) WorkerFactory {
f.store = store
return f
}
func (f *workerFactory) WithOptions(opts JobOptions) WorkerFactory {
f.opts = opts
return f
}
func NewWorkerFactory() WorkerFactory {
return &workerFactory{}
}
func (f *workerFactory) Make() Worker {
f.n++
return &worker{
id: f.n,
jobName: f.jobName,
job: f.job,
store: f.store,
opts: f.opts,
working: false,
runch: make(chan bool),
okch: make(chan bool),
stopch: make(chan bool),
}
}
func (w *worker) ID() int {
return w.id
}
func (w *worker) IsWorking() bool {
w.RLock()
defer w.RUnlock()
return w.working
}
func (w *worker) Start() {
for !w.isStopping() {
err := w.work()
w.handleWorkErr(err)
}
}
func (w *worker) handleWorkErr(err error) {
switch err {
case nil:
return
case ErrEmptyQueue:
w.Pause()
time.Sleep(time.Millisecond * 100)
return
case ErrWorkCanceled:
time.Sleep(time.Second)
return
default:
fmt.Printf("unhandled err: %v\n", err)
time.Sleep(time.Second)
return
}
}
func (w *worker) Stop() {
w.Pause()
w.stopch <- true
<-w.stopch
}
func (w *worker) Resume() {
w.runch <- true
<-w.okch
w.Lock()
w.working = true
w.Unlock()
}
func (w *worker) Pause() {
w.Lock()
defer w.Unlock()
w.working = false
}
func (w *worker) isStopping() bool {
for !w.IsWorking() {
select {
case <-w.runch:
w.okch <- true
return false
case <-w.stopch:
w.stopch <- true
w.stopch = nil
return true
}
}
return false
}
func (w *worker) work() error {
act, err := w.store.Dequeue(w.jobName)
if err != nil {
return err
}
defer act.Rollback()
row := act.Row()
ctx, cancel := context.WithTimeout(context.Background(), w.opts.ttl)
defer cancel()
task := &Task{row, true, w.id}
err = w.job.HandleTask(ctx, task)
if err != nil && w.opts.requeuing {
prepareTaskForRequeue(task, w.opts)
err = act.Requeue(row)
if err != nil {
return err
}
}
if err = ctx.Err(); err != nil {
return ErrWorkCanceled
}
return act.Commit()
}
func prepareTaskForRequeue(task *Task, opts JobOptions) {
if task.row.retries > 0 {
task.row.retries--
} else {
task.row.retries = opts.retries
task.row.timeout = nullTime{
Valid: opts.timeoutEnabled,
Time: time.Now().Add(opts.timeout).UTC(),
}
}
}