-
Notifications
You must be signed in to change notification settings - Fork 0
/
boxer.go
314 lines (259 loc) · 6 KB
/
boxer.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Package boxer
// provides a local job processing functionality.
//
// General workflow:
//
// 1. Create a new boxer: boxer.New()
//
// 2. Register a runner: b.Register("MyCalcsRunner", MyCalcsRunner)
//
// 3. Run the boxer: b.Run()
//
// 4. Create a new job: job.New("MyCalcsRunner", ...)
//
// 5. Push the job to the boxer: b.Push(j)
package boxer
import (
"context"
"errors"
"fmt"
mathrand "math/rand"
"os"
"os/signal"
"runtime"
"sync"
"syscall"
"time"
"github.com/thesoulless/boxer/v2/job"
"github.com/thesoulless/boxer/v2/metrics"
"sync/atomic"
)
// internal keys for context value storage
type valueKey int
const (
jobKey valueKey = iota
)
// Each job processor should be implemented as a Runner type function
type Runner func(ctx context.Context, args ...interface{}) error
type NoRunnerError struct {
JobType string
}
func (s *NoRunnerError) Error() string {
return fmt.Sprintf("No runner is registered for job type %s", s.JobType)
}
type handler func(ctx context.Context, job *job.Job) error
type Boxer interface {
Register(string, Runner)
Run()
Push(*job.Job) error
Count() int
OnFlyCounts() map[string]int32
Error() <-chan *job.Error
Terminate(bool)
// Done can be useful for later implementing triggers
// Done()
}
//type jobPayload map[string]chan []byte
type jobPayload map[string]chan *job.Job
type boxer struct {
Concurrency int
queues []string
done chan interface{}
shutdownWaiter *sync.WaitGroup
jobHandlers map[string]handler
jobs jobPayload
count int32
onFlyCounts map[string]*int32
jobErrors chan *job.Error
}
func New(withMetrics bool, namespace, subsystem string, queues ...string) (Boxer, error) {
if len(queues) < 1 {
queues = []string{"default"}
}
chs := make(map[string]chan *job.Job)
onFlyCounts := make(map[string]*int32, len(queues))
for _, queue := range queues {
chs[queue] = make(chan *job.Job)
zero := int32(0)
onFlyCounts[queue] = &zero
}
b := &boxer{
Concurrency: runtime.NumCPU(),
queues: queues,
done: make(chan interface{}),
shutdownWaiter: &sync.WaitGroup{},
jobHandlers: map[string]handler{},
jobs: chs,
count: 0,
jobErrors: make(chan *job.Error),
onFlyCounts: onFlyCounts,
}
if withMetrics {
err := metrics.New(b, namespace, subsystem)
if err != nil {
return nil, err
}
}
return b, nil
}
func (b *boxer) Register(name string, fn Runner) {
b.jobHandlers[name] = func(ctx context.Context, job *job.Job) error {
return fn(ctx, job.Args...)
}
}
func (b *boxer) Count() int {
v := atomic.LoadInt32(&b.count)
return int(v)
}
func (b *boxer) OnFlyCounts() map[string]int32 {
onFlyVal := make(map[string]int32, len(b.onFlyCounts))
for q, v := range b.onFlyCounts {
_v := atomic.LoadInt32(v)
onFlyVal[q] = _v
}
return onFlyVal
}
func (b *boxer) Run() {
b.shutdownWaiter.Add(b.Concurrency)
for i := 0; i < b.Concurrency; i++ {
go process(b)
}
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
killSignal := <-c
switch killSignal {
case os.Interrupt, syscall.SIGTERM:
b.Terminate(true)
}
}()
}
func (b *boxer) Error() <-chan *job.Error {
return b.jobErrors
}
func (b *boxer) Terminate(die bool) {
b.done <- true
b.shutdownWaiter.Wait()
if die {
os.Exit(0)
}
}
func process(b *boxer) {
// pause between 0 and 0.5B nanoseconds (0 - 0.5 seconds)
time.Sleep(time.Duration(mathrand.Int31() / 4)) //nolint:gosec
for {
// check for shutdown
select {
case <-b.done:
for i := 0; i < b.Concurrency; i++ {
b.shutdownWaiter.Done()
}
return
default:
}
err := processOne(b)
if err != nil {
var _e0 *NoRunnerError
if ok := errors.Is(err, _e0); !ok {
time.Sleep(1 * time.Second)
}
}
}
}
func processOne(b *boxer) error {
var (
j *job.Job
err error
)
j, err = b.Fetch(b.queues...)
if err != nil {
return err
}
if j == nil {
return nil
}
runner := b.jobHandlers[j.Type]
if runner == nil {
return &NoRunnerError{JobType: j.Type}
}
// increase onFlyCount when we're going to dispatch the job
_, ok := b.onFlyCounts[j.Queue]
if ok {
atomic.AddInt32(b.onFlyCounts[j.Queue], 1)
}
if j.Delay > 0 {
time.Sleep(j.Delay)
}
joberr := dispatch(jobContext(j), j, runner)
// decrease the onFlyCount
if _, ok = b.onFlyCounts[j.Queue]; ok {
atomic.AddInt32(b.onFlyCounts[j.Queue], -1)
}
if joberr != nil {
// log.Printf("error running %s job %s: %v", j.Type, j.ID, joberr)
var backtrace []string
if j.Backtrace > 0 {
backtrace = job.Backtrace(j.Backtrace)
}
hostname, _ := os.Hostname()
b.jobErrors <- &job.Error{
HostName: hostname,
RetryCount: j.Retried(),
FailedAt: time.Now().UTC(),
Backtrace: backtrace,
Err: joberr,
}
res := b.fail(j)
time.Sleep(1 * time.Second)
return res
}
// decrease only when a job is done
atomic.AddInt32(&b.count, -1)
return nil
}
// fail is called when a job dispatch result has error.
func (b *boxer) fail(job *job.Job) error {
if !job.CanRetry() {
return nil
}
job.Failed()
//jobBytes, err := json.Marshal(job)
//if err != nil {
// return err
//}
//var buf bytes.Buffer
//enc := gob.NewEncoder(&buf)
//err := enc.Encode(job)
//if err != nil {
// return err
//}
// pushes the job back to queue
//b.jobs[job.Queue] <- buf.Bytes()
b.jobs[job.Queue] <- job
return nil
}
func jobContext(job *job.Job) context.Context {
ctx := context.Background()
ctx = context.WithValue(ctx, jobKey, job.ID)
return ctx
}
func dispatch(ctx context.Context, job *job.Job, runner handler) error {
return runner(ctx, job)
}
func (b *boxer) Push(job *job.Job) error {
b.jobs[job.Queue] <- job
atomic.AddInt32(&b.count, 1)
return nil
}
func (b *boxer) Fetch(q ...string) (*job.Job, error) {
if len(q) == 0 {
return nil, fmt.Errorf("fetch must be called with one or more queue names")
}
// @TODO use weighted round-robin to select the queue
ch, ok := b.jobs[q[0]]
if !ok {
return nil, fmt.Errorf("channel unreachable %s", q[0])
}
data := <-ch
return data, nil
}