Skip to content

Simple reload support "borrowed" from supervisor.js #11

Open
bloodmage opened this Issue Jul 11, 2011 · 0 comments

1 participant

@bloodmage

I'm very glad to see if multi-node have support of "reload", this will make a "seamless" (no-connection-drop) restart server is a safe (isolated) manner.

Following is a simple watchdog of javascript files. "borrowed" from supervisor.js : https://github.com/isaacs/node-supervisor

    if (options.watchFiles) {
        console.log('INFO: watch files enabled.');
        var timer = null, counter = -1, mtime = null;
        function crash (oldStat, newStat) {

          // we only care about modification time, not access time.
          if (
            newStat.mtime.getTime() === oldStat.mtime.getTime()
          ) return;

          if (counter === -1) {
            timer = setTimeout(stopCrashing, 1000);
          }
          counter ++;

          if (counter == 0)
          {
            console.log("inform self about restart");
            process.kill(process.pid, 'SIGHUP');
          }
          else
          {
            console.log("overkill, ignore");
          }
        }

        function stopCrashing () {
          counter = -1;
        }

        function watchGivenFile (watch) {
          console.log('add watch file:'+watch);
          fs.watchFile(watch, crash);
        }


        fileExtensionPattern = /.*\.(node|js)/;

        var findAllWatchFiles = function(path, callback) {
          fs.stat(path, function(err, stats){
            if (err) {
              sys.error('Error retrieving stats for file: ' + path);
            } else {
              if (stats.isDirectory()) {
                fs.readdir(path, function(err, fileNames) {
                  if(err) {
                    sys.puts('Error reading path: ' + path);
                  }
                  else {
                    fileNames.forEach(function (fileName) {
                      findAllWatchFiles(path + '/' + fileName, callback);
                    });
                  }
                });
              } else {
                if (path.match(fileExtensionPattern)) {
                  callback(path);
                }
              }
            }
          });
        }
        findAllWatchFiles('./', watchGivenFile);
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.