-
Notifications
You must be signed in to change notification settings - Fork 20
/
worker.go
46 lines (39 loc) · 878 Bytes
/
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
package dashing
// A Job does periodic work and sends events to a channel.
type Job interface {
Work(send chan *Event)
}
// A Worker contains a collection of jobs.
type Worker struct {
broker *Broker
registry []Job
}
// Register a job for a particular worker.
func (w *Worker) Register(j Job) {
if j == nil {
panic("Can't register nil job")
}
w.registry = append(w.registry, j)
}
// Start all jobs.
func (w *Worker) Start() {
for _, j := range w.registry {
go j.Work(w.broker.events)
}
}
// NewWorker returns a Worker instance.
func NewWorker(b *Broker) *Worker {
return &Worker{
broker: b,
registry: append([]Job(nil), jobs...),
}
}
// Global registry for background jobs.
var jobs []Job
// Register a job to be kicked off upon starting a worker.
func Register(j Job) {
if j == nil {
panic("Can't register nil job")
}
jobs = append(jobs, j)
}