From 7336c7f66e94ac9e979a344c8b1f6ccecb1a71f7 Mon Sep 17 00:00:00 2001 From: Daniel Poelzleithner Date: Thu, 21 Jul 2011 13:29:53 +0200 Subject: [PATCH] try to restart on crash sometimes a restarted server will not instantly restart due to some open sockets for example. restartr now tries to restart crashed servers with some sleeps in between for some seconds (default: 3) and will try up to 5 times (default). Before giving up and waiting for file changes. This helped a lot with not working restarts here. --- restartr | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/restartr b/restartr index efca1a9..4acf25a 100755 --- a/restartr +++ b/restartr @@ -11,6 +11,10 @@ var fs = require('fs'), opts = parseCmdOpts(argv), cmd = opts.cmd, args = opts.args, + delay_wait = opts.delay_wait ? opts.delay_wait : 3000, + max_crash_restart = opts.max_crash_restart ? opts.max_crash_restart : 5, + restart_count = 0, + restart_id = 0, files = opts.files, crashed = false; @@ -20,13 +24,19 @@ files.forEach(function(file) { log('Files modified, restarting', 'log'); child.kill('SIGHUP'); + if (restart_id) clearTimeout(restart_id); if (crashed) start(); }); }); +delay = function() { + restart_id = setTimeout(start, delay_wait) +} + start = function() { log('\nMonitoring the following files for changes:\n' + inspect(files) + '\n'); log('STARTING NEW CHILD', 'log'); + restart_id = 0; child = spawn(cmd, args, {customFds: [1, 1, 1]}); var launched = (new Date).getTime(), @@ -36,7 +46,18 @@ start = function() { child.on('exit', function() { log('\nCHILD EXITED: ' + pid + '\n', 'log'); crashed = (((new Date).getTime() - launched) < crashThreshhold); - crashed ? log('process crashed, waiting for file change', 'warn') : start(); + if(crashed) { + restart_count += 1; + if(restart_count < max_crash_restart) { + log('process crashed. Retry count: ' + restart_count, 'warn'); + delay(); + } else { + log('process crashed, waiting for file change', 'warn'); + } + } else { + restart_count = 0; + start(); + } }); }; @@ -145,4 +166,4 @@ function log(msg, level) { } }; -start(); \ No newline at end of file +start();