Code Monkey home page Code Monkey logo

Comments (4)

glime-ai avatar glime-ai commented on June 20, 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.

from elsa-core.

coolshun avatar coolshun commented on June 20, 2024

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

from elsa-core.

msavencov avatar msavencov commented on June 20, 2024

@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

from elsa-core.

coolshun avatar coolshun commented on June 20, 2024

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

from elsa-core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.