Refactor workflow instance creation and execution logic. (#6586)

Updated `CreateAndRunInstanceAsync` to separate instance creation and execution with locking to handle nested workflow scenarios. Made `RunInstanceAsync` public to facilitate reuse in the distributed workflow client.
This commit is contained in:
Sipke Schoorstra 2025-04-14 11:57:14 +02:00 committed by GitHub
parent d92512f087
commit e5c397d4fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 3 deletions

View file

@ -31,8 +31,27 @@ public class DistributedWorkflowClient(
public async Task<RunWorkflowInstanceResponse> CreateAndRunInstanceAsync(CreateAndRunWorkflowInstanceRequest request, CancellationToken cancellationToken = default)
{
var result = await _localWorkflowClient.CreateAndRunInstanceAsync(request, cancellationToken);
return result;
var createRequest = new CreateWorkflowInstanceRequest
{
Properties = request.Properties,
CorrelationId = request.CorrelationId,
Name = request.Name,
Input = request.Input,
WorkflowDefinitionHandle = request.WorkflowDefinitionHandle,
ParentId = request.ParentId
};
var workflowInstance = await _localWorkflowClient.CreateInstanceInternalAsync(createRequest, cancellationToken);
// We need to lock newly created workflow instances too, because it might dispatch child workflows that attempt to resume the parent workflow.
// For example, when using a DispatchWorkflow activity configured to wait for the dispatched workflow to complete.
return await WithLockAsync(async () => await _localWorkflowClient.RunInstanceAsync(workflowInstance, new()
{
Input = request.Input,
Variables = request.Variables,
Properties = request.Properties,
TriggerActivityId = request.TriggerActivityId,
ActivityHandle = request.ActivityHandle
}, cancellationToken));
}
public async Task CancelAsync(CancellationToken cancellationToken = default)

View file

@ -104,7 +104,7 @@ public class LocalWorkflowClient(
return workflowInstanceManager.ExistsAsync(workflowInstanceId, cancellationToken);
}
private async Task<RunWorkflowInstanceResponse> RunInstanceAsync(WorkflowInstance workflowInstance, RunWorkflowInstanceRequest request, CancellationToken cancellationToken = default)
public async Task<RunWorkflowInstanceResponse> RunInstanceAsync(WorkflowInstance workflowInstance, RunWorkflowInstanceRequest request, CancellationToken cancellationToken = default)
{
var workflowState = workflowInstance.WorkflowState;