@@ -10,8 +10,8 @@ type ctxKey string
10
10
11
11
const runnerCtxKey ctxKey = "wfRunnerContext"
12
12
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 {
15
15
mu sync.Mutex
16
16
input interface {} // input can hold any type
17
17
output interface {} // output can hold any type
@@ -20,112 +20,116 @@ type WorkflowRunnerContext struct {
20
20
TasksStatusPhase map [string ][]StatusPhaseLog // Holds `$context` as the key
21
21
}
22
22
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 {}
28
32
}
29
- runnerCtx .StatusPhase = append (runnerCtx .StatusPhase , NewStatusPhaseLog (status ))
33
+ ctx .StatusPhase = append (ctx .StatusPhase , NewStatusPhaseLog (status ))
30
34
}
31
35
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 {}
37
41
}
38
- runnerCtx .TasksStatusPhase [task ] = append (runnerCtx .TasksStatusPhase [task ], NewStatusPhaseLog (status ))
42
+ ctx .TasksStatusPhase [task ] = append (ctx .TasksStatusPhase [task ], NewStatusPhaseLog (status ))
39
43
}
40
44
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 {})
47
51
}
48
- runnerCtx .context ["$context" ] = value
52
+ ctx .context ["$context" ] = value
49
53
}
50
54
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 {
56
60
return nil
57
61
}
58
- return runnerCtx .context ["$context" ]
62
+ return ctx .context ["$context" ]
59
63
}
60
64
61
65
// 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
66
70
}
67
71
68
72
// 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
73
77
}
74
78
75
79
// 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
80
84
}
81
85
82
86
// 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
87
91
}
88
92
89
93
// GetInputAsMap safely retrieves the input as a map[string]interface{}.
90
94
// 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 ()
94
98
95
- if inputMap , ok := runnerCtx .input .(map [string ]interface {}); ok {
99
+ if inputMap , ok := ctx .input .(map [string ]interface {}); ok {
96
100
return inputMap
97
101
}
98
102
99
103
// If input is not a map, create a map with an empty key and set input as the value
100
104
return map [string ]interface {}{
101
- "" : runnerCtx .input ,
105
+ "" : ctx .input ,
102
106
}
103
107
}
104
108
105
109
// GetOutputAsMap safely retrieves the output as a map[string]interface{}.
106
110
// 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 ()
110
114
111
- if outputMap , ok := runnerCtx .output .(map [string ]interface {}); ok {
115
+ if outputMap , ok := ctx .output .(map [string ]interface {}); ok {
112
116
return outputMap
113
117
}
114
118
115
119
// If output is not a map, create a map with an empty key and set output as the value
116
120
return map [string ]interface {}{
117
- "" : runnerCtx .output ,
121
+ "" : ctx .output ,
118
122
}
119
123
}
120
124
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 {
123
127
return context .WithValue (parent , runnerCtxKey , wfCtx )
124
128
}
125
129
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 )
129
133
if ! ok {
130
134
return nil , errors .New ("workflow context not found" )
131
135
}
0 commit comments