-
Notifications
You must be signed in to change notification settings - Fork 345
/
run.go
54 lines (48 loc) · 1.4 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
package work
import (
"fmt"
"reflect"
)
// 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, middleware []*middlewareHandler, jt *jobType) (returnCtx reflect.Value, returnError error) {
returnCtx = reflect.New(ctxType)
currentMiddleware := 0
maxMiddleware := len(middleware)
var next NextMiddlewareFunc
next = func() error {
if currentMiddleware < maxMiddleware {
mw := middleware[currentMiddleware]
currentMiddleware++
if mw.IsGeneric {
return mw.GenericMiddlewareHandler(job, next)
}
res := mw.DynamicMiddleware.Call([]reflect.Value{returnCtx, reflect.ValueOf(job), reflect.ValueOf(next)})
x := res[0].Interface()
if x == nil {
return nil
}
return x.(error)
}
if jt.IsGeneric {
return jt.GenericHandler(job)
}
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)
logError("runJob.panic", errorishError)
returnError = errorishError
}
}()
returnError = next()
return
}