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

QUESTION: Tell pm2 when server is actually ready to handle requests #2353

Closed
4storia opened this issue Aug 22, 2016 · 12 comments
Closed

QUESTION: Tell pm2 when server is actually ready to handle requests #2353

4storia opened this issue Aug 22, 2016 · 12 comments

Comments

@4storia
Copy link

4storia commented Aug 22, 2016

I've looked around the documentation, but I didn't really see any references to this, so my apologies if this is covered somewhere already.

I have a koa app that mounts many sub-apps before coming online. Because it has to require many (around 15) sub-apps, it takes 5-10 seconds to start/restart fully, aka to get to the point where it is actually ready to accept requests. It doesn't seem like pm2 picks up on this fact (not that I would expect it to), and when I do pm2 reload, there is a period of time where requests fail, as the application has not yet started listening for requests. Is there a way for me to tell pm2, from my application, that a process is actually "online"? Or is there a suggested workflow for reloading the application (which I'm running in cluster mode) so that my users don't experience downtime during deployments?

@Unitech
Copy link
Owner

Unitech commented Aug 23, 2016

You may want to call the .listen function once all your sub applications are ready. PM2 recognize an application as ready once the app calls .listen.

You may also want to override the kill timeout:
http://pm2.keymetrics.io/docs/usage/signals-clean-restart/#customize-exit-delay

Let me know if it helps,

@4storia
Copy link
Author

4storia commented Aug 23, 2016

So i'm already doing the following:

var app = koa();
... (requiring many sub-apps)
console.log("calling listen")
server = app.listen(port);
....
module.exports = app;

While tailing all pm2 logs, I see:

2016-08-23-13:32:26 PM2 Starting execution sequence in -fork mode- for app name:app id:0
2016-08-23-13:32:26 PM2 App name:app id:0 online
...
(several console.log statements about mounting sub-apps)
...
2016-08-23-13:32:32 app-0 calling listen
2016-08-23-13:32:32 app-0 Server is running.  { address: '::', family: 'IPv6', port: 8080 }

As you can see, pm2 says my application is "online" long before app.listen is actually executed. The above output is from operating without cluster mode for the sake of debugging, but the outcome is the same with cluster mode enabled.
I'm not particularly interested with the exit of the application (at least not at the moment), because my problem is really that pm2 lists my application as "online" as soon as app.js begins execution, rather than when app.listen is called.

I couldn't seem to find where pm2 specifically listens for app.listen, could you point me to the general area of code where this happens?

@vmarchaud
Copy link
Contributor

vmarchaud commented Aug 25, 2016

@4storia This behavior is actually only present on cluster mode. @Unitech can we add it to the fork mode without problem ?

@4storia
Copy link
Author

4storia commented Aug 25, 2016

Ah okay, I see. It seems a little misleading that pm2 reports the app as "online" as soon as it begins execution. I understand that pm2 doesn't just run web servers, but it would be great if I could let my app signal to pm2 when it was online, if for no other reason than to have the status from pm2 status report more accurately. I was able to get things to work with a combination of overriding the kill timeout like @Unitech suggested, and also adding some SIGINT handling to my app. Thanks for all the help everyone!

@4storia 4storia closed this as completed Aug 25, 2016
@vmarchaud
Copy link
Contributor

@4storia Little update on this, i've done this the past day on this branch, will merge to development soon, you just need to pass --wait-ready when you start the app, add do a process.send('ready'); to tell pm2 that the app is ready.

@4storia
Copy link
Author

4storia commented Aug 29, 2016

!! Oh man, thats super awesome! Thanks for looking into that, I really appreciate it. I'll be eagerly awaiting that pr getting merged :D

@4storia
Copy link
Author

4storia commented Sep 2, 2016

quick question - any update on when that branch will get merged back to development?

@vmarchaud
Copy link
Contributor

@Unitech gonna review it when he's gonna come back from JSConfChina in the next days

@bjrnt
Copy link

bjrnt commented Oct 27, 2016

@vmarchaud How come the PR related to this issue was closed? I think a couple of people, me included, were waiting for this to get merged so we can start using it.

@vmarchaud
Copy link
Contributor

vmarchaud commented Oct 27, 2016

@bjrnt I just cleaned the branch and re-maked it with upstream change on development, the new one is #2472
PS : should be merge in dev really soon

@vmarchaud
Copy link
Contributor

Landed in dev branch of pm2 : npm install -g pm2@next && pm2 update
I'll close since its merged but don't hesitate to make feedback or report bugs.

@yinrong
Copy link

yinrong commented Dec 13, 2018

@4storia Little update on this, i've done this the past day on this branch, will merge to development soon, you just need to pass --wait-ready when you start the app, add do a process.send('ready'); to tell pm2 that the app is ready.

@4storia Hi, I'm using pm2 to start java/python/binary, I need to implement the equivalent "process.send" in these languages. Is there any easy way to do this ?

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

5 participants