-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.go
146 lines (127 loc) · 3.52 KB
/
task.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
package lambdag
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
)
type Task struct {
dag *DAG
id string
handler TaskHandler
opts TaskOptions
}
type TaskOptions struct {
newLoggerFunc func(context.Context, *DAGRunContext) (*log.Logger, error)
newLockerFunc func(context.Context, *DAGRunContext) (LockerWithError, error)
}
type TaskRequest struct {
DAGRunID string
DAGRunConfig json.RawMessage
TaskResponses map[string]json.RawMessage
Logger *log.Logger
}
type TaskHandler interface {
Invoke(context.Context, *TaskRequest) (interface{}, error)
}
type TaskHandlerFunc func(context.Context, *TaskRequest) (interface{}, error)
func (h TaskHandlerFunc) Invoke(ctx context.Context, req *TaskRequest) (interface{}, error) {
return h(ctx, req)
}
func WithTaskLogger(fn func(context.Context, *DAGRunContext) (*log.Logger, error)) func(opts *TaskOptions) error {
return func(opts *TaskOptions) error {
opts.newLoggerFunc = fn
return nil
}
}
func WithTaskLocker(fn func(context.Context, *DAGRunContext) (LockerWithError, error)) func(opts *TaskOptions) error {
return func(opts *TaskOptions) error {
opts.newLockerFunc = fn
return nil
}
}
func newTask(dag *DAG, id string, handler TaskHandler, optFns ...func(opts *TaskOptions) error) (*Task, error) {
task := &Task{
dag: dag,
id: id,
handler: handler,
}
for _, optFn := range optFns {
if err := optFn(&task.opts); err != nil {
return nil, err
}
}
return task, nil
}
func (task *Task) ID() string {
return task.id
}
func (task *Task) TaskHandler() TaskHandler {
return task.handler
}
func (task *Task) NewLogger(ctx context.Context, dagRunCtx *DAGRunContext) (*log.Logger, error) {
if task.opts.newLoggerFunc == nil {
return task.dag.NewLogger(ctx, dagRunCtx)
}
return task.opts.newLoggerFunc(ctx, dagRunCtx)
}
func (task *Task) NewLocker(ctx context.Context, dagRunCtx *DAGRunContext) (LockerWithError, error) {
if task.opts.newLockerFunc == nil {
return NopLocker{}, nil
}
return task.opts.newLockerFunc(ctx, dagRunCtx)
}
func (task *Task) SetDownstream(descendants ...*Task) error {
for _, descendant := range descendants {
if err := task.dag.AddDependency(task, descendant); err != nil {
return err
}
}
return nil
}
func (task *Task) SetUpstream(ancestors ...*Task) error {
for _, ancestor := range ancestors {
if err := task.dag.AddDependency(ancestor, task); err != nil {
return err
}
}
return nil
}
func (task *Task) String() string {
return task.id
}
func (task *Task) GoString() string {
return fmt.Sprintf("*lambdag.Task{ID:%s}", task.ID())
}
func (task *Task) Execute(ctx context.Context, dagRunCtx *DAGRunContext) (json.RawMessage, error) {
l, err := task.NewLogger(ctx, dagRunCtx)
if err != nil {
return nil, err
}
locker, err := task.NewLocker(ctx, dagRunCtx)
if err != nil {
l.Printf("[error] create locker : DAGRunId %s Error %s", dagRunCtx.DAGRunID, err.Error())
return nil, err
}
lockGranted, err := locker.LockWithErr(ctx)
if err != nil {
l.Printf("[error] lock : DAGRunId %s Error %s", dagRunCtx.DAGRunID, err.Error())
return nil, err
}
if !lockGranted {
l.Printf("[warn] can not get lock : DAGRunId %s", dagRunCtx.DAGRunID)
return nil, WrapTaskRetryable(errors.New("can not get lock"))
}
req := &TaskRequest{
DAGRunID: dagRunCtx.DAGRunID,
DAGRunConfig: dagRunCtx.DAGRunConfig,
TaskResponses: dagRunCtx.TaskResponses,
Logger: l,
}
resp, err := task.TaskHandler().Invoke(ctx, req)
if err != nil {
return nil, err
}
return json.Marshal(resp)
}