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

PM2 scale command fails with -1 argument due to option parsing #5932

Open
pearwb1235 opened this issue Dec 25, 2024 · 0 comments
Open

PM2 scale command fails with -1 argument due to option parsing #5932

pearwb1235 opened this issue Dec 25, 2024 · 0 comments

Comments

@pearwb1235
Copy link

What's going wrong?

When using pm2 scale <app_name> -1 to reduce the number of instances for a PM2-managed application, an error occurs because the -1 is being treated as an option, not as a valid argument to decrease the number of instances.

Root Cause

The issue arises because commander.js, which PM2 uses for argument parsing, treats -1 as an option due to the leading hyphen (-), resulting in the error. As seen in commander.js documentation, the hyphen (-) is reserved for option flags, causing unintended behavior when attempting to pass a negative number to scale down instances.

Suggested Improvement

To resolve this, it would be ideal to:

  • Support the -1 argument as a valid input for reducing the number of instances.
  • Consider using a different symbol (such as ~ or another syntax) to indicate a relative scale change, e.g., pm2 scale <app_name> ~1 to decrease by one instance.

How could we reproduce this issue?

  1. Start an application using PM2:
    pm2 start --name <app_name> app.js

  2. Attempt to scale the application down by 1 instance using:
    pm2 scale <app_name> -1

Supporting information

--- PM2 report ----------------------------------------------------------------
Date                 : Wed Dec 25 2024 10:31:22 GMT+0800 (台北標準時間)
===============================================================================
--- Daemon -------------------------------------------------
pm2d version         : 5.4.3
node version         : 22.11.0
node path            : not found
argv                 : C:\Program Files\nodejs\node.exe,C:\Program Files (x86)\nvm\v22.11.0\node_modules\pm2\lib\Daemon.js
argv0                : node
user                 : undefined
uid                  : N/A
gid                  : N/A
uptime               : 33min
===============================================================================
--- CLI ----------------------------------------------------
local pm2            : 5.4.3
node version         : 22.11.0
node path            : not found
argv                 : C:\Program Files\nodejs\node.exe,C:\Program Files\nodejs\node_modules\pm2\bin\pm2,report
argv0                : C:\Program Files\nodejs\node.exe
user                 : user
===============================================================================
--- System info --------------------------------------------
arch                 : x64
platform             : win32
type                 : Windows_NT
cpus                 : 12th Gen Intel(R) Core(TM) i7-1260P
cpus nb              : 16
freemem              : 9051037696
totalmem             : 25358680064
home                 : C:\Users\user

References
commander.js documentation (v2.15.1)
PM2 code that handles scaling

pm2/lib/API.js

Lines 1828 to 1835 in e3a327f

if (typeof(number) === 'string' && number.indexOf('+') >= 0) {
number = parseInt(number, 10);
return addProcs(procs[0], number, end);
}
else if (typeof(number) === 'string' && number.indexOf('-') >= 0) {
number = parseInt(number, 10);
return rmProcs(procs[0], number, end);
}

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

No branches or pull requests

1 participant