Skip to content

refactor: speed up startup and fixes concurrency in shutdown#1742

Merged
stalniy merged 1 commit intomainfrom
refactor/api-startup
Jul 28, 2025
Merged

refactor: speed up startup and fixes concurrency in shutdown#1742
stalniy merged 1 commit intomainfrom
refactor/api-startup

Conversation

@stalniy
Copy link
Contributor

@stalniy stalniy commented Jul 26, 2025

Why

just improvement

Summary by CodeRabbit

  • Bug Fixes

    • Improved server shutdown process to ensure it executes only once, preventing multiple shutdowns on repeated signals.
    • Enhanced shutdown logic to handle cases where the server is not listening, ensuring proper cleanup.
  • Tests

    • Added and updated tests to verify server shutdown behavior based on the server's listening state and to ensure correct invocation of shutdown callbacks.

@stalniy stalniy requested a review from a team as a code owner July 26, 2025 07:52
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 26, 2025

"""

Walkthrough

The initialization process in the app was updated to run database and feature flag service setups concurrently. The server shutdown logic was refactored to ensure it executes only once, with the shutdown callback made optional and conditional server listening checks added. Tests were added and modified to verify shutdown behaviors for different server states.

Changes

File(s) Change Summary
App Initialization
apps/api/src/app.ts
Modified app initialization to concurrently initialize DB and feature flags; improved shutdown handler with once.
Shutdown Server Logic
apps/api/src/core/services/shutdown-server/shutdown-server.ts
Refactored shutdown logic: removed internal flag, made shutdown callback optional, checked server listening state.
Shutdown Server Tests
apps/api/src/core/services/shutdown-server/shutdown-server.spec.ts
Simplified an existing test and added two new tests for shutdown logic based on server listening state.

Sequence Diagram(s)

sequenceDiagram
    participant App
    participant DB as Database
    participant FF as FeatureFlagsService
    participant Scheduler
    participant Server
    participant Logger

    App->>DB: initDb() (concurrent)
    App->>FF: FeatureFlagsService.initialize() (concurrent)
    App->>Scheduler: scheduler.start()
    App->>Server: Start server

    Note over Server,App: On SIGTERM/SIGINT
    App->>Server: shutdownServer()
    alt Server is listening
        Server->>Server: server.close()
        Server->>Logger: Log shutdown
    else Server is not listening
        Server->>Logger: Log shutdown
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

Suggested reviewers

  • baktun14

Poem

In the warren where servers sleep,
Rabbits code while others leap.
Shutdowns now are handled right—
Only once, not twice a fright!
Features flag and DB race,
All initialized apace.
🐇✨ Review with a happy face!
"""


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between fbfa8fc and 0ccaa4b.

📒 Files selected for processing (3)
  • apps/api/src/app.ts (2 hunks)
  • apps/api/src/core/services/shutdown-server/shutdown-server.spec.ts (2 hunks)
  • apps/api/src/core/services/shutdown-server/shutdown-server.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • apps/api/src/app.ts
  • apps/api/src/core/services/shutdown-server/shutdown-server.spec.ts
  • apps/api/src/core/services/shutdown-server/shutdown-server.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: validate / validate-app
  • GitHub Check: test-build
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch refactor/api-startup

🪧 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @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.

@codecov
Copy link

codecov bot commented Jul 26, 2025

Codecov Report

❌ Patch coverage is 80.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.51%. Comparing base (81e9d42) to head (0ccaa4b).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
apps/api/src/app.ts 33.33% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1742      +/-   ##
==========================================
- Coverage   72.65%   72.51%   -0.14%     
==========================================
  Files         627      622       -5     
  Lines       15023    14845     -178     
  Branches     2637     2624      -13     
==========================================
- Hits        10915    10765     -150     
+ Misses       3781     3753      -28     
  Partials      327      327              
Flag Coverage Δ *Carryforward flag
api 80.27% <80.00%> (+0.01%) ⬆️
deploy-web 58.64% <ø> (ø) Carriedforward from 81e9d42
log-collector ?
notifications 87.25% <ø> (ø) Carriedforward from 81e9d42
provider-console 80.95% <ø> (ø) Carriedforward from 81e9d42
provider-proxy 85.00% <ø> (ø) Carriedforward from 81e9d42

*This pull request uses carry forward flags. Click here to find out more.

Files with missing lines Coverage Δ
...c/core/services/shutdown-server/shutdown-server.ts 100.00% <100.00%> (ø)
apps/api/src/app.ts 87.27% <33.33%> (+0.90%) ⬆️

... and 5 files with indirect coverage changes

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@stalniy stalniy force-pushed the refactor/api-startup branch 2 times, most recently from 95c1e6a to fbfa8fc Compare July 28, 2025 09:51
@stalniy stalniy force-pushed the refactor/api-startup branch from fbfa8fc to 0ccaa4b Compare July 28, 2025 16:10
@stalniy stalniy merged commit c6925e5 into main Jul 28, 2025
62 checks passed
@stalniy stalniy deleted the refactor/api-startup branch July 28, 2025 16:50
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

Comments