Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to subscribe to workflow and activity events to get changed status using Elsa 3.0? #5125

Closed
coolshun opened this issue Mar 22, 2024 · 4 comments

Comments

@coolshun
Copy link

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.

Copy link

glime-ai bot commented Mar 22, 2024

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.

@coolshun
Copy link
Author

bu there is no "Subscribe" method in "IEventPublisher". @noxe @petedavis

@msavencov
Copy link

@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

@coolshun
Copy link
Author

@msavencov I have another question, can you help me? #5131 How to create a workflow instance without immediately executing it and execut it later?

@elsa-workflows elsa-workflows locked and limited conversation to collaborators Apr 5, 2024
@sfmskywalker sfmskywalker converted this issue into discussion #5192 Apr 5, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants