Hook before server shutdown


Does anyone know of the proper way to add a hook that’s called before a Meteor server shutdown? I need to remove a record from db before a server is shut down from SIGKILL, SIGTERM, uncaught exception on the process, etc. I see in the source that there is a cleanup handler that looks like it can be used for this but I don’t see any public API to hook into it.

Any help is appreciated


I don’t think it’s possible. Maybe Node gives this callback. But when your server crashes it’s simply too late.
So I’d go for a solution to remove any unwanted documents when the server starts up again.


Erm. If your server crashes or is killed from outside the process there’s precious little you can do using server code.

You need to install a watchdog which monitors the server process.


if a process is killed from outside, shouldn’t it get a SIGTERM, SIGKILL, SIGINT, SIQUIT, SIGHUP, etc signal (or the windows equivalents) on the main process? or is there a circumstance when that signal isn’t sent but a process is still killed? I’m not really that familiar with os-level signals


Depends on the signal. In case of SIGKILL, the process has to exit immediately and no questions asked.

Due to not knowing which signal you’re getting (and a crashing process usually doesn’t behave nicely either), it would be pretty much unreliable to put such methods into the server process itself.

That’s the reason why watchdog processes exist in the first place.


okay that makes sense, thanks. I need to keep a list of available servers in a db collection. On startup each app server inserts itself into db. I was hoping to have a hook pre-shutdown to remove that server from db, but I guess I’ll have to have other app servers make a health check request to it before connecting to see if it’s up, and remove it from db if it fails a couple consecutive health checks