pm2 icon indicating copy to clipboard operation
pm2 copied to clipboard

Reloading with a new environment and --update-env does not update the environment

Open nitrocode opened this issue 9 years ago • 64 comments

Edit: I didn't realize I had already posted this a few weeks ago https://github.com/Unitech/pm2/issues/3164. @vmarchaud you gave the same solution but the solution doesn't work. :(

What's going wrong?

I have my staging and production environments setup in my ecosystem.config.js. When I run the following, my environment is still stuck on staging instead of production.

pm2 start ecosystem.config.js --env staging
pm2 reload all --env production --update-env

How could we reproduce this issue?

Edit: Here is a cloneable repo to reproduce the issue

  1. Set app to print the process.env.NODE_ENV variable

server.js

console.log(process.env.NODE_ENV);
  1. Setup 2 environments in ecosystem.config.js
module.exports = {
  apps: [
    // First application
    {
      name: 'app',
      script: 'server.js',
      instances: 'max',
      exec_mode: 'cluster',
      error_file: 'err.log',
      out_file: 'app.log',
      merge_logs: false,
      env: {
        NODE_ENV: 'development',
      },
      env_staging: {
        NODE_ENV: 'staging',
      },
      env_production: {
        NODE_ENV: 'production',
      },
    },
  ],
};
  1. Start app in environment 1
./node_modules/.bin/pm2 start ecosystem.config.js --env staging
  1. Reload app in environment 2
./node_modules/.bin/pm2 reload all --env production --update-env
  1. Cat one of the app files
$ cat app-1.log
staging
staging

The second line should say production because the environment was updated.

Workaround

Once the process is already started on env X, to get it to start on env Y

  1. Kill pm2 instances

    ./node_modules/.bin/pm2 stop all
    
  2. Kill pm2 god daemon

    pkill -9 PM2
    
  3. Start pm2 in the correct environment

    ./node_modules/.bin/pm2 reload all --env production --update-env
    

Supporting information

===============================================================================
--- PM2 REPORT (Wed Oct 04 2017 12:42:48 GMT-0400 (EDT)) ----------------------
===============================================================================
--- Daemon -------------------------------------------------
pm2d version         : 2.7.0
node version         : 8.4.0
node path            : /Users/user/projects/app/tmp/../node_modules/.bin/pm2
argv                 : /Users/user/.nvm/versions/node/v8.4.0/bin/node,/Users/user/projects/app/node_modules/pm2/lib/Daemon.js
argv0                : node
user                 : user
uid                  : 692055800
gid                  : 1739585065
uptime               : 1min
===============================================================================
--- CLI ----------------------------------------------------
local pm2            : 2.7.0
node version         : 8.4.0
node path            : /Users/user/.nvm/versions/node/v8.4.0/bin/pm2
argv                 : /Users/user/.nvm/versions/node/v8.4.0/bin/node,/Users/user/.nvm/versions/node/v8.4.0/bin/pm2,report
argv0                : node
user                 : user
uid                  : 692055800
gid                  : 1739585065
===============================================================================
--- System info --------------------------------------------
arch                 : x64
platform             : darwin
type                 : Darwin
cpus                 : Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
cpus nb              : 8
freemem              : 70680576
totalmem             : 17179869184
home                 : /Users/user
===============================================================================
--- PM2 list -----------------------------------------------
┌──────────┬────┬─────────┬───────┬────────┬─────────┬────────┬─────┬───────────┬─────────┬──────────┐
│ App name │ id │ mode    │ pid   │ status │ restart │ uptime │ cpu │ mem       │ user    │ watching │
├──────────┼────┼─────────┼───────┼────────┼─────────┼────────┼─────┼───────────┼─────────┼──────────┤
│ app      │ 0  │ cluster │ 68381 │ online │ 1       │ 32s    │ 0%  │ 32.7 MB   │ user │ disabled │
│ app      │ 1  │ cluster │ 68382 │ online │ 1       │ 32s    │ 0%  │ 32.8 MB   │ user │ disabled │
│ app      │ 2  │ cluster │ 68418 │ online │ 1       │ 29s    │ 0%  │ 33.1 MB   │ user │ disabled │
│ app      │ 3  │ cluster │ 68419 │ online │ 1       │ 29s    │ 0%  │ 33.4 MB   │ user │ disabled │
│ app      │ 4  │ cluster │ 68455 │ online │ 1       │ 26s    │ 0%  │ 34.1 MB   │ user │ disabled │
│ app      │ 5  │ cluster │ 68456 │ online │ 1       │ 26s    │ 0%  │ 33.5 MB   │ user │ disabled │
│ app      │ 6  │ cluster │ 68492 │ online │ 1       │ 22s    │ 0%  │ 33.0 MB   │ user │ disabled │
│ app      │ 7  │ cluster │ 68493 │ online │ 1       │ 22s    │ 0%  │ 33.8 MB   │ user │ disabled │
└──────────┴────┴─────────┴───────┴────────┴─────────┴────────┴─────┴───────────┴─────────┴──────────┘
===============================================================================
--- Daemon logs --------------------------------------------
/Users/user/.pm2/pm2.log last 20 lines:
PM2        | 2017-10-04 12:42:25: Stopping app:app id:_old_4
PM2        | 2017-10-04 12:42:25: Stopping app:app id:_old_5
PM2        | 2017-10-04 12:42:25: App name:app id:_old_4 disconnected
PM2        | 2017-10-04 12:42:25: App [app] with id [_old_4] and pid [68012], exited with code [0] via signal [SIGINT]
PM2        | 2017-10-04 12:42:25: App name:app id:_old_5 disconnected
PM2        | 2017-10-04 12:42:25: App [app] with id [_old_5] and pid [68030], exited with code [0] via signal [SIGINT]
PM2        | 2017-10-04 12:42:25: pid=68012 msg=process killed
PM2        | 2017-10-04 12:42:25: Starting execution sequence in -cluster mode- for app name:app id:6
PM2        | 2017-10-04 12:42:25: pid=68030 msg=process killed
PM2        | 2017-10-04 12:42:25: Starting execution sequence in -cluster mode- for app name:app id:7
PM2        | 2017-10-04 12:42:26: App name:app id:6 online
PM2        | 2017-10-04 12:42:26: App name:app id:7 online
PM2        | 2017-10-04 12:42:29: Stopping app:app id:_old_6
PM2        | 2017-10-04 12:42:29: Stopping app:app id:_old_7
PM2        | 2017-10-04 12:42:29: App name:app id:_old_6 disconnected
PM2        | 2017-10-04 12:42:29: App [app] with id [_old_6] and pid [68048], exited with code [0] via signal [SIGINT]
PM2        | 2017-10-04 12:42:29: App name:app id:_old_7 disconnected
PM2        | 2017-10-04 12:42:29: App [app] with id [_old_7] and pid [68066], exited with code [0] via signal [SIGINT]
PM2        | 2017-10-04 12:42:29: pid=68048 msg=process killed
PM2        | 2017-10-04 12:42:29: pid=68066 msg=process killed

Please copy/paste the above report in your issue on https://github.com/Unitech/pm2/issues

nitrocode avatar Oct 04 '17 14:10 nitrocode

@vmarchaud is this a bug?

nitrocode avatar Oct 06 '17 20:10 nitrocode

I am seeing lots of environment issues as well with this version of PM2. It appears to be ignoring anything in the "env_" fields.

BrandonCopley avatar Oct 10 '17 19:10 BrandonCopley

Please read the docs here, its explained :

You want to restart your process and pick-up changes in your ecosystem.json ? Use pm2 reload ecosystem.json --update-env

vmarchaud avatar Oct 10 '17 20:10 vmarchaud

Thanks!, I found the docs shortly after writing this, but couldn't find my thread. I was not expecting this update it looks like.

-- [image: Giftnix]

Brandon Copley

Founder & CEO

t: 512.784.6060 <(512)%20784-6060>

e: [email protected]

On Tue, Oct 10, 2017 at 3:37 PM, vmarchaud [email protected] wrote:

Closed #3192 https://github.com/Unitech/pm2/issues/3192.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/Unitech/pm2/issues/3192#event-1286895447, or mute the thread https://github.com/notifications/unsubscribe-auth/ABJShhySWz2wqYpkDvPZJuEcQCFRNR9wks5sq9WBgaJpZM4Ptu2k .

BrandonCopley avatar Oct 10 '17 21:10 BrandonCopley

@vmarchaud Im not sure if I understand. If I launch the app in env X and I want to reload the app in env Y, using --env Y --update-env, it does not seem to work. It will maintain env X unless I kill the PM2 god daemon and restart pm2 with the env Y.

Do my reproduction steps make sense?

nitrocode avatar Oct 11 '17 21:10 nitrocode

@vmarchaud On step 4 in my original post, my command is equivalent to the one that you suggested from within the docs. Shouldn't this command reload pm2 in the correct environment with my above ecosystem config?

./node_modules/.bin/pm2 reload all --env production --update-env

The top comment on the PM2 - Process File page in Disqus has a thread of people with the same issue. They suggest this command instead.

./node_modules/.bin/pm2 startOrGracefulReload prod.json --env production --update-env

Edit: works fine now... how strange... Turns out that using reload all was the culprit. Using the below command worked fine.

./node_modules/.bin/pm2 reload ecosystem.config.js --env production --update-env

nitrocode avatar Oct 17 '17 03:10 nitrocode

It seems pm2 startOrReload ecosystem.config.js --update-env not working.

But pm2 restart ... works.

cmal avatar Apr 03 '18 12:04 cmal

@vmarchaud judging by comments and votes it seems to be an issue. Do you mind reopening this ticket?

nitrocode avatar Apr 03 '18 21:04 nitrocode

Any updates on this issue? --update-env still doesn't work. Please re-open the issue.

kerimdzhanov avatar Apr 25 '18 14:04 kerimdzhanov

I will try to make some tests soon. If I understand option --update-env works with restart command but when we use ecosystem.config.js it doesn't update env. Am I right ?

wallet77 avatar Apr 25 '18 14:04 wallet77

Hi! Thanks for a quick reply!

Unfortunately nope, --update-env doesn't work neither with restart nor with reload, nor gracefull..., startOr... and so on...

We're trying to make deployment on our server, the directory structure is something like:

v1/
v2/
v3/
current -> ./v3 (symlink)

In the beginning I thought that the problem is in a symlink, but then I've changed the directory to v2/ directly then run pm2 start ecosystem.config.js and when I'm cding to v3/ and trying to restart pm2 ecosystem, it still starts files from the v2/ directory. Changing any file's contents, changing version in package.json doesn't help.

kerimdzhanov avatar Apr 25 '18 14:04 kerimdzhanov

Yay! I found this article and make it work!

http://pm2.keymetrics.io/docs/tutorials/capistrano-like-deployments

The key difference is that I had to explicitly set cwd option in my ecosystem.config.js to /an/absolute/path/to/current (the symlink).

Thus,

pm2 startOrGracefulReload current/ecosystem.config.js --update-env

works gracefully as expected!

I'm sorry, it was my bad.

Thanks for your attention! You're doing a great job!

kerimdzhanov avatar Apr 25 '18 20:04 kerimdzhanov

we have cwd , but startOrReload not work

cmal avatar Apr 27 '18 10:04 cmal

Try so many but this works pm2 restart 1 --update-env or Better still pm2 reload 1 --update-env Both allow you to pick up the changes on your env ASAP

delino12 avatar Jul 22 '18 12:07 delino12

I can't get any combination of the above suggestions to make --update-env work, even with arbitrarily simple "hello world" examples.

The only way I can get pm2 to pick up on a config change is to delete a specific app process or kill the daemon entirely. I guess I'll just add that to my deploy scripts.

dendrochronology avatar Jul 30 '18 20:07 dendrochronology

@delino12 the restart didn't work for me. I wrote up a quick proof of concept repo here using pm2 3.0.3.

nitrocode avatar Jul 30 '18 21:07 nitrocode

@nitrocode you have to kill the server. addition to that, app will pickup the new changes when ever a server is restarted. Please make sure you restart or reload both the .env and the application files changes.

delino12 avatar Jul 31 '18 07:07 delino12

@delino12 by server, do you mean the pm2 daemon, or a managed node process?

I'm still trying to suss out whether this is a documentation bug, a widespread misunderstanding, or the --update-env flag just straight-up not working as advertised.

dendrochronology avatar Jul 31 '18 12:07 dendrochronology

@delino12 I tried both of these commands and they did not start the app in the intended environment

pm2 reload all --env production --update-env
pm2 restart all --env production --update-env

I tried replacing all with 0 and that also did not work. Can you share the full command you used? Try the repo I posted above if you get a chance.

nitrocode avatar Aug 01 '18 02:08 nitrocode

@nitrocode here is a quick step to handle env process I will recommend you stop all app process first, make sure you stop the process from running, most application process is still running on the old env variables because the process is still running and server has cache the old env variables which will make changes not effective.

pm2 restart all --update-env pm2 restart [appname] --update-env

http://pm2.keymetrics.io/docs/usage/environment

delino12 avatar Aug 15 '18 10:08 delino12