forked from mail2fish/go-workers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workers.go
75 lines (60 loc) · 1.31 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
package workers
import (
"fmt"
"log"
"net/http"
"os"
)
const (
RETRY_KEY = "goretry"
SCHEDULED_JOBS_KEY = "schedule"
)
var Logger WorkersLogger = log.New(os.Stdout, "workers: ", log.Ldate|log.Lmicroseconds)
var managers = make(map[string]*manager)
var schedule = newScheduled(RETRY_KEY, SCHEDULED_JOBS_KEY)
var control = make(map[string]chan string)
var Middleware = NewMiddleware(
&MiddlewareLogging{},
&MiddlewareRetry{},
&MiddlewareStats{},
)
func Process(queue string, job jobFunc, concurrency int, mids ...Action) {
managers[queue] = newManager(queue, job, concurrency, mids...)
}
func Run() {
Start()
go handleSignals()
waitForExit()
}
func Start() {
schedule.start()
startManagers()
}
func Quit() {
quitManagers()
schedule.quit()
waitForExit()
}
func StatsServer(port int) {
http.HandleFunc("/stats", Stats)
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 startManagers() {
for _, manager := range managers {
manager.start()
}
}
func quitManagers() {
for _, m := range managers {
go (func(m *manager) { m.quit() })(m)
}
}
func waitForExit() {
for queue, manager := range managers {
manager.Wait()
delete(managers, queue)
}
}