-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.go
345 lines (278 loc) · 7.2 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
Package worker implements a task server compatible with graphile-worker.
Example:
package main
import (
"context"
"fmt"
"log"
worker "github.com/ioj/gophile-worker"
"github.com/ioj/gophile-worker/middleware"
)
type HelloPayload struct {
Name string `json:"name"`
}
func main() {
w := worker.New(nil)
w.Use(middleware.Logger)
w.Handle("hello", func(ctx context.Context, job *worker.Job) error {
p := HelloPayload{Name: "stranger"}
if err := job.UnmarshalPayload(&p); err != nil {
return err
}
fmt.Printf("Hello, %v!\n", p.Name)
return nil
})
log.Fatal(w.ListenAndServe("dbname=worker_test sslmode=disable"))
}
*/
package worker
import (
"context"
"database/sql"
"errors"
"fmt"
"sync"
"time"
"github.com/lib/pq"
)
// CtxKey is a key type for context values passed to handlers.
type CtxKey string
// CtxWorker is a context key which holds the worker instance.
const CtxWorker CtxKey = "worker"
// HandlerFunc defines a function to serve tasks.
type HandlerFunc func(ctx context.Context, job *Job) error
// MiddlewareFunc defines a function to process middleware.
type MiddlewareFunc func(HandlerFunc) HandlerFunc
// Used by Worker.stmts map
const (
getJobStmt = iota
failJobStmt
completeJobStmt
)
// Worker is the root of your worker application. You should instantiate one
// of these and call ListenAndServe() to start accepting jobs.
//
// Always use the New() method to create a new Worker.
type Worker struct {
opts *Opts
db *sql.DB
middlewares []MiddlewareFunc
handlerfuncs map[string]HandlerFunc
supportedIDs pq.StringArray
runningtasks sync.WaitGroup
// Prepared statements.
stmts map[int]*sql.Stmt
listening bool
shutdown chan bool
}
// New initialises a new Worker. Configuration options are provided
// via an instance of Opts. Calling this function in your code will
// probably look something like this if you want to use defaults:
//
// worker := worker.New(nil)
//
// or:
//
// opts := &worker.Opts{Schema: "worker"}
// worker := worker.New(opts)
//
func New(opts *Opts) *Worker {
return &Worker{opts: optsWithDefaults(opts)}
}
// ID returns the worker ID.
func (w *Worker) ID() string {
return w.opts.ID
}
// DB returns the pointer to the underlying *sql.DB.
// It might be null if worker wasn't started yet.
func (w *Worker) DB() *sql.DB {
return w.db
}
// Schema returns a quoted schema name used by the worker,
// as configured in Opts.
func (w *Worker) Schema() string {
return w.opts.Schema
}
// Handle registers the handler for the given task ID.
// If a handler already exists for the ID, Handle panics.
func (w *Worker) Handle(taskID string, task HandlerFunc) {
if w.listening {
panic("registering handlers when listening is forbidden")
}
if taskID == "" {
panic("invalid task id")
}
if _, ok := w.handlerfuncs[taskID]; ok {
panic("handler already registered")
}
if w.handlerfuncs == nil {
w.handlerfuncs = make(map[string]HandlerFunc)
}
w.handlerfuncs[taskID] = task
w.supportedIDs = append(w.supportedIDs, taskID)
}
// ListenAndServe opens a connection to the database and starts listening
// for incoming jobs. When a matching job appears, it is dispatched
// to a HandlerFunc.
func (w *Worker) ListenAndServe(conninfo string) error {
if w.listening {
return errors.New("worker is already listening")
}
if len(w.supportedIDs) == 0 {
return errors.New("no registered tasks")
}
w.shutdown = make(chan bool, 1)
var err error
if w.db, err = sql.Open("postgres", conninfo); err != nil {
return err
}
if err := w.db.Ping(); err != nil {
return err
}
if err := w.prepareStmts(); err != nil {
return err
}
listener := pq.NewListener(conninfo,
w.opts.MinReconnectInterval, w.opts.MaxReconnectInterval, nil)
err = listener.Listen("jobs:insert")
if err != nil {
return err
}
for {
if err := w.getJob(); err != nil {
panic(err)
}
select {
case <-w.shutdown:
goto shutdown
case <-listener.Notify:
case <-time.After(w.opts.PollInterval):
go listener.Ping()
}
}
shutdown:
w.runningtasks.Wait()
if err := listener.Close(); err != nil {
return err
}
if err := w.closePreparedStmts(); err != nil {
return nil
}
return w.db.Close()
}
// Shutdown gracefully shuts down the worker without interrupting any active tasks.
func (w *Worker) Shutdown() {
w.shutdown <- true
}
// Use appends a middleware handler to the Worker middleware stack.
//
// The middleware stack for a Worker will execute before searching for a matching
// taskID to a specific handler, which provides opportunity to respond early,
// change the course of the request execution, or set request-scoped values for
// the next Handler.
func (w *Worker) Use(middlewares ...MiddlewareFunc) {
if w.listening {
panic("adding middleware when listening is forbidden")
}
w.middlewares = append(w.middlewares, middlewares...)
}
func (w *Worker) closePreparedStmts() error {
for _, stmt := range w.stmts {
if err := stmt.Close(); err != nil {
return err
}
}
w.stmts = nil
return nil
}
func (w *Worker) prepareStmts() error {
var err error
if w.stmts == nil {
w.stmts = make(map[int]*sql.Stmt)
} else {
w.closePreparedStmts()
}
w.stmts[getJobStmt], err = w.db.Prepare(fmt.Sprintf(`
SELECT id, queue_name, task_identifier, payload
FROM %v.get_job($1, $2)`, w.opts.Schema))
if err != nil {
return err
}
w.stmts[completeJobStmt], err = w.db.Prepare(fmt.Sprintf(`
SELECT FROM %v.complete_job($1, $2)`, w.opts.Schema))
if err != nil {
return err
}
w.stmts[failJobStmt], err = w.db.Prepare(fmt.Sprintf(`
SELECT FROM %v.fail_job($1, $2, $3)`, w.opts.Schema))
return err
}
func (w *Worker) getJob() error {
for {
var id sql.NullInt64
var queueName sql.NullString
var taskID sql.NullString
var payload []byte
if err := w.stmts[getJobStmt].QueryRow(w.opts.ID, w.supportedIDs).
Scan(&id, &queueName, &taskID, &payload); err != nil {
return err
}
if !id.Valid || !taskID.Valid {
// get_job() returned null
return nil
}
job := &Job{
ID: id.Int64,
TaskID: taskID.String,
QueueName: queueName.String,
payload: payload,
}
go func(job *Job) {
w.runningtasks.Add(1)
defer w.runningtasks.Done()
w.doJob(job)
}(job)
}
}
func (w *Worker) failJob(job *Job, e error) error {
_, err := w.stmts[failJobStmt].Exec(w.opts.ID, job.ID, e.Error())
return err
}
func (w *Worker) completeJob(job *Job) error {
_, err := w.stmts[completeJobStmt].Exec(w.opts.ID, job.ID)
return err
}
func (w *Worker) doJob(job *Job) {
h, ok := w.handlerfuncs[job.TaskID]
if !ok {
panic("got task without handler. this should never happen")
}
// Recover from panics
defer func() {
if r := recover(); r != nil {
err, ok := r.(error)
if !ok {
err = fmt.Errorf("panic: %v", r)
}
w.failJob(job, err)
}
}()
for i := len(w.middlewares) - 1; i >= 0; i-- {
h = w.middlewares[i](h)
}
ctx := context.WithValue(context.Background(), CtxWorker, w)
if err := h(ctx, job); err != nil {
// TODO: retry mechanism
ferr := w.failJob(job, err)
if ferr != nil {
panic("couldn't fail job: " + ferr.Error())
}
} else {
// TODO: retry mechanism
ferr := w.completeJob(job)
if ferr != nil {
panic("couldn't complete job: " + ferr.Error())
}
}
}