@@ -52,6 +52,38 @@ for i = 1, executedEvents do
5252 lastSequenceId = tonumber (sequenceId )
5353end
5454
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+
5587-- Remove canceled timers
5688local timersToCancel = tonumber (getArgv ())
5789for i = 1 , timersToCancel do
@@ -80,6 +112,20 @@ for i = 1, timersToSchedule do
80112 storePayload (eventId , payloadData )
81113end
82114
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+
83129-- Send events to other workflow instances
84130local otherWorkflowInstances = tonumber (getArgv ())
85131for i = 1 , otherWorkflowInstances do
@@ -102,7 +148,7 @@ for i = 1, otherWorkflowInstances do
102148 local conflictEventPayloadData = getArgv ()
103149
104150 -- Does the instance exist already?
105- local instanceExists = redis .call (" EXISTS" , targetActiveInstanceExecutionState )
151+ local instanceExists = redis .call (" EXISTS" , targetActiveInstanceExecutionKey )
106152 if instanceExists == 1 then
107153 redis .call (" XADD" , pendingEventsKey , " *" , " event" , conflictEventData )
108154 storePayload (conflictEventId , conflictEventPayloadData )
@@ -147,61 +193,14 @@ for i = 1, otherWorkflowInstances do
147193 end
148194end
149195
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
197197local taskId = getArgv ()
198198local groupName = getArgv ()
199199local task = redis .call (" XRANGE" , workflowStreamKey , taskId , taskId )
200200if # task ~= 0 then
201201 local id = task [1 ][2 ][2 ]
202202 redis .call (" SREM" , workflowSetKey , id )
203203 redis .call (" XACK" , workflowStreamKey , groupName , taskId )
204-
205204 redis .call (" XDEL" , workflowStreamKey , taskId )
206205end
207206
0 commit comments