elsa-core
elsa-core copied to clipboard
How to subscribe to workflow and activity events to get changed status using Elsa 3.0?
How to subscribe to workflow and activity events to get changed status using Elsa 3.0? I want to get the status in business system.I want to get a example code.
To subscribe to workflow and activity events in Elsa 3.0, you can use the event handling features provided by Elsa to listen for specific events and react accordingly. Elsa 3.0 has a robust event system that allows you to subscribe to various workflow-related events, such as when an activity is executed, when a workflow is started, or when a workflow is halted.
Here is an example code snippet that demonstrates how to subscribe to workflow and activity events:
public class MyWorkflowEventHandler
{
private readonly IEventPublisher _eventPublisher;
public MyWorkflowEventHandler(IEventPublisher eventPublisher)
{
_eventPublisher = eventPublisher;
}
public void Subscribe()
{
_eventPublisher.Subscribe<WorkflowStarted>(OnWorkflowStarted);
_eventPublisher.Subscribe<WorkflowCompleted>(OnWorkflowCompleted);
_eventPublisher.Subscribe<ActivityExecuted>(OnActivityExecuted);
_eventPublisher.Subscribe<ActivityFaulted>(OnActivityFaulted);
}
private void OnWorkflowStarted(WorkflowStarted context)
{
// Handle the workflow started event
Console.WriteLine($"Workflow {context.WorkflowInstanceId} started.");
}
private void OnWorkflowCompleted(WorkflowCompleted context)
{
// Handle the workflow completed event
Console.WriteLine($"Workflow {context.WorkflowInstanceId} completed.");
}
private void OnActivityExecuted(ActivityExecuted context)
{
// Handle the activity executed event
Console.WriteLine($"Activity {context.ActivityId} executed in workflow {context.WorkflowInstanceId}.");
}
private void OnActivityFaulted(ActivityFaulted context)
{
// Handle the activity faulted event
Console.WriteLine($"Activity {context.ActivityId} faulted in workflow {context.WorkflowInstanceId}.");
}
}
In this example, IEventPublisher is used to subscribe to various events. You would need to implement the event handlers (OnWorkflowStarted, OnWorkflowCompleted, OnActivityExecuted, OnActivityFaulted) to perform the actions you want when these events occur.
Please note that the exact implementation details may vary depending on the version of Elsa you are using and the specific requirements of your application. For the most accurate and up-to-date information, you should refer to the official Elsa documentation and examples.
bu there is no "Subscribe" method in "IEventPublisher". @noxe @petedavis
@coolshun I did it by implementing my own event handler
public class WorkflowProgressTracker :
INotificationHandler<WorkflowExecuted>,
INotificationHandler<WorkflowExecuting>,
INotificationHandler<WorkflowFinished>,
INotificationHandler<WorkflowStarted>
{
// implementation omitted
}
then register all your handlers in startup by single call
services.AddNotificationHandlersFrom<Program>();
[!NOTE] Example above works on Elsa Server 3.0.6
The list of available events
Elsa.Alterations.Core.Notifications.AlterationJobCompleted
Elsa.Alterations.Core.Notifications.AlterationPlanSubmitted
Elsa.Workflows.Management.Notifications.WorkflowDefinitionCreated
Elsa.Workflows.Management.Notifications.WorkflowDefinitionDeleted
Elsa.Workflows.Management.Notifications.WorkflowDefinitionDeleting
Elsa.Workflows.Management.Notifications.WorkflowDefinitionPublished
Elsa.Workflows.Management.Notifications.WorkflowDefinitionPublishing
Elsa.Workflows.Management.Notifications.WorkflowDefinitionRetracted
Elsa.Workflows.Management.Notifications.WorkflowDefinitionRetracting
Elsa.Workflows.Management.Notifications.WorkflowDefinitionVersionDeleted
Elsa.Workflows.Management.Notifications.WorkflowDefinitionVersionDeleting
Elsa.Workflows.Management.Notifications.WorkflowDefinitionVersionsDeleted
Elsa.Workflows.Management.Notifications.WorkflowDefinitionVersionsDeleting
Elsa.Workflows.Management.Notifications.WorkflowDefinitionsDeleted
Elsa.Workflows.Management.Notifications.WorkflowDefinitionsDeleting
Elsa.Workflows.Management.Notifications.WorkflowInstanceSaved
Elsa.Workflows.Management.Notifications.WorkflowInstancesDeleted
Elsa.Workflows.Management.Notifications.WorkflowInstancesDeleting
Elsa.Workflows.Notifications.ActivityCancelled
Elsa.Workflows.Notifications.ActivityExecuted
Elsa.Workflows.Notifications.ActivityExecuting
Elsa.Workflows.Notifications.SafeSerializerDeserializing
Elsa.Workflows.Notifications.SafeSerializerSerializing
Elsa.Workflows.Notifications.SerializingWorkflowState
Elsa.Workflows.Notifications.WorkflowExecuted
Elsa.Workflows.Notifications.WorkflowExecuting
Elsa.Workflows.Notifications.WorkflowFinished
Elsa.Workflows.Notifications.WorkflowStarted
Elsa.Workflows.Runtime.Notifications.ActivityExecutionLogUpdated
Elsa.Workflows.Runtime.Notifications.ActivityExecutionRecordDeleted
Elsa.Workflows.Runtime.Notifications.ActivityExecutionRecordUpdated
Elsa.Workflows.Runtime.Notifications.BookmarksDeleted
Elsa.Workflows.Runtime.Notifications.BookmarksDeleting
Elsa.Workflows.Runtime.Notifications.RunTaskRequest
Elsa.Workflows.Runtime.Notifications.WorkflowBookmarksDeleted
Elsa.Workflows.Runtime.Notifications.WorkflowBookmarksIndexed
Elsa.Workflows.Runtime.Notifications.WorkflowBookmarksPersisted
Elsa.Workflows.Runtime.Notifications.WorkflowExecutionLogUpdated
Elsa.Workflows.Runtime.Notifications.WorkflowInboxMessageReceived
Elsa.Workflows.Runtime.Notifications.WorkflowTriggersIndexed
@msavencov I have another question, can you help me? #5131 How to create a workflow instance without immediately executing it and execut it later?