@@ -52,6 +52,38 @@ for i = 1, executedEvents do
52
52
lastSequenceId = tonumber (sequenceId )
53
53
end
54
54
55
+ -- Remove executed pending events
56
+ local lastPendingEventMessageId = getArgv ()
57
+ redis .call (" XTRIM" , pendingEventsKey , " MINID" , lastPendingEventMessageId )
58
+ redis .call (" XDEL" , pendingEventsKey , lastPendingEventMessageId )
59
+
60
+ -- Update instance state
61
+ local now = getArgv ()
62
+ local state = tonumber (getArgv ())
63
+
64
+ -- State constants
65
+ local ContinuedAsNew = tonumber (getArgv ())
66
+ local Finished = tonumber (getArgv ())
67
+
68
+ instance [" state" ] = state
69
+
70
+ -- If workflow instance finished, remove active execution
71
+ local activeInstanceExecutionKey = getKey ()
72
+ if state == ContinuedAsNew or state == Finished then
73
+ -- Remove active execution
74
+ redis .call (" DEL" , activeInstanceExecutionKey )
75
+
76
+ instance [" completed_at" ] = now
77
+
78
+ redis .call (" SREM" , activeInstancesKey , instanceSegment )
79
+ end
80
+
81
+ if lastSequenceId > 0 then
82
+ instance [" last_sequence_id" ] = lastSequenceId
83
+ end
84
+
85
+ redis .call (" SET" , instanceKey , cjson .encode (instance ))
86
+
55
87
-- Remove canceled timers
56
88
local timersToCancel = tonumber (getArgv ())
57
89
for i = 1 , timersToCancel do
@@ -80,6 +112,20 @@ for i = 1, timersToSchedule do
80
112
storePayload (eventId , payloadData )
81
113
end
82
114
115
+ -- Schedule activities
116
+ local activities = tonumber (getArgv ())
117
+ local activitySetKey = getKey ()
118
+ local activityStreamKey = getKey ()
119
+ for i = 1 , activities do
120
+ local activityId = getArgv ()
121
+ local activityData = getArgv ()
122
+
123
+ local added = redis .call (" SADD" , activitySetKey , activityId )
124
+ if added == 1 then
125
+ redis .call (" XADD" , activityStreamKey , " *" , " id" , activityId , " data" , activityData )
126
+ end
127
+ end
128
+
83
129
-- Send events to other workflow instances
84
130
local otherWorkflowInstances = tonumber (getArgv ())
85
131
for i = 1 , otherWorkflowInstances do
@@ -102,7 +148,7 @@ for i = 1, otherWorkflowInstances do
102
148
local conflictEventPayloadData = getArgv ()
103
149
104
150
-- Does the instance exist already?
105
- local instanceExists = redis .call (" EXISTS" , targetActiveInstanceExecutionState )
151
+ local instanceExists = redis .call (" EXISTS" , targetActiveInstanceExecutionKey )
106
152
if instanceExists == 1 then
107
153
redis .call (" XADD" , pendingEventsKey , " *" , " event" , conflictEventData )
108
154
storePayload (conflictEventId , conflictEventPayloadData )
@@ -147,61 +193,14 @@ for i = 1, otherWorkflowInstances do
147
193
end
148
194
end
149
195
150
- -- Update instance state
151
- local now = getArgv ()
152
- local state = tonumber (getArgv ())
153
-
154
- -- State constants
155
- local ContinuedAsNew = tonumber (getArgv ())
156
- local Finished = tonumber (getArgv ())
157
-
158
- instance [" state" ] = state
159
-
160
- -- If workflow instance finished, remove active execution
161
- local activeInstanceExecutionKey = getKey ()
162
- if state == ContinuedAsNew or state == Finished then
163
- -- Remove active execution
164
- redis .call (" DEL" , activeInstanceExecutionKey )
165
-
166
- instance [" completed_at" ] = now
167
-
168
- redis .call (" SREM" , activeInstancesKey , instanceSegment )
169
- end
170
-
171
- if lastSequenceId > 0 then
172
- instance [" last_sequence_id" ] = lastSequenceId
173
- end
174
-
175
- redis .call (" SET" , instanceKey , cjson .encode (instance ))
176
-
177
- -- Schedule activities
178
- local activities = tonumber (getArgv ())
179
- local activitySetKey = getKey ()
180
- local activityStreamKey = getKey ()
181
- for i = 1 , activities do
182
- local activityId = getArgv ()
183
- local activityData = getArgv ()
184
-
185
- local added = redis .call (" SADD" , activitySetKey , activityId )
186
- if added == 1 then
187
- redis .call (" XADD" , activityStreamKey , " *" , " id" , activityId , " data" , activityData )
188
- end
189
- end
190
-
191
- -- Remove executed pending events
192
- local lastPendingEventMessageId = getArgv ()
193
- redis .call (" XTRIM" , pendingEventsKey , " MINID" , lastPendingEventMessageId )
194
- redis .call (" XDEL" , pendingEventsKey , lastPendingEventMessageId )
195
-
196
- -- Complete workflow task and unlock instance
196
+ -- Complete workflow task and mark instance task as completed
197
197
local taskId = getArgv ()
198
198
local groupName = getArgv ()
199
199
local task = redis .call (" XRANGE" , workflowStreamKey , taskId , taskId )
200
200
if # task ~= 0 then
201
201
local id = task [1 ][2 ][2 ]
202
202
redis .call (" SREM" , workflowSetKey , id )
203
203
redis .call (" XACK" , workflowStreamKey , groupName , taskId )
204
-
205
204
redis .call (" XDEL" , workflowStreamKey , taskId )
206
205
end
207
206
0 commit comments