-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathworker.go
57 lines (46 loc) · 1.18 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
package wasps
import (
"context"
)
// Worker is an interface representing a wasps working agent.
// It will be used to process a job of own job channel, and clean up its resources when being removed from the pool.
type Worker interface {
// Do will process a job.
Do(job *Job)
// return job channel of worker
JobChan() chan *Job
// return stop channel of worker
StopChan() chan struct{}
}
// Job is the struct that represents the smallest unit of worker tasks
type Job struct {
Ctx context.Context
PayLoad payLoad
Args []interface{}
RecoverFn func(r interface{})
}
// callbackWorker is a minimal Worker implementation that attempts to cast each job into func(...interface{}).
type callbackWorker struct {
job chan *Job
close chan struct{}
}
type DefaultWorkerPayLoad func(ctx context.Context, args ...interface{})
func (c *callbackWorker) Do(job *Job) {
select {
case <-job.Ctx.Done():
return
default:
}
f, ok := job.PayLoad.(DefaultWorkerPayLoad)
if ok {
f(job.Ctx, job.Args...)
} else {
panic("invalid input callback")
}
}
func (c *callbackWorker) JobChan() chan *Job {
return c.job
}
func (c *callbackWorker) StopChan() chan struct{} {
return c.close
}