Azure/durabletask

I don't understand why this is not working

herme063 opened this issue · 1 comments

I am trying to create a work flow:

1- Do Step 1, which is a long running process that runs in the background.
Once finished it will fire an event.
2- If Step 1 is successful, proceed to Step 2 otherwise CleanUp the workflow
3- Step 2 is also a long running process that runs in the background.
Once it finishes. it will fire an event.
4- If Step 2 is successful, we are done otherwise CleanUp the workflow

Naively, I just created an orchestration, and inside run task I have something like this:

try
{
	input.RefStep1 = await context.ScheduleTask<string>(
		Step1.Name, Step1.Version, input);
		
	var step1Completed = await _step1CompletedEvent.Task;
	
	if (step1Completed["Status"] != "success")
	{
		throw new Exception($"Step1 failed: {step1Completed["StatusDescription"]}");
	}
	
	input.RefStep2 = await context.ScheduleTask<string>(
		Step2.Name, Step2.Version, input);
		
	var step2Completed = await _step2CompletedEvent.Task;
	
	if (step2Completed["Status"] != "success")
	{
		throw new Exception($"Step2 failed: {step2Completed["StatusDescription"]}");
	}
}
catch (Exception)
{
	await context.ScheduleTask<string>(
		CleanUp.Name, CleanUp.Version, input);
}

My expectation is that Step1 return right away and execution waits for completed event task.
I have an asynchronous call to CreateOrchestrationInstanceWithRaisedEventAsync() somewhere in the logic of Step1.
The orchestrator RunTask() is called again with null input; and OnEvent() is never called.

I come from prototyping using Elsa Workflow and WorkflowCore libraries, and they do support (sort of) this kind of pattern.
Just to re-iterate the pattern is one of the step/activity will suspend the workflow then later the step/activity will resume it back to execute the next step (on success).

How do I achieve this with Durable framework?

The way your orchestration code is written seems correct. However, I think your use of CreateOrchestrationInstanceWithRaisedEventAsync() to send the event is not correct. Depending on the IOrchestrationService you're using, that might end up overwriting your orchestration instance with a new one (and it sounds like that may be happening in your case).

The correct API to call for raising the event to an existing orchestration is TaskHubClient.RaiseEventAsync(...).