Read the statement by Michael Teeuw here.
Exit MM2 process gracefully?
-
@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-MyWinkmodule: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-onlyor 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-onlymode. 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.jsis 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 startandpm2 stop mmwithpm2would 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.jsmight 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 forSIGINTis 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.jsis 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.
