Read the statement by Michael Teeuw here.
Exit MM2 process gracefully?
-
@shbatm Even if it does run some kind of cleanup routine when it closes, pm2 can’t know everything about what MagicMirror and the modules are doing, so writing a shutdown routine into your module can’t hurt.
I’ve used the PubNub API in a couple of my modules and I run a routine on SIGINT to remove listeners and subscriptions. Pm2’s exit might take care of nullifying these or it might not, but with by writing the exit routine I can be sure these are taken care of.
-
@j-e-f-f Thanks for the quick feedback!
I was assuming I would need the cleanup routine in my module, I was just wondering if there was some routine in the core code that already listened for SIGINT and then called each module’s cleanup function (if there was one)–like it does with each node_helper.js’s start function–that way I would just have to add a function instead of add multiple SIGINT handlers (one in each module).
It’s probably a niche requirement and it’s easy enough to add them for each module that needs one. Just curious if it existed, or was maybe worth adding.
-
I ended up opening an issue related to this on GitHub here: #1056. I was trying to implement the method @j-e-f-f suggested, but when using Electron, it ignores the call to
process.on('SIGINT'...)
.What I am suggesting, in case anyone else developing a module needs this: allow module developers to implement a
stop: function() { }
in theirnode_helper.js
file that performs any cleanup required. When exiting, the MM app will call this function, if it exists, and each module can gracefully exit on it’s own.There is a working example of this here: shbatm/MagicMirror@9935d2f
-
@shbatm what problems did you run in to? I’m using
process.on('SIGINT'...)
in two of my modules and they both get called called on MM exit.Here’s an example from my
MMM-MyWink
module:socketNotificationReceived: function(notification, payload){ var self = this; if (notification === 'MMM-MYWINK-GET-INITIAL-STATUS') { // ... edited for brevity } else { this.config = payload; //log into Wink account and set up subscriptions this.initialize(); //set up some cleanup on application exit process.on('SIGINT', function () { console.log("[MMM-MyWink] Cleaning Up"); self.pubnub.removeListener(self.pnListener); self.pubnub.unsubscribeAll(); process.exit(); }); } } },
Full code here:
https://github.com/jclarke0000/MMM-MyWink/blob/master/node_helper.js -
@j.e.f.f Just curious, are you running MM as
server-only
or with Electron (npm start
)? My problem is I used almost exactly the same snippet you have and it was completely ignored when using Electron; however, it worked fine when usingserver-only
mode. I did some research and found that the Electron browser intercepts the exit commands and needs anapp.on("before-quit"...
command, but I couldn’t override this from a module.I ended up making a tweak to the core and a pull request to work around this:
https://github.com/MichMich/MagicMirror/pull/1058If this gets incorporated then all you’d need in your
node_helper.js
is astop()
function:stop: function() { console.log("[MMM-MyWink] Cleaning Up"); self.pubnub.removeListener(self.pnListener); self.pubnub.unsubscribeAll(); }
-
@shbatm I have that code working with Electron running. I’m running it on the server side in node_helper.js, not in he browser-side main module file. Maybe that’s the difference?
-
@j-e-f-f, very strange. Mine was in the node-helper file as well, called inside the start function and I also tried it in the core js/app.js file with the same result. As I mentioned, it worked fine for server only and I was pulling my hair out trying to get it to work when running MM normally… Ctrl+C when running
npm start
andpm2 stop mm
withpm2
would exit without even writing the console line.Full code that wasn’t working here
Working code based on the change request I described here
Thank you for the help, I’m going to chalk it up to a fluke that I’ve spent too much time investigating and now have a work around. It’ll be nice to have a standard clean-up method incorporated in the main code anyways.
-
@shbatm very strange indeed! The only thing I did differently is I didn’t set up the listener in the
start()
function, but in thesocketNotificationReceived()
routine. Maybe the difference is that I’m setting up the listener after Electron has started. Thestart()
function innode_helper.js
might execute before Electron starts?I like your
stop()
function idea but I worry that it won’t be called in the event of an MM crash, in which case anything that should otherwise have run in thestop()
routine won’t get called. Listening forSIGINT
is a more reliable option in that situation, especially if you have resources that need to be explicitly released. -
@j.e.f.f That could be, I wonder if when Electron starts it somehow removes any SIGINT listeners already created.
With respect to running
stop()
during a MM crash – the main function injs/app.js
is still attaching toprocess.on("SIGINT", ...)
just upstream of the individual node_helper files. If MM crashes I think it should still call the stop() functions unless the node_helper array gets corrupted. An advantage to cycling through each node_helper would be that you don’t have multiple individual SIGINT listeners that are trying to call a process.exit before the other is done.