forked from scrapbird/gotowork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgotowork.go
64 lines (56 loc) · 1.45 KB
/
gotowork.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
// Provides a simple and easy to use worker queue.
package gotowork
// Define work as a first class function
type Work func()
// Define the worker queue
type WorkerQueue chan Inbox
// Define the inbox
type Inbox chan Work
// This is the worker type, this is used to keep track of which queue the worker is in and the worker id, signaling channels etc.
type Worker struct {
id int
inbox Inbox
workerQueue WorkerQueue
quit chan bool
done chan bool
}
// Creates a new worker struct and initializes it.
//
// workerQueue := make(WorkerQueue, 5) // Create worker queue that fits up to 5 workers.
// worker := NewWorker(1, workerQueue) // Create a new worker in the queue.
func NewWorker(id int, workerQueue WorkerQueue) Worker {
worker := Worker{
id: id,
inbox: make(chan Work),
workerQueue: workerQueue,
quit: make(chan bool),
done: make(chan bool),
}
return worker
}
// Tells the worker to start subscribing to the worker queue and executing jobs.
func (w Worker) Start() {
go func() {
for {
// signal that we are ready for work
w.workerQueue <- w.inbox
select {
case work := <-w.inbox:
work()
case <-w.quit:
w.done <- true
return
}
}
}()
}
// Tells the worker to stop once it has finished it's current job.
func (w Worker) Stop() {
go func() {
w.quit <- true
}()
}
// Waits for a worker to finish before returning.
func (w Worker) WaitForFinish() {
<-w.done
}