Skip to content

Add Default Workflow and Activity Commit Strategy Support (aborted due to force push on main)#7137

Closed
dwoldo wants to merge 0 commit intoelsa-workflows:mainfrom
dwoldo:main
Closed

Add Default Workflow and Activity Commit Strategy Support (aborted due to force push on main)#7137
dwoldo wants to merge 0 commit intoelsa-workflows:mainfrom
dwoldo:main

Conversation

@dwoldo
Copy link

@dwoldo dwoldo commented Dec 7, 2025

Add Default Workflow and Activity Commit Strategy Support

Summary

This PR introduces optional global default commit strategies for both workflows and activities, providing a fallback mechanism when no explicit commit strategy is configured. This feature allows developers to set application-wide defaults while still maintaining the ability to override them at the workflow or activity level.

Addresses #7135 #7135

Problem

Currently, if a workflow or activity doesn't specify a commit strategy, no commits occur during execution (except for the final commit). This requires developers to explicitly configure commit strategies for every workflow and activity, even when they want consistent behavior across their application.

Solution

Implemented two complementary features:

1. Default Workflow Commit Strategy

  • Allows setting a global default commit strategy that applies to all workflows without an explicit strategy
  • Configured via WithDefaultWorkflowCommitStrategy() extension method
  • Does not appear in the commit strategy registry (discrete implementation)
  • Can be overridden by workflow-specific strategies

2. Default Activity Commit Strategy

  • Allows setting a global default commit strategy that applies to all activities without an explicit strategy
  • Configured via WithDefaultActivityCommitStrategy() extension method
  • Falls through to workflow-level strategy resolution if not configured
  • Does not appear in the commit strategy registry (discrete implementation)
  • Can be overridden by activity-specific strategies

Resolution Order

For Activities:

  1. Activity-specific commit strategy (if configured)
  2. Default activity commit strategy (if configured)
  3. Workflow-level commit strategy (if configured)
  4. Default workflow commit strategy (if configured)
  5. No commit (default behavior)

For Workflows:

  1. Workflow-specific commit strategy (if configured)
  2. Default workflow commit strategy (if configured)
  3. No commit (default behavior)

Usage Example

// Configure default strategies
services.AddElsa(elsa => elsa
    .UseWorkflows(workflows => workflows
        // Set default workflow-level strategy
        .WithDefaultWorkflowCommitStrategy(new ActivityExecutedWorkflowStrategy())
        
        // Set default activity-level strategy
        .WithDefaultActivityCommitStrategy(new ExecutedActivityStrategy())
        
        // Standard strategies still available for explicit use
        .UseCommitStrategies(strategies => strategies.AddStandardStrategies())
    )
);

// Workflows/activities without explicit strategies will use the defaults
// Explicit strategies still override the defaults

Key Features

  • Type-safe API: Accepts IWorkflowCommitStrategy and IActivityCommitStrategy instances directly
  • Discrete implementation: Default strategies don't appear in the registry, avoiding confusion in UI/tooling
  • Backward compatible: Existing behavior unchanged when defaults are not configured
  • Fluent API: Clean, chainable configuration methods
  • Full test coverage: 11 integration tests validating all scenarios

Changes

Core Implementation

  • CommitStateOptions.cs: Added DefaultWorkflowCommitStrategy and DefaultActivityCommitStrategy properties
  • CommitStrategiesFeature.cs: Added SetDefaultWorkflowCommitStrategy() and SetDefaultActivityCommitStrategy() configuration methods
  • ModuleExtensions.cs: Added WithDefaultWorkflowCommitStrategy() and WithDefaultActivityCommitStrategy() extension methods
  • DefaultActivitySchedulerMiddleware.cs: Modified to fall back to default workflow strategy
  • DefaultActivityInvokerMiddleware.cs: Modified to implement full resolution chain with default strategies

Testing

  • DefaultWorkflowCommitStrategy/Tests.cs: 5 integration tests covering workflow-level defaults
  • DefaultActivityCommitStrategy/Tests.cs: 6 integration tests covering activity-level defaults
  • All tests include exact commit count validation to ensure runtime behavior matches expectations

Test Coverage

Workflow Default Strategy Tests:

  1. Workflow uses default when no explicit strategy is set (6 commits)
  2. Workflow-specific strategy overrides default (1 commit)
  3. No commits when no default and no explicit strategy (1 commit)
  4. Default strategy not visible in registry
  5. Default strategy with standard strategies doesn't duplicate

Activity Default Strategy Tests:

  1. Activity uses default when no explicit strategy is set (6 commits)
  2. Activity-specific strategy overrides default (4 commits)
  3. No commits when no default and no explicit strategy (1 commit)
  4. Default strategy not visible in registry
  5. Default strategy with standard strategies doesn't duplicate
  6. Default workflow strategy used when no activity default (6 commits)

Breaking Changes

None. This is a purely additive feature that maintains backward compatibility.

Additional Notes

  • Composite activities (like Sequence) trigger commit strategy evaluation when processing child activity completions, which is why some tests show 6 commits instead of 3 (3 WriteLine activities + 3 Sequence composite completions)
  • The implementation ensures that default strategies are stored separately from the registry to maintain a clear separation between explicitly registered strategies and fallback defaults
  • All 11 integration tests pass with precise commit count validation

Documentation

Usage examples and API documentation have been added via XML comments on all new public methods.


This change is Reviewable

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for configuring optional global default commit strategies for both workflows and activities. The feature provides a fallback mechanism when no explicit commit strategy is configured at the workflow or activity level, allowing developers to set application-wide defaults while maintaining override capabilities.

