@@ -10,11 +10,12 @@ import (
10
10
"github.com/cschleiden/go-workflows/internal/history"
11
11
"github.com/cschleiden/go-workflows/internal/task"
12
12
"github.com/cschleiden/go-workflows/workflow"
13
- "github.com/go-redis/redis/v8"
14
13
"github.com/pkg/errors"
15
14
)
16
15
17
16
func (rb * redisBackend ) GetWorkflowTask (ctx context.Context ) (* task.Workflow , error ) {
17
+ // TODO: Check for timer events, and add them to pending events if required
18
+
18
19
instanceTask , err := rb .workflowQueue .Dequeue (ctx , rb .options .WorkflowLockTimeout , rb .options .BlockTimeout )
19
20
if err != nil {
20
21
return nil , err
@@ -70,8 +71,6 @@ func (rb *redisBackend) GetWorkflowTask(ctx context.Context) (*task.Workflow, er
70
71
// Remove all pending events
71
72
rb .rdb .XTrim (ctx , pendingEventsKey (instanceTask .ID ), 0 )
72
73
73
- log .Println ("Returned task for " , instanceTask )
74
-
75
74
return & task.Workflow {
76
75
ID : instanceTask .ID ,
77
76
WorkflowInstance : core .NewWorkflowInstance (instanceTask .ID , instanceState .ExecutionID ),
@@ -85,30 +84,11 @@ func (rb *redisBackend) ExtendWorkflowTask(ctx context.Context, taskID string, i
85
84
}
86
85
87
86
func (rb * redisBackend ) CompleteWorkflowTask (ctx context.Context , taskID string , instance core.WorkflowInstance , state backend.WorkflowState , executedEvents []history.Event , activityEvents []history.Event , workflowEvents []history.WorkflowEvent ) error {
88
-
89
- // Add events to stream
90
- var lastMessageID string
91
-
87
+ // Add executed events to the history
92
88
for _ , executedEvent := range executedEvents {
93
- // TODO: Use pipeline
94
- eventData , err := json .Marshal (executedEvent )
95
- if err != nil {
89
+ if err := addEventToStream (ctx , rb .rdb , historyKey (instance .GetInstanceID ()), & executedEvent ); err != nil {
96
90
return err
97
91
}
98
-
99
- cmd := rb .rdb .XAdd (ctx , & redis.XAddArgs {
100
- Stream : historyKey (instance .GetInstanceID ()),
101
- ID : "*" ,
102
- Values : map [string ]interface {}{
103
- "event" : string (eventData ),
104
- },
105
- })
106
- id , err := cmd .Result ()
107
- if err != nil {
108
- return errors .Wrap (err , "could not create event stream" )
109
- }
110
-
111
- lastMessageID = id
112
92
}
113
93
114
94
// Send new events to the respective streams
@@ -130,27 +110,15 @@ func (rb *redisBackend) CompleteWorkflowTask(ctx context.Context, taskID string,
130
110
131
111
// Insert pending events for target instance
132
112
for _ , event := range events {
133
- // TODO: Use pipeline
134
- eventData , err := json .Marshal (event )
135
- if err != nil {
113
+ if err := addEventToStream (ctx , rb .rdb , pendingEventsKey (instance .GetInstanceID ()), & event ); err != nil {
136
114
return err
137
115
}
138
-
139
- cmd := rb .rdb .XAdd (ctx , & redis.XAddArgs {
140
- Stream : pendingEventsKey (targetInstance .GetInstanceID ()),
141
- ID : "*" ,
142
- Values : map [string ]interface {}{
143
- "event" : string (eventData ),
144
- },
145
- })
146
- _ , err = cmd .Result ()
147
- if err != nil {
148
- return errors .Wrap (err , "could not create event stream" )
149
- }
150
116
}
151
117
152
118
// TODO: Delay unlocking the current instance. Can we find a better way here?
153
119
if targetInstance != instance {
120
+ // TODO: Make sure this task is not already enqueued
121
+
154
122
if err := rb .workflowQueue .Enqueue (ctx , targetInstance .GetInstanceID (), nil ); err != nil {
155
123
return errors .Wrap (err , "could not add instance to locked instances set" )
156
124
}
@@ -164,7 +132,6 @@ func (rb *redisBackend) CompleteWorkflowTask(ctx context.Context, taskID string,
164
132
}
165
133
166
134
instanceState .State = state
167
- instanceState .LastMessageID = lastMessageID // TODO: Do we need this?
168
135
169
136
if err := updateInstance (ctx , rb .rdb , instance .GetInstanceID (), instanceState ); err != nil {
170
137
return errors .Wrap (err , "could not update workflow instance" )
@@ -182,7 +149,7 @@ func (rb *redisBackend) CompleteWorkflowTask(ctx context.Context, taskID string,
182
149
}
183
150
}
184
151
185
- // Unlock instance
152
+ // Complete workflow task and unlock instance
186
153
if err := rb .workflowQueue .Complete (ctx , taskID ); err != nil {
187
154
return errors .Wrap (err , "could not complete workflow task" )
188
155
}
@@ -193,22 +160,11 @@ func (rb *redisBackend) CompleteWorkflowTask(ctx context.Context, taskID string,
193
160
}
194
161
195
162
func (rb * redisBackend ) addWorkflowInstanceEvent (ctx context.Context , instance core.WorkflowInstance , event history.Event ) error {
196
- // Add pending event
197
- eventData , err := json .Marshal (event )
198
- if err != nil {
163
+ // Add event to pending events for instance
164
+ if err := addEventToStream (ctx , rb .rdb , pendingEventsKey (instance .GetInstanceID ()), & event ); err != nil {
199
165
return err
200
166
}
201
167
202
- if err := rb .rdb .XAdd (ctx , & redis.XAddArgs {
203
- Stream : pendingEventsKey (instance .GetInstanceID ()),
204
- ID : "*" ,
205
- Values : map [string ]interface {}{
206
- "event" : string (eventData ),
207
- },
208
- }).Err (); err != nil {
209
- return errors .Wrap (err , "could not add event to stream" )
210
- }
211
-
212
168
// Queue workflow task
213
169
// TODO: Ensure this can only be queued once
214
170
if err := rb .workflowQueue .Enqueue (ctx , instance .GetInstanceID (), nil ); err != nil {
0 commit comments