Skip to content

Commit

Permalink
Implemented flag --watch-delay with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
livankrekh committed Jul 1, 2018
1 parent cae6313 commit 3785d6e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
1 change: 1 addition & 0 deletions bin/pm2
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ commander.version(pkg.version)
.option('--merge-logs', 'merge logs from different instances but keep error and out separated')
.option('--watch [paths]', 'watch application folder for changes', function(v, m) { m.push(v); return m;}, [])
.option('--ignore-watch <folders|files>', 'List of paths to ignore (name or regex)')
.option('--watch-delay <delay>', 'specify a restart delay after changing files (--watch-delay 4 (in sec) or 4000ms)')
.option('--no-color', 'skip colors')
.option('--no-vizion', 'start an app without vizion feature (versioning control)')
.option('--no-autorestart', 'start an app without automatic restart')
Expand Down
8 changes: 8 additions & 0 deletions lib/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,14 @@ class API {
}
}

if (opts.watchDelay) {
if (typeof opts.watchDelay === "string" && opts.watchDelay.indexOf("ms") !== -1)
app_conf.watch_delay = parseInt(opts.watchDelay);
else {
app_conf.watch_delay = parseFloat(opts.watchDelay) * 1000;
}
}

/**
* If -w option, write configuration to configuration.json file
*/
Expand Down
22 changes: 12 additions & 10 deletions lib/Watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,18 @@ module.exports = function ClusterMode(God) {

console.error('Change detected on path %s for app %s - restarting', path, pm2_env.name);

God.restartProcessName(pm2_env.name, function(err, list) {
self.restarting = false;

if (err) {
log('Error while restarting', err);
return false;
}

return log('Process restarted');
});
setTimeout(function() {
God.restartProcessName(pm2_env.name, function(err, list) {
self.restarting = false;

if (err) {
log('Error while restarting', err);
return false;
}

return log('Process restarted');
});
}, (pm2_env.watch_delay || 0));

return false;
});
Expand Down
2 changes: 1 addition & 1 deletion test/programmatic/flagWatch.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ describe('Flag --ignore-watch', function() {
f_w.handleFolders('./node_modules', res);
should(res).be.empty();
});
});
});
10 changes: 10 additions & 0 deletions test/programmatic/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ describe('Watcher', function() {
})
})

it('should work with watch_delay', function(cb) {
testPM2Env('server-watch:online')({watch: true, watch_delay: 4000}, cb);
pm2.start(extend(json, {watch: true, watch_delay: 4000}), errShouldBeNull);
})

it('should not crash with watch_delay without watch', function(cb) {
testPM2Env('server-watch:online')({watch_delay: 4000}, cb);
pm2.start(extend(json, {watch_delay: 4000}), errShouldBeNull);
})

/**
* Test #1668
*/
Expand Down

0 comments on commit 3785d6e

Please sign in to comment.