Permalink
Browse files

extracted the restart callback out. fixed the watch file bug

  • Loading branch information...
liangzan committed May 20, 2012
1 parent 84413c0 commit 83f3812db7f500ec5fafcac5bc05de80e8066d15
Showing with 29 additions and 31 deletions.
  1. +29 −31 lib/process_manager.js
View
@@ -41,6 +41,27 @@ processManager.spawnProcesses = function(cmdOptions, childProcs, watchedFiles, p
}
};
+processManager.restartCallback = function(opt) {
+ return function(event, filename) {
+ _.each(parser.matchingProcesses(opt.procName, opt.childProcs), function(pName) {
+ recordWatchedFiles(pName, opt.filePath, opt.watchedFiles);
+ killRunningProcess(pName, function(err) {
+ if (err) {
+ winston.error('[forewoman] Error stopping the process');
+ winston.error(err.message);
+ } else {
+ var prevChildProcess = opt.childProcs[pName];
+ opt.childProcs[pName] = spawnProcess(prevChildProcess.command,
+ opt.cmdOptions,
+ opt.childProcs,
+ opt.watchedFiles);
+ }
+ });
+ });
+ };
+};
+
+
/*
* start watching the files
*
@@ -51,23 +72,13 @@ processManager.watchFiles = function(cmdOptions, childProcs, watchedFiles) {
var watchOpts = parser.parseWatchFileOpts(cmdOptions);
_.each(_.keys(watchOpts), function(procName) {
findAllWatchFiles(watchOpts[procName], function(filePath) {
- watchGivenFile(filePath, function(event, filename) {
- _.each(parser.matchingProcesses(procName, childProcs), function(pName) {
- recordWatchedFiles(pName, filePath, watchedFiles);
- killRunningProcess(pName, function(err) {
- if (err) {
- winston.error('[forewoman] Error stopping the process');
- winston.error(err.message);
- } else {
- var prevChildProcess = childProcs[pName];
- childProcs[pName] = spawnProcess(prevChildProcess.command,
- cmdOptions,
- childProcs,
- watchedFiles);
- }
- });
- });
- });
+ watchGivenFile(filePath, processManager.restartCallback({
+ procName: procName,
+ childProcs: childProcs,
+ filePath: filePath,
+ cmdOptions: cmdOptions,
+ watchedFiles: watchedFiles
+ }));
});
});
};
@@ -118,7 +129,7 @@ function recordWatchedFiles(processName, filePath, watchedFiles) {
* @callback {string} event, {string} filename
*/
function watchGivenFile(filePath, callback) {
- fs.watchFile(filePath, { persistent: true }, callback);
+ fs.watchFile(filePath, { persistent: false }, callback);
}
/*
@@ -167,18 +178,6 @@ function procEnvVar(cmdOptions, index) {
return envVar;
}
-
-
-function removeWatch(processName, watchedFiles) {
- if (typeof watchedFiles[processName] !== 'undefined' &&
- watchedFiles[processName].length > 0) {
- while(watchedFiles[processName].length > 0) {
- fs.unwatchFile(watchedFiles[processName].shift());
- }
- }
-}
-
-
/*
* spawns the individual processes
*
@@ -205,7 +204,6 @@ function spawnProcess(opts, cmdOptions, childProcs, watchedFiles) {
childProcess.on('exit', function(code) {
console.log(('[' + opts.name + '] exited with code ' + code)[color]);
removePIDFile(opts.name);
- removeWatch(opts.name, watchedFiles);
});
childProcess.command = opts;

0 comments on commit 83f3812

Please sign in to comment.