0

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?

hownowbrowncow
  • 465
  • 1
  • 5
  • 18
  • there's not enough detail in your question to provide any kind of answer. please edit your question to provide more detail about your scenario, what you need to do and why – Derick Bailey Mar 29 '16 at 21:47
  • @DerickBailey added a bit more detail into exactly what i'm trying to accomplish. – hownowbrowncow Mar 30 '16 at 16:22

1 Answers1

1

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.

Community
  • 1
  • 1
christophetd
  • 3,834
  • 20
  • 33
  • Yeah that's exactly right. Essentially I want to record the current state of operation and then continue on boot. In the linked answer yours as well what is `process`? In what context is this event handler placed? Can it be arbitrarily placed within the codebase? – hownowbrowncow Mar 30 '16 at 16:15
  • I suggest you place it in the very beginning of your script (maybe after all your `require`s), since if an error occurs before the exit handler is registered, it will not be taken into account and you won't be able to record your current state. – christophetd Mar 30 '16 at 16:29