-
Notifications
You must be signed in to change notification settings - Fork 242
/
worker.go
155 lines (134 loc) · 3.3 KB
/
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
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
153
154
155
package goworker
import (
"encoding/json"
"errors"
"fmt"
"sync"
"time"
)
type worker struct {
process
}
func newWorker(id string, queues []string) (*worker, error) {
process, err := newProcess(id, queues)
if err != nil {
return nil, err
}
return &worker{
process: *process,
}, nil
}
func (w *worker) MarshalJSON() ([]byte, error) {
return json.Marshal(w.String())
}
func (w *worker) start(conn *RedisConn, job *Job) error {
work := &work{
Queue: job.Queue,
RunAt: time.Now(),
Payload: job.Payload,
}
buffer, err := json.Marshal(work)
if err != nil {
return err
}
conn.Send("SET", fmt.Sprintf("%sworker:%s", workerSettings.Namespace, w), buffer)
logger.Debugf("Processing %s since %s [%v]", work.Queue, work.RunAt, work.Payload.Class)
return w.process.start(conn)
}
func (w *worker) fail(conn *RedisConn, job *Job, err error) error {
failure := &failure{
FailedAt: time.Now(),
Payload: job.Payload,
Exception: "Error",
Error: err.Error(),
Worker: w,
Queue: job.Queue,
}
buffer, err := json.Marshal(failure)
if err != nil {
return err
}
conn.Send("RPUSH", fmt.Sprintf("%sfailed", workerSettings.Namespace), buffer)
return w.process.fail(conn)
}
func (w *worker) succeed(conn *RedisConn, job *Job) error {
conn.Send("INCR", fmt.Sprintf("%sstat:processed", workerSettings.Namespace))
conn.Send("INCR", fmt.Sprintf("%sstat:processed:%s", workerSettings.Namespace, w))
return nil
}
func (w *worker) finish(conn *RedisConn, job *Job, err error) error {
if err != nil {
w.fail(conn, job, err)
} else {
w.succeed(conn, job)
}
return w.process.finish(conn)
}
func (w *worker) work(jobs <-chan *Job, monitor *sync.WaitGroup) {
conn, err := GetConn()
if err != nil {
logger.Criticalf("Error on getting connection in worker %v: %v", w, err)
return
} else {
w.open(conn)
PutConn(conn)
}
monitor.Add(1)
go func() {
defer func() {
defer monitor.Done()
conn, err := GetConn()
if err != nil {
logger.Criticalf("Error on getting connection in worker %v: %v", w, err)
return
} else {
w.close(conn)
PutConn(conn)
}
}()
for job := range jobs {
if workerFunc, ok := workers[job.Payload.Class]; ok {
w.run(job, workerFunc)
logger.Debugf("done: (Job{%s} | %s | %v)", job.Queue, job.Payload.Class, job.Payload.Args)
} else {
errorLog := fmt.Sprintf("No worker for %s in queue %s with args %v", job.Payload.Class, job.Queue, job.Payload.Args)
logger.Critical(errorLog)
conn, err := GetConn()
if err != nil {
logger.Criticalf("Error on getting connection in worker %v: %v", w, err)
return
} else {
w.finish(conn, job, errors.New(errorLog))
PutConn(conn)
}
}
}
}()
}
func (w *worker) run(job *Job, workerFunc workerFunc) {
var err error
defer func() {
conn, errCon := GetConn()
if errCon != nil {
logger.Criticalf("Error on getting connection in worker on finish %v: %v", w, errCon)
return
} else {
w.finish(conn, job, err)
PutConn(conn)
}
}()
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Sprint(r))
}
}()
conn, err := GetConn()
if err != nil {
logger.Criticalf("Error on getting connection in worker on start %v: %v", w, err)
return
} else {
w.start(conn, job)
PutConn(conn)
}
err = workerFunc(job.Queue, job.Payload.Args...)
}