hownowbrowncow
hownowbrowncow

Reputation: 475

node.js shutdown / restart observer

We have a number of tasks in a static queue on our server. When the server shuts down (or restarts) we'd prefer not to lose these tasks and therefore we will stash them in a DB structure. On boot this DB structure will be dumped back into the static queue and processing of these queued tasks will continue.

How is it possible to detect a shut down, halt that shutdown, and then continue the shutdown once the above DB storage function has been executed? from what context should this shutdown observation be made?

Upvotes: 0

Views: 191

Answers (1)

christophetd
christophetd

Reputation: 3874

I'm not sure I understood your question, but if I got it right you want to run some code before your scripts exits to do some kind of cleanup.

You can use process.on(event, handler) to register an exit handler for your script for various events, including exit (the scripts exits), SIGINT (the user Ctrl + Cs the script) and uncaughtException (an exception thrown is not caught). Take a look at this answer.

Upvotes: 1

Related Questions