Exit MM2 process gracefully?



  • I may be missing it somewhere, but does the core MM2 process call anything to clean up modules and exit gracefully?

    I was going to add a shutdown function to clean up some child processes started by my module, but was wondering if this functionality is already built into the core node-js app anywhere? This really only comes into play when restarting the MM process (without restarting the server itself).

    What I’m looking for would be something similar to the pseudo-code below:

    process.on('SIGINT', function() {
      modules.forEach( m => { 
        if (typeof m.exit === "function") {
          m.exit()
        }
      });
      setTimeout(function() { process.exit(0); }, 300 );
    );
    

  • Module Developer

    @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.


Log in to reply
 

Looks like your connection to MagicMirror Forum was lost, please wait while we try to reconnect.