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

fix: queue deployment on multiple app from same repo sometimes failed #4554

Open
wants to merge 2 commits into
base: next
Choose a base branch
from

Conversation

onyedikachi-david
Copy link

@onyedikachi-david onyedikachi-david commented Dec 12, 2024

Description

This PR fixes issue #4391 where queue deployment on multiple apps from the same repo sometimes fails.

/claim #4391

Fixes: #4391

Signed-off-by: David Anyatonwu <davidanyatonwu@gmail.com>
@andrasbacsai
Copy link
Member

@coderabbitai review

Copy link

coderabbitai bot commented Feb 11, 2025

📝 Walkthrough

Summary by CodeRabbit

  • New Features

    • Enhanced the deployment process with improved error notifications, validation checks, and clearer status updates.
    • Introduced safer operations through transactional management, ensuring deployments execute only under proper conditions.
  • Bug Fixes

    • Resolved issues that could lead to unexpected errors and race conditions during deployment, resulting in a more reliable release process.

Walkthrough

This pull request refines the deployment process across two areas. In the deployment job, it adds validation checks on critical entities, enhances error handling, introduces database transaction management and refactors container operations, along with improved logging and notifications. In parallel, the helper functions for application deployments are updated to wrap their operations in transactions to ensure atomicity and prevent race conditions during concurrent deployments.

Changes

File(s) Summary
app/Jobs/…/ApplicationDeploymentJob.php Enhanced error handling and validation in the constructor; added transaction management and status verification in handle; refactored container management; introduced initialize_deployment and generate_healthcheck_commands; improved logging and notifications.
bootstrap/helpers/applications.php Wrapped deployment-related functions (queue_application_deployment, force_start_deployment, queue_next_deployment, next_after_cancel, next_queuable) in transactions; added checks for concurrent deployments to prevent race conditions.

Sequence Diagram(s)

sequenceDiagram
    participant J as ApplicationDeploymentJob
    participant DB as Database
    participant C as ContainerManager
    participant N as Notification System

    J->>J: initialize_deployment()
    J->>DB: DB.beginTransaction()
    J->>J: Verify deployment status (IN_PROGRESS)
    alt Valid Status
        J->>C: Execute container management operations
        J->>N: Log actions and dispatch success notification
        J->>DB: DB.commit()
    else Invalid Status
        J->>DB: DB.rollBack()
        J->>N: Log error and dispatch failure notification
    end
Loading
sequenceDiagram
    participant F as Deployment Helper Functions
    participant DB as Database
    participant Q as Deployment Queue

    F->>DB: DB.beginTransaction()
    F->>Q: Lock/update deployment record
    alt Deployment meets criteria
        F->>DB: Commit transaction
    else Issue detected
        F->>DB: Roll back transaction
    end
Loading

Assessment against linked issues

Objective Addressed Explanation
Prevent deployment queue failures on concurrent deployments ([#4391])

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • 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.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (4)
bootstrap/helpers/applications.php (2)

96-124: Inconsistent usage of the deployment status constants.

Here you rely on 'status', 'in_progress' whereas in other places (lines 34, 37, etc.) the enum constant ApplicationDeploymentStatus::IN_PROGRESS->value is used. Consider unifying for clarity.


129-153: Unify 'in_progress' vs. ApplicationDeploymentStatus::IN_PROGRESS->value.

Passing string literals in some subqueries while using the enum in others can lead to confusion. Keeping them consistent prevents accidental mismatches later.

app/Jobs/ApplicationDeploymentJob.php (2)

391-430: $cleanup_attempted logic always set to true.

Because $cleanup_attempted is unconditionally set to true at line 393, the if (! $cleanup_attempted) branch at line 420 is never triggered. Revisit this logic if the intent was to invoke queue_next_deployment($this->application) under certain cleanup failures.


432-478: Possible code duplication in initialize_deployment().

This method reimplements setup steps also present in the constructor. If initialize_deployment() is not explicitly called, consider removing dead code or unifying initialization to follow DRY principles.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 23c4061 and 2af0ed0.

📒 Files selected for processing (2)
  • app/Jobs/ApplicationDeploymentJob.php (5 hunks)
  • bootstrap/helpers/applications.php (1 hunks)
🔇 Additional comments (12)
bootstrap/helpers/applications.php (4)

9-9: Good addition for transaction handling.

No issues here. Using the DB facade is appropriate to support atomic operations and prevent race conditions.


14-72: Potential null reference for $application->destination->server.

While the transaction-based concurrency approach using DB::transaction() and ->lockForUpdate() is solid, consider adding a guard to ensure $application->destination and $application->destination->server are not null to avoid unexpected runtime errors in lines 19–21.


76-90: Force deployment logic is well-isolated.

The transaction ensures concurrency is properly handled when force-starting a deployment. This change prevents race conditions while allowing an override, which looks intentional and correct.


158-184: Good transaction-based flow in next_after_cancel.

Locking the queued deployments for update and verifying concurrency before marking it as in-progress ensures correct coordination of deployments. This is an excellent way to handle race conditions.

app/Jobs/ApplicationDeploymentJob.php (8)

29-29: DB facade import is properly aligned with the new transaction logic.

No issues found. This import supports the transactional approach introduced in the handle method.


176-182: Constructor validation for the deployment queue.

Ensuring the queue record exists up front prevents avoidable downstream failures. A solid defensive approach.


196-203: Explicit check for the application instance.

Verifying that $this->application is valid and adjusting $this->restart_only is a robust way to avoid hidden edge cases.


211-216: Loading and validating the server.

This step preempts potential null references and clarifies that a valid server is mandatory for deployment tasks.


219-222: Destination validation prevents misconfiguration issues.

Throwing an exception for a missing destination helps fail fast in case of bad input or data corruption.


270-360: Transactional concurrency checks for deployment queue.

Wrapping concurrency validations in a transaction effectively ensures only one job can set up or confirm deployment status at once. This is well-structured for preventing race conditions.


365-369: Marking deployment as FINISHED on success.

Officially completing the deployment and firing ApplicationStatusChanged is consistent with a successful termination flow.


370-390: Clean rollback on error.

Catching exceptions, rolling back, and marking the queue status as FAILED properly reflects a failed deployment scenario. The re-throw ensures the job is flagged as failed in the queue system.

@DeVoresyah
Copy link

is there any updates on this ?

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.

[Bug]: Queue deployment on multiple app from same repo sometimes failed
4 participants