Unitech / pm2

Node.js Production Process Manager with a built-in Load Balancer.
https://pm2.keymetrics.io/docs/usage/quick-start/
Other
41.52k stars 2.62k forks source link

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

Closed 4storia closed 8 years ago

4storia commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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

4storia commented 8 years ago

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!

vmarchaud commented 8 years ago

@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 commented 8 years ago

!! 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 commented 8 years ago

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

vmarchaud commented 8 years ago

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

bjrnt commented 8 years ago

@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 commented 8 years ago

@bjrnt I just cleaned the branch and re-maked it with upstream change on development, the new one is https://github.com/Unitech/pm2/pull/2472 PS : should be merge in dev really soon

vmarchaud commented 8 years ago

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 commented 5 years ago

@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 ?