-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathworker.go
46 lines (40 loc) · 927 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 jobqueue
import (
"sync"
)
// Worker - the worker threads that actually process the jobs
type Worker struct {
done *sync.WaitGroup
readyPool chan chan Job
assignedJobQueue chan Job
quit chan bool
}
// NewWorker creates a new worker
func NewWorker(readyPool chan chan Job, done *sync.WaitGroup) *Worker {
return &Worker{
done: done,
readyPool: readyPool,
assignedJobQueue: make(chan Job),
quit: make(chan bool),
}
}
// Start - begins the job processing loop for the worker
func (w *Worker) Start() {
go func() {
w.done.Add(1)
for {
w.readyPool <- w.assignedJobQueue // check the job queue in
select {
case job := <-w.assignedJobQueue: // see if anything has been assigned to the queue
job.Process()
case <-w.quit:
w.done.Done()
return
}
}
}()
}
// Stop stops the worker
func (w *Worker) Stop() {
w.quit <- true
}