mjwrazor mjwrazor - 1 month ago 11
Node.js Question

Is there a way to shutdown Nodejs and restart my app from scratch?

The app needs to completely die so I don't end up with multiple node processes running. I need to kill node but I also need to start up a new node. Npm start would be great because it calls gulp and rebundles the project.

Is there a simple way to do this. Right now I have a route that calls process.exit() but it returns

[nodemon] clean exit - waiting for changes before restart

EDIT

What I mean from scratch is to git pull, re-bundle and then start node again.

EDIT 2



@Paul .. Well actually I have an API I call from a specific page that uses simple-git and pulls from the master branch. This will only send a message to the user there is an update. And another route I have is process.exit(0). Right now i have it set up in a singular cluster so one master one worker. This doesn't seem to work.

I am a little confused on how I would use the detached spawn process. I need to run gulp, which will bundle everything and then start app.js. However with my singular cluster it seems to get a net:: ERR_EMPTY_RESPONSE every so often which is not acceptable. This is just something I have never dealt with before and haven't seen anyone else do this.

Edit 3.



I ended up taking out the cluster code and running it and it seems to be working were it rebundles. Although no node restart. I don't believe we need a node restart but I will definitely look into how to do that still.

Answer

I ended up solving the issue with the help of @Paul who had some pointers that lead me in the right direction.

What we ended up doing.

in app.js > the server This is made to search git for an update and flip a boolean

var isUpdate = false;
var checkUpdate = function(){
  simpleGit.pull(function(err, data){
    if(data && data.summary.changes){
      isUpdate = true;
    }
  });
  return isUpdate;
};
// if true is sent then /api/reboot will be called from the client
router.get('/api/update', function (req, res) {
  res.json({isUpdate: checkUpdate()});
});
// reboot client
router.get('/api/reboot', function (req, res) {
  process.exit(0);
});

gulps default and tasks called

// Easy to understand but will show start
gulp.task('default', function(callback) {
  runSequence('bundle', 'chrome', 'start', callback);
});
// Launch node process
function boot() {
  return exec('node app.js', function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
  }).on('exit', function () {
    runSequence('bundle', boot);
  });
}
// Launches server and auto-updates
gulp.task('start', function(cb){
  boot()
});

On the client I ran an interval to check for an update when there was I asked the user to update and when they say yes a reboot is sent. I then tell the user to wait for a second while the update finishes and then I reload the page with.

$window.location.reload();

Inside of a timeout of course so the user has time to read the message about what version they are on.