Skip to content

Commit 76fea46

Browse files
committed
Task Do implementation and refactoring
Signed-off-by: Ricardo Zanini <[email protected]>
1 parent 85ffc5e commit 76fea46

File tree

8 files changed

+320
-251
lines changed

8 files changed

+320
-251
lines changed

impl/context.go

Lines changed: 62 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ type ctxKey string
1010

1111
const runnerCtxKey ctxKey = "wfRunnerContext"
1212

13-
// WorkflowRunnerContext holds the necessary data for the workflow execution within the instance.
14-
type WorkflowRunnerContext struct {
13+
// WorkflowContext holds the necessary data for the workflow execution within the instance.
14+
type WorkflowContext struct {
1515
mu sync.Mutex
1616
input interface{} // input can hold any type
1717
output interface{} // output can hold any type
@@ -20,112 +20,116 @@ type WorkflowRunnerContext struct {
2020
TasksStatusPhase map[string][]StatusPhaseLog // Holds `$context` as the key
2121
}
2222

23-
func (runnerCtx *WorkflowRunnerContext) SetStatus(status StatusPhase) {
24-
runnerCtx.mu.Lock()
25-
defer runnerCtx.mu.Unlock()
26-
if runnerCtx.StatusPhase == nil {
27-
runnerCtx.StatusPhase = []StatusPhaseLog{}
23+
type TaskContext interface {
24+
SetTaskStatus(task string, status StatusPhase)
25+
}
26+
27+
func (ctx *WorkflowContext) SetStatus(status StatusPhase) {
28+
ctx.mu.Lock()
29+
defer ctx.mu.Unlock()
30+
if ctx.StatusPhase == nil {
31+
ctx.StatusPhase = []StatusPhaseLog{}
2832
}
29-
runnerCtx.StatusPhase = append(runnerCtx.StatusPhase, NewStatusPhaseLog(status))
33+
ctx.StatusPhase = append(ctx.StatusPhase, NewStatusPhaseLog(status))
3034
}
3135

32-
func (runnerCtx *WorkflowRunnerContext) SetTaskStatus(task string, status StatusPhase) {
33-
runnerCtx.mu.Lock()
34-
defer runnerCtx.mu.Unlock()
35-
if runnerCtx.TasksStatusPhase == nil {
36-
runnerCtx.TasksStatusPhase = map[string][]StatusPhaseLog{}
36+
func (ctx *WorkflowContext) SetTaskStatus(task string, status StatusPhase) {
37+
ctx.mu.Lock()
38+
defer ctx.mu.Unlock()
39+
if ctx.TasksStatusPhase == nil {
40+
ctx.TasksStatusPhase = map[string][]StatusPhaseLog{}
3741
}
38-
runnerCtx.TasksStatusPhase[task] = append(runnerCtx.TasksStatusPhase[task], NewStatusPhaseLog(status))
42+
ctx.TasksStatusPhase[task] = append(ctx.TasksStatusPhase[task], NewStatusPhaseLog(status))
3943
}
4044

41-
// SetWorkflowCtx safely sets the `$context` value
42-
func (runnerCtx *WorkflowRunnerContext) SetWorkflowCtx(value interface{}) {
43-
runnerCtx.mu.Lock()
44-
defer runnerCtx.mu.Unlock()
45-
if runnerCtx.context == nil {
46-
runnerCtx.context = make(map[string]interface{})
45+
// SetInstanceCtx safely sets the `$context` value
46+
func (ctx *WorkflowContext) SetInstanceCtx(value interface{}) {
47+
ctx.mu.Lock()
48+
defer ctx.mu.Unlock()
49+
if ctx.context == nil {
50+
ctx.context = make(map[string]interface{})
4751
}
48-
runnerCtx.context["$context"] = value
52+
ctx.context["$context"] = value
4953
}
5054

51-
// GetWorkflowCtx safely retrieves the `$context` value
52-
func (runnerCtx *WorkflowRunnerContext) GetWorkflowCtx() interface{} {
53-
runnerCtx.mu.Lock()
54-
defer runnerCtx.mu.Unlock()
55-
if runnerCtx.context == nil {
55+
// GetInstanceCtx safely retrieves the `$context` value
56+
func (ctx *WorkflowContext) GetInstanceCtx() interface{} {
57+
ctx.mu.Lock()
58+
defer ctx.mu.Unlock()
59+
if ctx.context == nil {
5660
return nil
5761
}
58-
return runnerCtx.context["$context"]
62+
return ctx.context["$context"]
5963
}
6064

6165
// SetInput safely sets the input
62-
func (runnerCtx *WorkflowRunnerContext) SetInput(input interface{}) {
63-
runnerCtx.mu.Lock()
64-
defer runnerCtx.mu.Unlock()
65-
runnerCtx.input = input
66+
func (ctx *WorkflowContext) SetInput(input interface{}) {
67+
ctx.mu.Lock()
68+
defer ctx.mu.Unlock()
69+
ctx.input = input
6670
}
6771

6872
// GetInput safely retrieves the input
69-
func (runnerCtx *WorkflowRunnerContext) GetInput() interface{} {
70-
runnerCtx.mu.Lock()
71-
defer runnerCtx.mu.Unlock()
72-
return runnerCtx.input
73+
func (ctx *WorkflowContext) GetInput() interface{} {
74+
ctx.mu.Lock()
75+
defer ctx.mu.Unlock()
76+
return ctx.input
7377
}
7478

7579
// SetOutput safely sets the output
76-
func (runnerCtx *WorkflowRunnerContext) SetOutput(output interface{}) {
77-
runnerCtx.mu.Lock()
78-
defer runnerCtx.mu.Unlock()
79-
runnerCtx.output = output
80+
func (ctx *WorkflowContext) SetOutput(output interface{}) {
81+
ctx.mu.Lock()
82+
defer ctx.mu.Unlock()
83+
ctx.output = output
8084
}
8185

8286
// GetOutput safely retrieves the output
83-
func (runnerCtx *WorkflowRunnerContext) GetOutput() interface{} {
84-
runnerCtx.mu.Lock()
85-
defer runnerCtx.mu.Unlock()
86-
return runnerCtx.output
87+
func (ctx *WorkflowContext) GetOutput() interface{} {
88+
ctx.mu.Lock()
89+
defer ctx.mu.Unlock()
90+
return ctx.output
8791
}
8892

8993
// GetInputAsMap safely retrieves the input as a map[string]interface{}.
9094
// If input is not a map, it creates a map with an empty string key and the input as the value.
91-
func (runnerCtx *WorkflowRunnerContext) GetInputAsMap() map[string]interface{} {
92-
runnerCtx.mu.Lock()
93-
defer runnerCtx.mu.Unlock()
95+
func (ctx *WorkflowContext) GetInputAsMap() map[string]interface{} {
96+
ctx.mu.Lock()
97+
defer ctx.mu.Unlock()
9498

95-
if inputMap, ok := runnerCtx.input.(map[string]interface{}); ok {
99+
if inputMap, ok := ctx.input.(map[string]interface{}); ok {
96100
return inputMap
97101
}
98102

99103
// If input is not a map, create a map with an empty key and set input as the value
100104
return map[string]interface{}{
101-
"": runnerCtx.input,
105+
"": ctx.input,
102106
}
103107
}
104108

105109
// GetOutputAsMap safely retrieves the output as a map[string]interface{}.
106110
// If output is not a map, it creates a map with an empty string key and the output as the value.
107-
func (runnerCtx *WorkflowRunnerContext) GetOutputAsMap() map[string]interface{} {
108-
runnerCtx.mu.Lock()
109-
defer runnerCtx.mu.Unlock()
111+
func (ctx *WorkflowContext) GetOutputAsMap() map[string]interface{} {
112+
ctx.mu.Lock()
113+
defer ctx.mu.Unlock()
110114

111-
if outputMap, ok := runnerCtx.output.(map[string]interface{}); ok {
115+
if outputMap, ok := ctx.output.(map[string]interface{}); ok {
112116
return outputMap
113117
}
114118

115119
// If output is not a map, create a map with an empty key and set output as the value
116120
return map[string]interface{}{
117-
"": runnerCtx.output,
121+
"": ctx.output,
118122
}
119123
}
120124

121-
// WithRunnerContext adds the WorkflowRunnerContext to a parent context
122-
func WithRunnerContext(parent context.Context, wfCtx *WorkflowRunnerContext) context.Context {
125+
// WithWorkflowContext adds the WorkflowContext to a parent context
126+
func WithWorkflowContext(parent context.Context, wfCtx *WorkflowContext) context.Context {
123127
return context.WithValue(parent, runnerCtxKey, wfCtx)
124128
}
125129

126-
// GetRunnerContext retrieves the WorkflowRunnerContext from a context
127-
func GetRunnerContext(ctx context.Context) (*WorkflowRunnerContext, error) {
128-
wfCtx, ok := ctx.Value(runnerCtxKey).(*WorkflowRunnerContext)
130+
// GetWorkflowContext retrieves the WorkflowContext from a context
131+
func GetWorkflowContext(ctx context.Context) (*WorkflowContext, error) {
132+
wfCtx, ok := ctx.Value(runnerCtxKey).(*WorkflowContext)
129133
if !ok {
130134
return nil, errors.New("workflow context not found")
131135
}

0 commit comments

Comments
 (0)