workflow-core icon indicating copy to clipboard operation
workflow-core copied to clipboard

Workflow host Publish Event - Get specific execution pointer for comparing with Step Completed Life Cycle Event

Open Huntk23 opened this issue 4 years ago • 1 comments

Is your feature request related to a problem? Please describe.

I have some workflow steps that should trigger an event/delegate back to a UI to display data that has been processed instead of polling for the data which can get costly. Is there an easy way to do this?

Describe the solution you'd like

Some of my workflow steps need to ping back to my UI to refresh so that data can be seen. I would like a way to know when a specific step has completed from a PublishEvent

Sort of something like this:

public string EventDrivenExecutionPointerId { get; set; }

// Blazor OnInitializedAsync
workflowHost.OnLifeCycleEvent += lifeCycleEvent =>
{
    logger.LogInformation("Life cycle event: {@LifeCycleEvent}", lifeCycleEvent);

    if (lifeCycleEvent is StepCompleted stepCompleted && stepCompleted.ExecutionPointerId == EventDrivenExecutionPointerId)
    {
        // Blazor UI Update
        StateHasChanged();
    }
};

EventDrivenExecutionPointerId = await workflowHost.PublishEvent("MyEvent", "0", "hello"); // wfc.ExecutionPointer.Id (GUID)

Describe alternatives you've considered

  1. Tried passing an Action delegate to my TSettings for IWorkflow<TSettings>
    • Causes NewtonSoft JSON.Net self reference loop error
  2. Using a Scoped DI service
    • Workflow steps do not pickup the scoped configured service that was instantiated on page initialize
  3. Subscribe to WorkflowHost.OnLifeCycleEvent
    • Not sure how to filter for the specific ExecutionPointerId of the step I am waiting for completion

Additional context

  • MSSQL persistence

Huntk23 avatar Apr 04 '22 22:04 Huntk23

I was able to get this to work by polling the specific event execution pointer:

_workflowStepChecker = Task.Run(async () =>
{
    while (true)
    {
        var workflowInstance = await PersistenceProvider.GetWorkflowInstance(_job.WorkflowInstanceId);
        var executionPointer = workflowInstance.ExecutionPointers.Find(x => x.EventName == Constants.JobDataConfigurationUploadEvent);

        if (executionPointer.Status == PointerStatus.Complete) break;

        await Task.Delay(2000);
    }

    _workflowFilesExist = true;

    await InvokeAsync(StateHasChanged);
});

Is there a better way to achieve this?

Huntk23 avatar Apr 05 '22 00:04 Huntk23