forked from jrallison/go-workers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workers.go
152 lines (120 loc) · 2.76 KB
/
workers.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package workers
import (
"errors"
"fmt"
"log"
"net/http"
"os"
"sync"
"github.com/garyburd/redigo/redis"
)
var Logger WorkersLogger = log.New(os.Stdout, "workers: ", log.Ldate|log.Lmicroseconds)
type Workers struct {
config *config
managers map[string]*manager
schedule *scheduled
control map[string]chan string
access sync.Mutex
started bool
beforeStart []func()
duringDrain []func()
}
// ensure that Workers struct fulfils GoWorkers interface
var _ GoWorkers = (*Workers)(nil)
func NewWorkers(config *config) *Workers {
return &Workers{
config: config,
managers: make(map[string]*manager),
control: make(map[string]chan string),
access: sync.Mutex{},
started: false,
}
}
func newDefaultMiddlewares(config *config) *Middlewares {
return NewMiddleware(
&MiddlewareRetry{config},
&MiddlewareStats{config},
)
}
func (w *Workers) RedisPool() *redis.Pool {
return w.config.Pool
}
func (w *Workers) Process(queue string, job jobFunc, concurrency int, mids ...Action) {
w.access.Lock()
defer w.access.Unlock()
w.managers[queue] = newManager(w.config, queue, job, concurrency, mids...)
}
func (w *Workers) Run() {
w.Start()
go w.handleSignals()
w.WaitForExit()
}
func (w *Workers) ResetManagers() error {
w.access.Lock()
defer w.access.Unlock()
if w.started {
return errors.New("Cannot reset worker managers while workers are running")
}
w.managers = make(map[string]*manager)
return nil
}
func (w *Workers) Start() {
w.access.Lock()
defer w.access.Unlock()
if w.started {
return
}
runHooks(w.beforeStart)
w.startSchedule()
w.startManagers()
w.started = true
}
func (w *Workers) Quit() {
w.access.Lock()
defer w.access.Unlock()
if !w.started {
return
}
w.quitManagers()
w.quitSchedule()
runHooks(w.duringDrain)
w.WaitForExit()
w.started = false
}
func StatsServer(workers *Workers, port int) {
statsClosure := func(w http.ResponseWriter, req *http.Request) {
Stats(workers, w, req)
}
http.HandleFunc("/stats", statsClosure)
Logger.Println("Stats are available at", fmt.Sprint("http://localhost:", port, "/stats"))
if err := http.ListenAndServe(fmt.Sprint(":", port), nil); err != nil {
Logger.Println(err)
}
}
func (w *Workers) startSchedule() {
if w.schedule == nil {
w.schedule = newScheduled(w.config, w.config.retryQueue, w.config.scheduledJobsQueue)
}
w.schedule.start()
}
func (w *Workers) quitSchedule() {
if w.schedule != nil {
w.schedule.quit()
w.schedule = nil
}
}
func (w *Workers) startManagers() {
for _, manager := range w.managers {
manager.start()
}
}
func (w *Workers) quitManagers() {
for _, m := range w.managers {
go (func(m *manager) { m.quit() })(m)
}
}
func (w *Workers) WaitForExit() {
for _, manager := range w.managers {
manager.Wait()
}
}