-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathrun.go
37 lines (29 loc) · 886 Bytes
/
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
package floc
// Run creates a new Context and Control and runs the flow.
func Run(job Job) (result Result, data interface{}, err error) {
// Create context and control
ctx := NewContext()
defer ctx.Release()
ctrl := NewControl(ctx)
defer ctrl.Release()
// Run the flow
return RunWith(ctx, ctrl, job)
}
// RunWith runs the flow with the Context and Control given.
func RunWith(ctx Context, ctrl Control, job Job) (result Result, data interface{}, err error) {
// Return invalid job error if the job is nil
if job == nil {
return None, nil, ErrInvalidJob{}
}
// Run the flow and return the result
unhandledErr := job(ctx, ctrl)
result, data, err = ctrl.Result()
if result != None {
return result, data, err
}
// Return Failed if unhandled error left after the execution.
if unhandledErr != nil {
return Failed, nil, unhandledErr
}
return None, nil, nil
}