Closed AshishShanker closed 6 years ago
The solution to this problem is not to listen to any of the events mentioned, instead look for a message 'shutdown'. So this works:
process.on('message' async (msg) => { // do whatever cleanup necessary, with await to allow adequate time for the clean up to happen }
I need a mechanism to handle
child_process
events.In the current architecture, the
child_process
that runs my script is spawned bywrapper.js
which has a handler forchild.on('exit',...)
, but affords no opportunity for the application programmer to add additional listeners forexit
or other events.The use case I want to enable is a general clean up that the script would do (e.g. close down db connections, sign-off from streams, etc.) when the Windows Service is Stopped by the user from the Services control.
I tried putting in
process.on(['exit'|'SIGINT'|'SIGTERM'|...], handler_func)
in my script, but none of these events trigger because the globalprocess
object applies to the spawningnode
process, not thechild_process
.Perhaps, it may make sense to expose a
handlers
object in theService
object, so user-definedchild_process
handlers could be passed ontowrapper.js
to wire up with thechild_process
at the time it does afork()
?I know I am asking for an API change, and that needs to be weighed with impact on
node-linux
andnode-mac
as well, but it would be great to know how others have solved the problem of handling user initiated Windows service shutdown.