-
Notifications
You must be signed in to change notification settings - Fork 3
/
pool.go
59 lines (48 loc) · 1.12 KB
/
pool.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 axe
import (
"fmt"
)
// Pool manages tasks and queues.
type Pool struct {
tasks map[string]*Task
queues map[*Queue]bool
closed chan struct{}
// The function gets invoked by the pool with critical errors.
Reporter func(error)
}
// NewPool creates and returns a new pool.
func NewPool() *Pool {
return &Pool{
tasks: make(map[string]*Task),
queues: make(map[*Queue]bool),
closed: make(chan struct{}),
}
}
// Add will add the specified task and its queue to the pool.
func (p *Pool) Add(task *Task) {
// check existence
if p.tasks[task.Name] != nil {
panic(fmt.Sprintf(`axe: task with name "%s" already exists`, task.Name))
}
// save task
p.tasks[task.Name] = task
// add task to queue
task.Queue.tasks = append(task.Queue.tasks, task.Name)
// save queue
p.queues[task.Queue] = true
}
// Run will launch the queue watchers and task workers in the background.
func (p *Pool) Run() {
// start all queues
for queue := range p.queues {
queue.start(p)
}
// start all tasks
for _, task := range p.tasks {
task.start(p)
}
}
// Close will close the pool.
func (p *Pool) Close() {
close(p.closed)
}