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 restart "services.json" doesn't update environment variables #528

Closed
gillesdemey opened this issue Jun 19, 2014 · 47 comments
Closed

PM2 restart "services.json" doesn't update environment variables #528

gillesdemey opened this issue Jun 19, 2014 · 47 comments

Comments

@gillesdemey
Copy link

We're using a JSON file to define all our node services and their appropriate environment variables, script starting points etc.

When adding an environment variable for one of our services and issuing pm2 restart services.json it doesn't actually add the environment variable to the process (tested using pm2 dump).

Deleting all the services and adding them again does work though but takes down our API's for a while.

@soyuka
Copy link
Collaborator

soyuka commented Jun 22, 2014

On restart pm2 will not reload services.json variables.
I don't know if a fix in cluster mode is possible, needs investigations.

@dandv
Copy link
Contributor

dandv commented Jul 15, 2014

+1 - exporting the regular to environment variables (via export VAR=...) didn't seem to work either, so I've switched my app to use processes.json. Now I find out this doesn't work either? :)

@Unitech
Copy link
Owner

Unitech commented Jul 15, 2014

I would prefer to preserve environment on restart without changes. If you want to change the value of an ENV var, it's better to pm2 delete my-app; pm2 start my-app.js

@dandv
Copy link
Contributor

dandv commented Jul 15, 2014

Our use case is continuous deployment. One of the environment variables is the hash of the last GitHub commit, which becomes available on the client so we can tell which version it's running. This variable would change with every restart. pm2 delete followed by pm2 start is not 0s downtime reload.

@rlidwka
Copy link
Collaborator

rlidwka commented Jul 16, 2014

I would prefer to preserve environment on restart without changes.

Is it possible to merge the environment somehow? So you can do VAR1=123 pm2 start xxx and VAR2=456 pm2 restart xxx, and both variables will be present? I know it's very tricky, but it could solve this.

@soyuka
Copy link
Collaborator

soyuka commented Jul 16, 2014

If I need to change variables in an application I usually use a configuration file not ENV variables so I really don't know why they would need to be changed once the application has been started. Of course, that's only a point of view.

@dandv
Copy link
Contributor

dandv commented Jul 16, 2014

@soyuka, I agree with you, but Meteor forces us to use environment variables instead of config files. Maybe you could mention in that ticket your thoughts on this?

@rlidwka
Copy link
Collaborator

rlidwka commented Jul 16, 2014

That environment variable looks really weird.

But it is a valid use-case, and if some applications use it, pm2 should probably support it.

@Unitech Unitech added 0.9.5 and removed bug labels Jul 29, 2014
Unitech added a commit that referenced this issue Aug 21, 2014
@Unitech Unitech removed the 0.9.5 label Aug 22, 2014
@Unitech
Copy link
Owner

Unitech commented Aug 22, 2014

The 0.10.0 fixing this issue has been published:

$ npm install pm2@latest -g

Now environment variable are refreshed on restart (via CLI and via JSON declaration)

@Unitech Unitech closed this as completed Aug 22, 2014
@nickpoorman
Copy link

Does this work for startOrRestart?

@rodrigok
Copy link

Don't work for me using pm2 0.10.8

@rodrigok
Copy link

Sorry, I was using

pm2 restart appName

and not

pm2 restart configFile.json

Everything is working now.

Thanks.

@rodrigok
Copy link

But startOrRestart really doesn't work.

@mykwillis
Copy link

I'm trying to figure out how one is supposed to refresh an environment variable for a cluster that is doing 0 downtime reloads using:

pm2 reload myapp.json

It seems that the refresh of environment was only implemented with pm2 restart, but that doesn't give 0 downtime.

Is it intentional that changes to the .JSON file are not picked up by PM2 when a reload is invoked? pm2 delete and pm2 start seems a long way from zero downtime.

@ericherdzik-zz
Copy link

+1 @mykwillis Would like to have a way to gracefully load a new environment configuration.

@Evanlec
Copy link

Evanlec commented Nov 25, 2015

+1 -- this would be very useful -- found that pm2 restart didn't even work -- I had to do pm2 delete; pm2 start xxx.json

@StyleT
Copy link

StyleT commented Dec 8, 2015

+1 without this feature I can't perform zero downtime deploy

@CJMcGregor
Copy link

+1 This is a much needed feature to have reload / gracefulReload refresh env vars.

@Unitech
Copy link
Owner

Unitech commented Dec 17, 2015

It's not the case for reload / gracefulReload, but this feature is available for restarts with PM2 1.0:

You can try it:

$ npm install Unitech/pm2#development -g

You can also restart with different pre-defined environment variables via

$ pm2 restart services.json --env production

@mhchaudhry
Copy link

Also agree. Would definitely like the ability to update env with zero downtime. i.e. reload
e.g.
pm2 reload app.json --env debug (sets NODE_DEBUG, DEBUG vars)
pm2 reload app.json --env nodebug (turns debugging off)

@gflandre
Copy link

+1

1 similar comment
@ThomasFlorelli
Copy link

+1

@soyuka
Copy link
Collaborator

soyuka commented Mar 25, 2016

@gflandre @Florelli did you try the development version?

@gflandre
Copy link

@soyuka no I haven't yet (our issue is with production use, so it requires us to use a stable version).
Does it allow zero-downtime reloads w/ env update? I'll give it a go on my dev env anyway :)

@Booom3
Copy link

Booom3 commented Sep 8, 2016

