0
process.on('exit', async () => {
  console.log('updating')
  await campaignHelper.setIsStartedAsFalse()
  console.log('exit')
  process.exit(1)
})

I'm going to hook the process exit event and update database field before exit. updating is being shown at exit. But further actions are not executed.

DB is Mongo

Then this code is in dev mode so I'm using ctrl+c to terminate the process.

tpikachu
  • 4,478
  • 2
  • 17
  • 42
  • Is this [link](https://stackoverflow.com/questions/40574218/how-to-perform-an-async-operation-on-exit) what you are looking for? – Kid Jan 16 '20 at 17:00
  • @O.o - Now that this question has been edited to be about a Ctrl-C shut-down that link won't help. – jfriend00 Jan 16 '20 at 17:18

1 Answers1

0

For Ctrl-C (which you have now added to your question), you can't do what you want to do. node.js does not have that feature.

Since the Ctrl-C is under your control, you could send a command to your server to do a normal shut-down and then it could do your asynchronous work and then call process.exit() rather than you just typing Ctrl-C in the console. This is what many real servers do in production. They have a control port (that is not accessible from the outside world) that you can issue commands to, one of which would be to do a controlled shut-down.

Original answer

(before there was any mention of Ctrl-C being the shut-down initiation)

You can't run asynchronous operations on the exit event (it's too late in the shutdown sequence).

You can run asynchronous operations on the beforeExit event.

But, the beforeExit event is only called if nodejs naturally exits on its own because it's queue of remaining work scheduled is empty (no open sockets, files, timers, etc...). It will not be called if the process exits abnormally (such as an unhandled exception or Ctrl-C) or if process.exit() is called manually.

You can handle the case of manually calling process.exit() by replacing the call to process.exit() in your app with a call to a custom shutdown function that does your housekeeping work and then when that has successfully completed, you then call process.exit().

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Ok. I'm running this code as dev mode and to exit using ctrl+c – tpikachu Jan 16 '20 at 17:05
  • @tpikachu - What does "not working" mean? As I explain above, there is no mechanism to make asynchronous calls during an abnormal shut-down. You did NOT put that in your question that it was a Ctrl-C abort. There is no way to do that. – jfriend00 Jan 16 '20 at 17:05
  • The 'beforeExit' event is not emitted for conditions causing explicit termination, such as calling process.exit() or uncaught exceptions. – Kid Jan 16 '20 at 17:05
  • process.on('beforeExit', async () => { console.log('xxxxx') }) – tpikachu Jan 16 '20 at 17:07
  • @tpikachu - My answer explicitly states that this only works for normal shut-down. Your question did not state you were trying to do this on a Ctrl-C. Nodejs. does not offer the feature you want. – jfriend00 Jan 16 '20 at 17:08
  • @tpikachu - You should edit your question to say that you're trying to do this in the middle of a Ctrl-C. And, then we can answer that node.js does not offer that feature. – jfriend00 Jan 16 '20 at 17:09
  • @tpikachu - No, not me. – jfriend00 Jan 16 '20 at 17:10
  • Check your window for a while. I'll downvote once and you can see who did – tpikachu Jan 16 '20 at 17:11
  • @tpikachu - FYI, since the Ctrl-C is under your control, you could send a command to your server to do a normal shut-down and then it could do your asynchronous work and then call `process.exit()` rather than you just typing Ctrl-C in the console. This is what many real servers do in production. They have a control port that you can issue commands to, one of which would be to do a controlled shut-down. – jfriend00 Jan 16 '20 at 17:13
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206082/discussion-between-tpikachu-and-jfriend00). – tpikachu Jan 16 '20 at 17:13
  • @tpikachu - Did this answer your question? I know it may not be the answer you wanted, but it's how node.js works. – jfriend00 Jan 18 '20 at 05:03
  • @jfriend00 Let me ask you one thing more. If this node backend is running by pm2 process manager and I shutdown this with pm2 stop `id` – tpikachu Jan 18 '20 at 12:57
  • Then can you say this is naturally terminated? – tpikachu Jan 18 '20 at 12:58
  • @tpikachu - I doubt it, but would have to test to be sure. The documentation explicitly says that node.js has to decide on its own to shutdown, that there are no more open resources (files, timers, TCP sockets, etc...). Without that, it won't call the `beforeExit` that enables asynchronous operations. The problem is that all these other methods have ***forced*** a shutdown upon nodejs and it has no way of saying, "but wait while I do some other things first". – jfriend00 Jan 18 '20 at 17:12