/
worker.go
59 lines (53 loc) · 1.16 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
package job
import (
"fmt"
"github.com/labstack/gommon/log"
)
type (
Worker struct {
ID uint64
jobs chan *Job
dispatchStatus chan *DispatchStatus
Quit chan bool
Dispatcher *Dispatcher
}
)
func CreateNewWorker(d *Dispatcher, id uint64, workerQueue chan *Worker, jobQueue chan *Job, dStatus chan *DispatchStatus) *Worker {
w := &Worker{
Dispatcher: d,
ID: id,
jobs: jobQueue,
dispatchStatus: dStatus,
}
go func() {
workerQueue <- w
}()
return w
}
func (w *Worker) Start() {
go func() {
for {
select {
case job := <-w.jobs:
err := job.F()
if err != nil {
log.Warn(fmt.Sprintf("Job has errored out: %s", err.Error()))
}
job.RepeatCounter++
w.dispatchStatus <- &DispatchStatus{Type: "worker", ID: w.ID, Status: "quit"}
// See if it is repeatable
if job.IsRepeatable {
if job.RepeatCounter < job.RepeatCount || job.RepeatCount == -1 {
w.Dispatcher.AddJob(job.F, job.IsRepeatable, job.RepeatCount, job.RepeatCounter)
} else {
w.Quit <- true
}
} else {
w.Quit <- true
}
case <-w.Quit:
return
}
}
}()
}