I've ran into this problem too, I need it to reload the environment variables. I couldn't get it to do it until I deleted the process itself from PM2 and started it as if it was new.

@sameetn
Copy link

sameetn commented Dec 6, 2016

Any update on this? We have th same issue with 2.1.3 although we do not use the json file.

@AuspeXeu
Copy link

AuspeXeu commented Jan 8, 2017

Same issue ...

@vmarchaud
Copy link
Contributor

See --update-env docs here.

@AuspeXeu
Copy link

AuspeXeu commented Jan 9, 2017

@vmarchaud seems not to affect log file configuration changes though...

@vmarchaud
Copy link
Contributor

Yeah some options aren't updated, we are thinking about changing this behavior but it has some pro and cons.

@Zing22
Copy link

Zing22 commented Mar 20, 2017

+1 restart and reload will not reload ignore_watch configuration, which took me two hours to fix my bug.

@vmarchaud
Copy link
Contributor

Reminder : You need to pass the ecosystem.json path if you started the application with it, pm2 doesnt save its location so PM2 can't pickup change by using pm2 restart applicationName

@Zing22
Copy link

Zing22 commented Mar 21, 2017

@vmarchaud the bash history shows that I used pm2 restart ecosystem.json all the time, and I tried again just a moment ago, the output was:

# pm2 restart ecosystem.json
[PM2] Applying action restartProcessId on app [UEMINA](ids: 0)
[PM2] [UEMINA](0) ✓

and the change of ignore_watch didn't affect. But if I turn watch from true to false, it could work.
My pm2 version is 2.0.18, on CentOS 7.

@vmarchaud
Copy link
Contributor

@Zing22 You should update, behavior have changed around 2.x if i correctly recall

@Zing22
Copy link

Zing22 commented Mar 21, 2017

@vmarchaud Problem still there, BTW, the exec mode is fork.
Is there a command like pm2 show 0 to display ignore_watch list?

anyway, thanks for your time : )

@vmarchaud
Copy link
Contributor

@Zing22 You can dump the process data using pm2 prettylist and see the options

@arcreative
Copy link

arcreative commented Nov 9, 2017

Ok so maybe I'm taking crazy pills or something, but I keep having persistent problems with ENV vars and code/assets just downright not updating when I do a pm2 restart either from the server or Capistrano deployment. It's maddening. If I ever make a meaningful change, I have to do cap production pm2:stop && cap production pm2:delete || cap production pm2:start, and now it's to the point where I don't ever trust that restart is going to do what I need it to. It just seems horribly unreliable to me and it's happened on multiple projects that are in no way copied or related to one another. Am I just fundamentally misunderstanding what is supposed to be happening here?

On a prior project, I had some static web assets being served from an express server, and said assets would just refuse to update unless I first deleted the pm2 process and restarted it. There's no mechanism for doing this even intentionally that I'm aware of, and it caused many hours of frustration over many weeks, and ultimately required writing a simple bash script to stop/delete/start the process on deployment because it just wasn't working out of the box.

More context: I'm using .env files (via node-dotenv module), and npm shrinkwrap. shrinkwrap changes were also incidentally ignored on last deploy--when I finally stopped and deleted the pm2 process, the next run exhibited a require error that was an issue on my UAT deployment but NOT production, which is telling me that pm2 is "caching" something I don't want it to.

All other process managers I've worked with do a CLEAN RESTART from scratch when you restart them. There should be no state left on the process via PM2 or any other portion of the stack.

If I can't get this figured out in a sensible way I'm going to have to drop PM2 entirely for my sanity's sake :-)

@dendrochronology
Copy link

Although this issue was closed in 2014, I still see weird behavior around restarts, even with tiny example projects. The only consistent solution I have is to run pm2 kill to unload the daemon entirely.

@nebular
Copy link

nebular commented Sep 11, 2020

please fix this broken, complex and useless functionality. Env Vars are Env Vars, not JSON, not YML, not ecosystems, not anything different to a EXPORT VAR=VALUE and process.env returning the correct value.

@Norbz
Copy link

Norbz commented Apr 28, 2021

This is indeed not fix, and actually I find it more and more unreliable with the latest version

Before, I could just pm2 update, now I often need to kill and reload everything, making no downtime reload impossible...
I don't get why such and important yet probably easy to fix issue can go on for 7 years now...

@arcreative
Copy link

Seems to me that the only salient plan of action at this point is to stop using this solution in its entirety. Definitely had some potential and great things going for it, but the maintainers don't seem to be taking this seriously enough--at least for my production needs.

@LuckyArdhika
Copy link

Okay, this problem come in 2022

@Ozak93
Copy link

Ozak93 commented Jul 25, 2023

Still a problem in 2023.

@Ozak93
Copy link

Ozak93 commented Jul 25, 2023

Seems to me that the only salient plan of action at this point is to stop using this solution in its entirety. Definitely had some potential and great things going for it, but the maintainers don't seem to be taking this seriously enough--at least for my production needs.

What alternatives is there? i'd love to know what you use now

@arcreative
Copy link

arcreative commented Jul 25, 2023

What alternatives is there? i'd love to know what you use now

Kubernetes 😂

@Ozak93
Copy link

Ozak93 commented Jul 25, 2023 via email

@arcreative
Copy link

@Ozak93 absolutely not. While it's definitely not analogous to PM2, having pods for each process with health checks was functionally the same for us. It's a direction we wanted to head anyway, definitely a massive time investment, but you get to feel like a sorcerer when you're done 😆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests