Skip to content

[FEATURE] Add Saga capability #330

Open
@guibranco

Description

@guibranco

Description

To enhance our command/event handling capabilities, we must integrate SAGA pattern support into the CommandsConsumer and EventsConsumer classes.
This will allow us to handle complex workflows involving multiple commands and events, manage long-running processes, and implement timeouts effectively.

Problem Statement

  • Current Issue: Our current setup does not support the SAGA pattern, which limits our ability to manage complex workflows and transactions across multiple events and commands. This can result in difficulties with process coordination, consistency, and timeout management.
    Impact: Without SAGA capabilities, managing long-running transactions and complex workflows becomes challenging, increasing the risk of inconsistencies and failures.

Proposed Solution

  • Integrate SAGA Pattern:
    • Implement SAGA pattern handling to manage multiple commands, events, and timeouts. This includes triggering, continuing, persisting, and completing SAGA processes.

Implementation Steps

  1. Triggering SAGA:

    • Create an abstract class, GenericSagaHandler, that inherits from the ISagaHandler interface to start a SAGA when an initial event or command is received. It should function similarly to how current events and commands trigger handlers.
    • The implementing SAGA class should inherit this class and implement the ISagaData interface.
    • For each StartedBy<T> that this class handles, it should have a Handle method implemented.
    • For each Handle<T> that this class handles, it should have a Handle method implemented.
    • For each HandleTimeout<T> that this class handles should have a Timeout method implemented.
  2. Handling Multiple Messages:

    • The SAGA should handle various types of events or commands or timeouts and maintain its state across these messages.
    • Create a SAGA data class to store and manage the SAGA state:
      public class SagaData : ISagaData
      {
          public Guid SagaId { get; set; }
          public string State { get; set; }
          public DateTime CreatedAt { get; set; }
          // Additional properties as needed
      }
  3. Correlation and State Management:

    • Ensure that SAGA data is correlated across all relevant handlers. This involves maintaining and updating the SAGA state based on incoming events or commands.
  4. Persistence:

    • Implement persistence for SAGA data, supporting SQL, NoSQL, or in-memory storage based on project needs:
      public class SagaRepository
      {
          public void Save(SagaData sagaData) { /* Implementation */ }
          public SagaData Load(Guid sagaId) { /* Implementation */ }
      }
  5. Completion:

    • Implement logic to finalize the SAGA once all required events or commands have been processed successfully. Could you ensure proper cleanup and completion handling?
  6. Timeout Feature:

    • Integrate a timeout mechanism to handle scenarios where a SAGA takes too long. Could you define timeout policies and implement recovery or failure handling for expired SAGA processes?
  7. Extend ISaga and Implement ISagaData:

    • Ensure that the SAGA implementation extends from ISaga and implements ISagaData to hold and manage SAGA details effectively.
  8. Testing:

    • Thoroughly test the SAGA integration to ensure it handles various scenarios, including normal operation, failure recovery, and timeout management. Could you verify that SAGA data is correctly managed and persisted?

Additional Notes

  • Review the SAGA pattern documentation for best practices and detailed implementation guidance.
  • Ensure the new SAGA capabilities are well-documented and integrated with existing command-handling/event-handling processes.
  • Consider performance implications and optimize the SAGA implementation to handle large-scale scenarios effectively.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DIIoCcommunicationsdependenciesPull requests that update a dependency fileenhancementNew feature or requesteventsgitautoGitAuto label to trigger the app in a issue.good first issueGood for newcomershacktoberfestParticipation in the Hacktoberfest eventhelp wantedExtra attention is needed🎲 databaseDatabase-related operations

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions