Skip to content
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

GitAuto: [FEATURE] Add Saga capability #581

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

gitauto-ai[bot]
Copy link
Contributor

@gitauto-ai gitauto-ai bot commented Oct 23, 2024

Resolves #330

What is the feature

Implement SAGA pattern support in the CommandsConsumer and EventsConsumer classes to handle complex workflows involving multiple commands and events. This enhancement will enable us to manage long-running processes, coordinate processes across multiple events and commands, and implement timeouts effectively.

Why we need the feature

Our current system lacks support for the SAGA pattern, limiting our ability to manage complex workflows and transactions that span multiple events and commands. This limitation leads to challenges in process coordination, maintaining consistency, and managing timeouts. By integrating SAGA capabilities, we can handle long-running transactions and complex workflows more effectively, reducing the risk of inconsistencies and failures, and improving the overall reliability of our system.

How to implement and why

To integrate the SAGA pattern, we will follow these steps:

  1. Create a Generic Saga Handler:

    • Develop an abstract class GenericSagaHandler that inherits from the ISagaHandler interface. This class will serve as the base for SAGA implementations, handling the initiation of a SAGA when an initial event or command is received, similar to how current events and commands trigger handlers.
    • Implementing SAGA classes will inherit from GenericSagaHandler and implement the ISagaData interface to manage SAGA-specific data and state.
  2. Implement Handling Methods:

    • For each StartedBy<T> message type the SAGA handles, implement a corresponding Handle method to process the initiation of the SAGA.
    • For each Handle<T> message type, implement a Handle method to process intermediate messages that influence the SAGA's state.
    • For each HandleTimeout<T>, implement a Timeout method to manage timeout scenarios within the SAGA lifecycle.
  3. Create a Saga Data Class:

    • Define a SAGA data class to store and manage the SAGA's state across multiple messages. This class will implement the ISagaData interface.

    • Example:

      public class SagaData : ISagaData
      {
          public Guid SagaId { get; set; }
          public string State { get; set; }
          public DateTime CreatedAt { get; set; }
          // Additional properties as needed
      }
    • This data class ensures that SAGA state is maintained consistently throughout the process.

  4. Manage Correlation and State:

    • Implement correlation logic to associate incoming events and commands with the appropriate SAGA instance using identifiers like SagaId.
    • Update the SAGA state within each handler method based on the business logic, ensuring consistency and correct progression of the workflow.
  5. Implement Persistence:

    • Develop a persistence mechanism for SAGA data to allow the state to be saved and retrieved across the application's lifetime.

    • The persistence layer can support SQL, NoSQL, or in-memory storage, depending on project requirements.

    • Example:

      public class SagaRepository
      {
          public void Save(SagaData sagaData)
          {
              // Implementation to save sagaData to the chosen storage
          }
      
          public SagaData Load(Guid sagaId)
          {
              // Implementation to load sagaData from storage using sagaId
          }
      }
    • Persisting SAGA data ensures resilience and continuity, especially in distributed systems or in case of failures.

  6. Implement Completion Logic:

    • Define conditions under which the SAGA should be completed, and implement logic to finalize and clean up the SAGA instance accordingly.
    • Proper completion handling prevents resource leaks and ensures that finished SAGA processes do not interfere with ongoing ones.
  7. Integrate Timeout Mechanism:

    • Implement a timeout mechanism to handle cases where a SAGA does not reach completion within an expected timeframe.
    • Define timeout policies and implement HandleTimeout<T> methods to manage expired SAGA processes, including rollback or compensating actions if necessary.
  8. Extend ISaga Interface:

    • Ensure that the SAGA implementations extend from the ISaga interface and utilize the ISagaData implementations for consistent integration with existing infrastructure.
    • This adherence promotes uniformity and makes the SAGA components align with other messaging patterns in the system.
  9. Testing:

    • Conduct thorough testing of the SAGA implementation across various scenarios, including:
      • Normal operation with successful completion.
      • Handling of intermediate failures and retries.
      • Timeout scenarios and proper execution of compensating actions.
    • Testing ensures reliability and robustness of the SAGA integration before deployment.

By following these steps, we can effectively integrate the SAGA pattern into our system, providing enhanced capabilities for managing complex workflows and improving overall system reliability.

About backward compatibility

The introduction of SAGA capabilities is designed to be backward compatible:

  • Non-Intrusive Additions: The new classes (GenericSagaHandler, SagaData, SagaRepository) and interfaces (ISagaHandler, ISagaData) are additions that do not modify existing classes or interfaces.
  • Optional Adoption: Existing command and event handlers remain unchanged and can continue to function without adopting the SAGA pattern.
  • Integration with Existing Infrastructure: By extending existing interfaces and following established patterns, the new SAGA components integrate smoothly without disrupting current functionalities.
  • Testing for Assurance: Comprehensive testing ensures that existing features are not adversely affected by the new additions.

This approach ensures that current implementations remain stable while providing the option to leverage the SAGA pattern for new or updated workflows as needed.

Test these changes locally

git checkout -b gitauto/issue-#330-eb1ceaa8-eb9d-4cc6-ab62-7bf36608b527
git pull origin gitauto/issue-#330-eb1ceaa8-eb9d-4cc6-ab62-7bf36608b527

@github-actions github-actions bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Oct 23, 2024
Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

Sonarcsharp (reported by Codacy) found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

@AppVeyorBot
Copy link

Build CrispyWaffle 8.2.220 failed (commit d1aac282a0 by @gitauto-ai[bot])

@gstraccini gstraccini bot added communications dependencies Pull requests that update a dependency file DI enhancement New feature or request events gitauto GitAuto label to trigger the app in a issue. good first issue Good for newcomers hacktoberfest Participation in the Hacktoberfest event help wanted Extra attention is needed IoC 🎲 database Database-related operations labels Oct 23, 2024
@gstraccini gstraccini bot requested a review from guibranco October 23, 2024 00:50
@gstraccini gstraccini bot added 🚦 awaiting triage Items that are awaiting triage or categorization 🤖 bot Automated processes or integrations labels Oct 23, 2024
@guibranco guibranco removed help wanted Extra attention is needed good first issue Good for newcomers hacktoberfest Participation in the Hacktoberfest event labels Oct 23, 2024
@gstraccini gstraccini bot added good first issue Good for newcomers hacktoberfest Participation in the Hacktoberfest event help wanted Extra attention is needed labels Jan 12, 2025
Copy link

coderabbitai bot commented Jan 12, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@AppVeyorBot
Copy link

@AppVeyorBot
Copy link

Build CrispyWaffle 9.1.285 failed (commit c6e1045a9d by @gstraccini[bot])

@AppVeyorBot
Copy link

Build CrispyWaffle 9.1.299 failed (commit 19c9165cc4 by @gstraccini[bot])

@AppVeyorBot
Copy link

Build CrispyWaffle 9.1.304 failed (commit da874807f7 by @gstraccini[bot])

Copy link
Contributor

Infisical secrets check: ✅ No secrets leaked!

💻 Scan logs
12:20AM INF scanning for exposed secrets...
12:20AM INF 650 commits scanned.
12:20AM INF scan completed in 768ms
12:20AM INF no leaks found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚦 awaiting triage Items that are awaiting triage or categorization 🤖 bot Automated processes or integrations communications 🎲 database Database-related operations dependencies Pull requests that update a dependency file DI enhancement New feature or request events gitauto GitAuto label to trigger the app in a issue. good first issue Good for newcomers hacktoberfest Participation in the Hacktoberfest event help wanted Extra attention is needed IoC size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Add Saga capability
2 participants