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:
parent
d92512f087
commit
e5c397d4fb
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue