Skip to content

add PM2 cluster mode to utilize both CPU cores#65

Merged
Aiiion merged 1 commit into
masterfrom
infra/pm2-cluster
Jun 17, 2026
Merged

add PM2 cluster mode to utilize both CPU cores#65
Aiiion merged 1 commit into
masterfrom
infra/pm2-cluster

Conversation

@Aiiion

@Aiiion Aiiion commented Jun 17, 2026

Copy link
Copy Markdown
Owner

Summary

  • Adds ecosystem.config.cjs to run 2 cluster workers via PM2 (matching the 2 vCPU server)
  • Updates Dockerfile to install PM2 globally and startup.sh to use pm2-runtime (keeps process in foreground for correct Docker signal handling)
  • Gates the purge cron job to the primary worker (NODE_APP_INSTANCE === '0') to prevent duplicate deletes across workers — the flush job is already protected by a Redis distributed lock

Test plan

  • Build the Docker image and confirm container starts with 2 PM2 workers (pm2 list inside container)
  • Confirm docker stop shuts down cleanly (no hanging processes)
  • Confirm rate limiting still works correctly across workers (Redis-backed)
  • Confirm auth flow (login + verify) works correctly across workers
  • At 05:00 UTC, confirm purge job runs exactly once (check logs from only worker 0)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Application now runs with multi-instance clustering for improved reliability and performance.
  • Bug Fixes

    • Prevented duplicate cron job execution across clustered application instances.
  • Chores

    • Updated application startup process to use PM2 process manager for better stability and resource management.

@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The PR adopts PM2 as the process manager: PM2 is installed globally in the Dockerfile, a new ecosystem.config.cjs configures the Express app in cluster mode with two instances, startup.sh switches from npm start to pm2-runtime, and src/cron.mjs gates the daily log-purge cron job to the primary cluster worker only.

Changes

PM2 Cluster Mode Adoption

Layer / File(s) Summary
PM2 installation, ecosystem config, and startup entrypoint
Dockerfile, ecosystem.config.cjs, startup.sh
Dockerfile adds npm install -g pm2; ecosystem.config.cjs exports a two-instance cluster config for express-api running ./src/index.mjs; startup.sh replaces exec npm start with exec pm2-runtime ecosystem.config.cjs.
Cron purge gating to primary PM2 worker
src/cron.mjs
registerCronJobs reads NODE_APP_INSTANCE to set isPrimaryWorker, conditionally creates the daily purge task only on the primary instance, and uses purgeTask?.stop() in the returned stop() handler.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • Aiiion/express-api#40: Introduced the daily log-purge cron job in registerCronJobs that this PR now gates to the primary PM2 cluster worker.

Poem

🐇 Two workers now hum in the cluster's embrace,
But only one rabbit purges logs in that space.
PM2 spins up the show with a nod,
ecosystem.config — a tidy new prod!
Hop hop, no duplicates, all is in place. 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'add PM2 cluster mode to utilize both CPU cores' directly and clearly summarizes the main change—adding PM2 cluster mode support to leverage the server's dual-core capacity.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch infra/pm2-cluster

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
Dockerfile (1)

15-16: Pin PM2 to an explicit version for reproducible and safer builds.

npm install -g pm2 will resolve whatever is latest at build time, so rebuilds can drift unexpectedly. Pinning a tested version avoids silent runtime changes.

Suggested patch
 # Install PM2 globally
-RUN npm install -g pm2
+ARG PM2_VERSION=7.0.1
+RUN npm install -g pm2@${PM2_VERSION}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Dockerfile` around lines 15 - 16, The RUN npm install -g pm2 command installs
the latest version of PM2 at build time, making builds non-reproducible across
different runs. Modify the command to pin PM2 to a specific tested version by
appending `@version-number` to pm2, for example npm install -g pm2@5.x.x, ensuring
consistent builds and preventing unexpected runtime changes from silent version
updates.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@Dockerfile`:
- Around line 15-16: The RUN npm install -g pm2 command installs the latest
version of PM2 at build time, making builds non-reproducible across different
runs. Modify the command to pin PM2 to a specific tested version by appending
`@version-number` to pm2, for example npm install -g pm2@5.x.x, ensuring
consistent builds and preventing unexpected runtime changes from silent version
updates.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 4cea12ac-745d-4e3b-a811-bdfc6e83505f

📥 Commits

Reviewing files that changed from the base of the PR and between fbfef08 and 1361c2f.

📒 Files selected for processing (4)
  • Dockerfile
  • ecosystem.config.cjs
  • src/cron.mjs
  • startup.sh

@Aiiion Aiiion merged commit 5ece575 into master Jun 17, 2026
2 checks passed
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.

1 participant