elsa-workflows / elsa-core

A .NET workflows library
https://v3.elsaworkflows.io/
MIT License
6.58k stars 1.21k forks source link

[BUG] Timer Trigger not respecting SingletoneStrategy #5308

Open wseegersatdealx opened 7 months ago

wseegersatdealx commented 7 months ago

Description

The timer trigger does not seem to respect the SingletoneStrategy.

Elsa v3.1.3

Steps to Reproduce

Due to a bug with the default scheduler where it doubles on each trigger instance I used the quartz scheduler.

    // ...
    elsa.UseQuartz();
    elsa.UseScheduling(x => x.UseQuartzScheduler());
});

Using the Elsa.Samples.AspNet.Heartbeats project and updating the HeartbeatWorkflow to the following.

public class HeartbeatWorkflow : WorkflowBase
{
    protected override void Build(IWorkflowBuilder builder)
    {
        builder.Root = new Sequence
        {
            Activities =
            {
                new Timer(5.Seconds())
                {
                    CanStartWorkflow = true
                },
                new WriteLine(context => $"Start {context.GetRequiredService<ISystemClock>().UtcNow.TimeOfDay}"),
                new Delay(10.Seconds()),
                new WriteLine(context => $"Finish {context.GetRequiredService<ISystemClock>().UtcNow.TimeOfDay}"),
            }
        };

        builder
            .WithActivationStrategyType<SingletonStrategy>();
    }
}

Expected Behavior

This workflow should print out Start ... then Finish ... in sequence.

Actual Behavior

Every 5 seconds a Start ... and a corresponding Finish ... is printed.

Start 09:13:13.7904393
Start 09:13:18.7184339
Start 09:13:23.7192477
Finish 09:13:23.8538843
Start 09:13:28.7107583
Finish 09:13:28.7340925
Start 09:13:33.7075519
Finish 09:13:33.7464776
Start 09:13:38.7213696
Finish 09:13:38.7266934

Log Output

Include any relevant log output or error messages that might help diagnose the issue.

Troubleshooting Attempts

This does not happen when I trigger the same work from HttpEndpoint trigger.

glime-ai[bot] commented 7 months ago

To address the issue you're experiencing with the SingletonStrategy not being respected when using the Timer activity in Elsa, it's important to ensure that the SingletonStrategy is properly configured and that there are no conflicting configurations that might cause the workflow to behave differently than expected.

Based on the information provided, it seems like you have already configured the SingletonStrategy for your workflow. However, the behavior you're observing suggests that new workflow instances are being created every 5 seconds, which is not the expected behavior for a singleton workflow.

Here are a few troubleshooting steps you can take:

  1. Check for Conflicting Configurations: Ensure that there are no other configurations that might be overriding the SingletonStrategy. This includes checking for any other workflow definitions that might be using the same Timer activity with different activation strategies.

  2. Review Elsa Version: Make sure you are using a version of Elsa that supports the SingletonStrategy as expected. If you are using an older version, consider upgrading to the latest stable release.

  3. Review Quartz Configuration: Since you are using Quartz with Elsa, ensure that the Quartz configuration is correct and that it's not causing any unexpected behavior with the scheduling of the Timer activity.

  4. Check for Known Issues: Review the Elsa GitHub issues and discussions to see if there are any known issues related to the SingletonStrategy and Timer activities. It's possible that you might be encountering a bug that has already been reported.

  5. Create a Minimal Reproducible Example: If possible, create a minimal reproducible example that isolates the issue. This can help in identifying whether the problem is with your specific configuration or with Elsa itself.

  6. Seek Support from the Community: If you're unable to resolve the issue on your own, consider reaching out to the Elsa community for support. You can ask questions on the Elsa GitHub repository, Stack Overflow, or the official Elsa Slack channel.

If you have any log output or error messages, please include them as they can provide additional context that might help diagnose the issue.

If you've already tried some of these steps, please let me know which ones and what the outcomes were, so we can further narrow down the problem.

This comment was generated by Glime.

wseegersatdealx commented 7 months ago

While poking around it seems like IWorkflowActivationStrategy.GetAllowActivationAsync is never reached.

Have I incorrectly configured the workflow?

m03392 commented 7 months ago

Same problem for me:

GetAllowActivationAsync() gets never called.

gribunin commented 5 months ago

IWorkflowActivationStrategy.GetAllowActivationAsync is called only from WorkflowHost.CanStartWorkflowAsync which is not called from anywhere across the codebase. All activities which trigger new workflow, like Event, Timer, etc. just create a new workflow and do not respect the activation strategy. It seems the activation strategy doesn't affect anything at all.

dwoldo commented 2 weeks ago

@sfmskywalker was this resolved in #3559 ?

sfmskywalker commented 2 weeks ago

Looks like it, but later it got broken, which was fixed more recently via https://github.com/elsa-workflows/elsa-core/pull/6076