Key changes:

  • Added DefaultWorkflowCommitStrategy and DefaultActivityCommitStrategy properties to CommitStateOptions to store global fallback strategies
  • Modified middleware components (DefaultActivityInvokerMiddleware, DefaultActivitySchedulerMiddleware, BackgroundActivityInvokerMiddleware) to implement fallback logic that checks for default strategies when no explicit strategy is specified
  • Introduced fluent API extension methods (WithDefaultWorkflowCommitStrategy, WithDefaultActivityCommitStrategy) for easy configuration

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
src/modules/Elsa.Workflows.Core/CommitStates/Options/CommitStateOptions.cs Added properties for default workflow and activity commit strategy instances with XML documentation
src/modules/Elsa.Workflows.Core/CommitStates/CommitStrategiesFeature.cs Added SetDefaultWorkflowCommitStrategy and SetDefaultActivityCommitStrategy methods to configure global defaults
src/modules/Elsa.Workflows.Core/CommitStates/Extensions/ModuleExtensions.cs Added fluent extension methods WithDefaultWorkflowCommitStrategy and WithDefaultActivityCommitStrategy for easy configuration
src/modules/Elsa.Workflows.Core/Middleware/Activities/DefaultActivityInvokerMiddleware.cs Modified to check for default activity/workflow commit strategies when no explicit strategy is specified, implementing full resolution chain
src/modules/Elsa.Workflows.Core/Middleware/Workflows/DefaultActivitySchedulerMiddleware.cs Modified to fall back to default workflow commit strategy when no explicit strategy is configured
src/modules/Elsa.Workflows.Runtime/Middleware/Activities/BackgroundActivityInvokerMiddleware.cs Updated constructor to accept IOptions<CommitStateOptions> and pass it to base class
src/modules/Elsa.Workflows.Core/CommitStates/USAGE_EXAMPLE.md Added usage documentation for workflow commit strategies (incomplete - missing activity strategy documentation)
test/integration/Elsa.Workflows.IntegrationTests/Scenarios/DefaultWorkflowCommitStrategy/Tests.cs Added 5 integration tests validating default workflow commit strategy behavior
test/integration/Elsa.Workflows.IntegrationTests/Scenarios/DefaultWorkflowCommitStrategy/SimpleWorkflowWithoutWorkflowCommitStrategy.cs Test workflow without explicit commit strategy
test/integration/Elsa.Workflows.IntegrationTests/Scenarios/DefaultWorkflowCommitStrategy/WorkflowWithExplicitWorkflowCommitStrategy.cs Test workflow with explicit commit strategy to validate override behavior
test/integration/Elsa.Workflows.IntegrationTests/Scenarios/DefaultWorkflowCommitStrategy/CommitTracker.cs Test helper to track commit invocations
test/integration/Elsa.Workflows.IntegrationTests/Scenarios/DefaultActivityCommitStrategy/Tests.cs Added 6 integration tests validating default activity commit strategy behavior and fallback chain
test/integration/Elsa.Workflows.IntegrationTests/Scenarios/DefaultActivityCommitStrategy/SimpleWorkflowWithoutActivityCommitStrategy.cs Test workflow without explicit activity commit strategy
test/integration/Elsa.Workflows.IntegrationTests/Scenarios/DefaultActivityCommitStrategy/WorkflowWithExplicitActivityCommitStrategy.cs Test workflow with explicit activity commit strategy to validate override behavior
test/integration/Elsa.Workflows.IntegrationTests/Scenarios/DefaultActivityCommitStrategy/CommitTracker.cs Test helper to track commit invocations (duplicate implementation)

@dwoldo
Copy link
Author

dwoldo commented Dec 9, 2025

@KnibbsyMan Ready to review.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 3 comments.


/// <summary>
/// Sets the specified activity commit strategy as the global default for all activities that do not specify their own strategy.
/// The strategy will be automatically registered if not already present.
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation states "The strategy will be automatically registered if not already present", but this is incorrect. The default strategy is intentionally NOT added to the registry - it only serves as a fallback. This is verified by the test DefaultStrategyNotInRegistry() which confirms the strategy is not visible in the registry. The documentation should state: "The strategy will not be added to the registry and serves only as a fallback."

Suggested change
/// The strategy will be automatically registered if not already present.
/// The strategy will not be added to the registry and serves only as a fallback.

Copilot uses AI. Check for mistakes.
}

[Fact(DisplayName = "Default workflow strategy is not visible in commit strategy registry")]
public async Task DefaultWorkflowStrategyNotInRegistry()
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test method is declared as async Task but doesn't contain any await statements. It should be changed to public void instead of public async Task for consistency and clarity. Compare with the similar test DefaultStrategyNotInRegistry() in DefaultActivityCommitStrategy/Tests.cs (line 115) which correctly uses public void.

Suggested change
public async Task DefaultWorkflowStrategyNotInRegistry()
public void DefaultWorkflowStrategyNotInRegistry()

Copilot uses AI. Check for mistakes.

/// <summary>
/// Sets the specified workflow commit strategy as the global default for all workflows that do not specify their own strategy.
/// The strategy will be automatically registered if not already present.
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation states "The strategy will be automatically registered if not already present", but this is incorrect. The default strategy is intentionally NOT added to the registry - it only serves as a fallback. This is verified by the test DefaultWorkflowStrategyNotInRegistry() which confirms the strategy is not visible in the registry. The documentation should state: "The strategy will not be added to the registry and serves only as a fallback."

Suggested change
/// The strategy will be automatically registered if not already present.
/// The strategy will not be added to the registry and serves only as a fallback.

Copilot uses AI. Check for mistakes.
@dwoldo dwoldo closed this Dec 16, 2025
@dwoldo dwoldo changed the title Add Default Workflow and Activity Commit Strategy Support Add Default Workflow and Activity Commit Strategy Support (aborted due to force push on main) Dec 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants