forked from gocraft/work
-
Notifications
You must be signed in to change notification settings - Fork 4
/
run.go
101 lines (84 loc) · 2.69 KB
/
run.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
package work
import (
"context"
"fmt"
"reflect"
)
// runJob returns an error if the job fails, or there's a panic, or we couldn't
// reflect correctly. if we return an error, it signals we want the job to be retried.
func runJob(
job *Job,
ctxType reflect.Type,
middlewares []*middlewareHandler,
jt *jobType,
logger StructuredLogger,
) (returnCtx reflect.Value, returnError error) {
returnCtx = reflect.New(ctxType)
ctx := job.extractTraceContext(context.Background())
next := func() error {
mw := chainMiddleware(returnCtx, middlewares)
if jt.isGeneric {
switch h := jt.genericHandler.(type) {
case JobHandler:
return mw(ctx, job, func(_ context.Context, j *Job) error { return h(j) })
case JobContextHandler:
return mw(ctx, job, h)
}
}
return mw(ctx, job, func(_ context.Context, j *Job) error {
res := jt.dynamicHandler.Call([]reflect.Value{returnCtx, reflect.ValueOf(job)})
x := res[0].Interface()
if x == nil {
return nil
}
return x.(error)
})
}
defer func() {
if panicErr := recover(); panicErr != nil {
// err turns out to be interface{}, of actual type "runtime.errorCString"
// Luckily, the err sprints nicely via fmt.
errorishError := fmt.Errorf("%v", panicErr)
logger.Error("runJob.panic", errAttr(errorishError))
returnError = errorishError
}
}()
returnError = next()
return
}
// chainMiddleware creates a single middleware out of a chain of many middlewares.
//
// Execution is done in left-to-right order, including passing of context.
// For example chainMiddleware(one, two, three) will execute one before two before
// three, and three will see context changes of one and two.
func chainMiddleware(returnCtx reflect.Value, middlewares []*middlewareHandler) JobContextMiddleware {
n := len(middlewares)
return func(ctx context.Context, j *Job, jch JobContextHandler) error {
chainer := func(currentMw *middlewareHandler, currentHandler JobContextHandler) JobContextHandler {
return func(currentCtx context.Context, j *Job) error {
bareHandler := func() error {
return currentHandler(currentCtx, j)
}
if currentMw.isGeneric {
switch mwh := currentMw.genericMiddleware.(type) {
case JobMiddleware:
return mwh(j, bareHandler)
case JobContextMiddleware:
return mwh(currentCtx, j, currentHandler)
}
}
res := currentMw.dynamicMiddleware.Call([]reflect.Value{returnCtx, reflect.ValueOf(j), reflect.ValueOf(bareHandler)})
x := res[0].Interface()
if x == nil {
return nil
}
return x.(error)
}
}
chainedHandler := jch
for i := n - 1; i >= 0; i-- {
chainedHandler = chainer(middlewares[i], chainedHandler)
}
return chainedHandler(ctx, j)
}
}