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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send a 'ready' message to parent process if api server was spawned #4744

Merged
merged 3 commits into from Mar 14, 2022

Conversation

cannikin
Copy link
Member

@cannikin cannikin commented Mar 14, 2022

This enables zero-downtime restarts for the API server with pm2. See https://pm2.keymetrics.io/docs/usage/signals-clean-restart/#graceful-start

process.send will only exist if the process was spawned from a parent, so we need to check if it is defined (which it is when pm2 is in control). There is no "api server" in serverless environments so this code should have no effect there.

We'll also want to document the fact somewhere that you need to add two things to your ecosystem.config.js for pm2:

{
  name: 'algostake-api',
  script: 'node_modules/.bin/rw',
  args: 'serve api',
  instances: 'max',
  exec_mode: 'cluster',
+ wait_ready: true,
+ listen_timeout: 10000,
},

wait_ready tells it to actually listen for that message. The default timeout is 3 seconds which is not enough for the API server to start. Here it's increased to 10 seconds.

This was tested in Algostake and works like a charm! Previous there was 2-3 seconds of downtime each time the API server was restarted on a deploy. Now it's seamless!

I'd love to include the BAREMETAL deploy option by the v1.0 deadline but I've got a lot of stuff that still needs to get done before then! 馃槵

@dac09
Copy link
Collaborator

dac09 commented Mar 14, 2022

Hey Rob, nice one! I wonder if its worth adding to the other handlers as well?

@cannikin
Copy link
Member Author

Ahh good call, in case you're serving web or both! On it...

@cannikin
Copy link
Member Author

Maybe would be cleaner with just if (process.send)?

@cannikin
Copy link
Member Author

Or even process.send && process.send('ready')??

@dac09
Copy link
Collaborator

dac09 commented Mar 14, 2022

Or even process.send && process.send('ready')??

I was gonna say... but didn't want to be a pedant! I like this better

Maybe even have it in a function?

@cannikin
Copy link
Member Author

Oooo what's a good name? notifyParentReady() perhaps

@dac09
Copy link
Collaborator

dac09 commented Mar 14, 2022

notifyParentReady

Yep sounds good!

Other options: sendProcessReady, sendSignalReady - all of them should be fine!

@cannikin
Copy link
Member Author

Voil脿!

@dac09 dac09 merged commit 197c51c into main Mar 14, 2022
@dac09 dac09 deleted the rc-pm2-server-ready branch March 14, 2022 04:00
@jtoar jtoar added this to the next-release milestone Mar 14, 2022
@thedavidprice thedavidprice modified the milestones: next-release, v0.50.0 Mar 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:feature This PR introduces a new feature topic/api
Projects
No open projects
Status: Archived
Development

Successfully merging this pull request may close these issues.

None yet

4 participants