@@ -262,12 +262,20 @@ func (e *executor) handleWorkflowTaskStarted(event history.Event, a *history.Wor
262
262
263
263
func (e * executor ) handleActivityScheduled (event history.Event , a * history.ActivityScheduledAttributes ) error {
264
264
c := e .workflowState .RemoveCommandByEventID (event .ScheduleEventID )
265
- if c != nil {
266
- // Ensure the same activity was scheduled again
267
- ca := c .Attr .(* command.ScheduleActivityTaskCommandAttr )
268
- if a .Name != ca .Name {
269
- return fmt .Errorf ("previous workflow execution scheduled different type of activity: %s, %s" , a .Name , ca .Name )
270
- }
265
+
266
+ // Ensure activity
267
+ if c == nil {
268
+ return fmt .Errorf ("previous workflow execution scheduled an activity which could not be found" )
269
+ }
270
+
271
+ if c .Type != command .CommandType_ScheduleActivity {
272
+ return fmt .Errorf ("previous workflow execution scheduled an activity, not: %v" , c .Type )
273
+ }
274
+
275
+ // Ensure the same activity was scheduled again
276
+ ca := c .Attr .(* command.ScheduleActivityTaskCommandAttr )
277
+ if a .Name != ca .Name {
278
+ return fmt .Errorf ("previous workflow execution scheduled different type of activity: %s, %s" , a .Name , ca .Name )
271
279
}
272
280
273
281
return nil
@@ -276,10 +284,9 @@ func (e *executor) handleActivityScheduled(event history.Event, a *history.Activ
276
284
func (e * executor ) handleActivityCompleted (event history.Event , a * history.ActivityCompletedAttributes ) error {
277
285
f , ok := e .workflowState .FutureByScheduleEventID (event .ScheduleEventID )
278
286
if ! ok {
279
- return nil
287
+ return fmt . Errorf ( "could not find pending future for activity completion" )
280
288
}
281
289
282
- e .workflowState .RemoveCommandByEventID (event .ScheduleEventID )
283
290
err := f (a .Result , nil )
284
291
if err != nil {
285
292
return fmt .Errorf ("setting result: %w" , err )
@@ -291,11 +298,9 @@ func (e *executor) handleActivityCompleted(event history.Event, a *history.Activ
291
298
func (e * executor ) handleActivityFailed (event history.Event , a * history.ActivityFailedAttributes ) error {
292
299
f , ok := e .workflowState .FutureByScheduleEventID (event .ScheduleEventID )
293
300
if ! ok {
294
- return errors .New ("no pending future found for activity failed event" )
301
+ return errors .New ("no pending future for activity failed event" )
295
302
}
296
303
297
- e .workflowState .RemoveCommandByEventID (event .ScheduleEventID )
298
-
299
304
if err := f (nil , errors .New (a .Reason )); err != nil {
300
305
return fmt .Errorf ("setting result: %w" , err )
301
306
}
@@ -304,7 +309,14 @@ func (e *executor) handleActivityFailed(event history.Event, a *history.Activity
304
309
}
305
310
306
311
func (e * executor ) handleTimerScheduled (event history.Event , a * history.TimerScheduledAttributes ) error {
307
- e .workflowState .RemoveCommandByEventID (event .ScheduleEventID )
312
+ c := e .workflowState .RemoveCommandByEventID (event .ScheduleEventID )
313
+ if c == nil {
314
+ return fmt .Errorf ("previous workflow execution scheduled a timer" )
315
+ }
316
+
317
+ if c .Type != command .CommandType_ScheduleTimer {
318
+ return fmt .Errorf ("previous workflow execution scheduled a timer, not: %v" , c .Type )
319
+ }
308
320
309
321
return nil
310
322
}
@@ -316,8 +328,6 @@ func (e *executor) handleTimerFired(event history.Event, a *history.TimerFiredAt
316
328
return nil
317
329
}
318
330
319
- e .workflowState .RemoveCommandByEventID (event .ScheduleEventID )
320
-
321
331
if err := f (nil , nil ); err != nil {
322
332
return fmt .Errorf ("setting result: %w" , err )
323
333
}
@@ -332,8 +342,6 @@ func (e *executor) handleTimerCanceled(event history.Event, a *history.TimerCanc
332
342
return nil
333
343
}
334
344
335
- e .workflowState .RemoveCommandByEventID (event .ScheduleEventID )
336
-
337
345
if err := f (nil , nil ); err != nil {
338
346
return fmt .Errorf ("setting result: %w" , err )
339
347
}
@@ -343,27 +351,35 @@ func (e *executor) handleTimerCanceled(event history.Event, a *history.TimerCanc
343
351
344
352
func (e * executor ) handleSubWorkflowScheduled (event history.Event , a * history.SubWorkflowScheduledAttributes ) error {
345
353
c := e .workflowState .RemoveCommandByEventID (event .ScheduleEventID )
346
- if c != nil {
347
- ca := c .Attr .(* command.ScheduleSubWorkflowCommandAttr )
348
- if a .Name != ca .Name {
349
- return errors .New ("previous workflow execution scheduled a different sub workflow" )
350
- }
354
+ if c == nil {
355
+ return fmt .Errorf ("previous workflow execution scheduled a sub workflow" )
356
+ }
357
+
358
+ if c .Type != command .CommandType_ScheduleSubWorkflow {
359
+ return fmt .Errorf ("previous workflow execution scheduled a sub workflow, not: %v" , c .Type )
360
+ }
351
361
352
- // Set correct InstanceID here.
353
- // TODO: see if we can provide better support for commands here and find a better place to store
354
- // this message.
355
- ca .Instance = a .SubWorkflowInstance
362
+ ca := c .Attr .(* command.ScheduleSubWorkflowCommandAttr )
363
+ if a .Name != ca .Name {
364
+ return fmt .Errorf ("previous workflow execution scheduled different type of sub workflow: %s, %s" , a .Name , ca .Name )
356
365
}
357
366
358
- // TOOD: If the command cannot be found, raise error?
367
+ // Set correct InstanceID here.
368
+ // TODO: see if we can provide better support for commands here and find a better place to store
369
+ // this message.
370
+ ca .Instance = a .SubWorkflowInstance
359
371
360
372
return nil
361
373
}
362
374
363
375
func (e * executor ) handleSubWorkflowCancellationRequest (event history.Event , a * history.SubWorkflowCancellationRequestedAttributes ) error {
364
376
c := e .workflowState .RemoveCommandByEventID (event .ScheduleEventID )
365
- if c != nil {
366
- return fmt .Errorf ("expected to find a sub workflow cancellation event, instead got: %v" , event .Type )
377
+ if c == nil {
378
+ return fmt .Errorf ("previous workflow execution cancelled a sub-workflow execution" )
379
+ }
380
+
381
+ if c .Type != command .CommandType_CancelSubWorkflow {
382
+ return fmt .Errorf ("previous workflow execution cancelled a sub-workflow execution, not: %v" , c .Type )
367
383
}
368
384
369
385
return e .workflow .Continue (e .workflowCtx )
@@ -375,8 +391,6 @@ func (e *executor) handleSubWorkflowFailed(event history.Event, a *history.SubWo
375
391
return errors .New ("no pending future found for sub workflow failed event" )
376
392
}
377
393
378
- e .workflowState .RemoveCommandByEventID (event .ScheduleEventID )
379
-
380
394
if err := f (nil , errors .New (a .Error )); err != nil {
381
395
return fmt .Errorf ("setting result: %w" , err )
382
396
}
@@ -390,8 +404,6 @@ func (e *executor) handleSubWorkflowCompleted(event history.Event, a *history.Su
390
404
return errors .New ("no pending future found for sub workflow completed event" )
391
405
}
392
406
393
- e .workflowState .RemoveCommandByEventID (event .ScheduleEventID )
394
-
395
407
if err := f (a .Result , nil ); err != nil {
396
408
return fmt .Errorf ("setting result: %w" , err )
397
409
}
@@ -403,8 +415,6 @@ func (e *executor) handleSignalReceived(event history.Event, a *history.SignalRe
403
415
// Send signal to workflow channel
404
416
workflowstate .ReceiveSignal (e .workflowCtx , e .workflowState , a .Name , a .Arg )
405
417
406
- e .workflowState .RemoveCommandByEventID (event .ScheduleEventID )
407
-
408
418
return e .workflow .Continue (e .workflowCtx )
409
419
}
410
420
@@ -441,7 +451,7 @@ func (e *executor) processCommands(ctx context.Context, t *task.Workflow) (bool,
441
451
c .State = command .CommandState_Committed
442
452
443
453
switch c .Type {
444
- case command .CommandType_ScheduleActivityTask :
454
+ case command .CommandType_ScheduleActivity :
445
455
a := c .Attr .(* command.ScheduleActivityTaskCommandAttr )
446
456
447
457
scheduleActivityEvent := e .createNewEvent (
0 commit comments