Skip to content

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

@ghost

Description

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);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions