Skip to content

Commit

Permalink
[bin] Ensure both daemons and long running processes get the same sta…
Browse files Browse the repository at this point in the history
…t checking
  • Loading branch information
indexzero committed Dec 24, 2010
1 parent ea6849d commit 38177c4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
43 changes: 22 additions & 21 deletions bin/forever
Original file line number Diff line number Diff line change
Expand Up @@ -95,33 +95,32 @@ if (typeof options['max'] === 'undefined') {
var config = {
root: argv.p
};

function tryStart (callback) {
var fullPath, uid = forever.randomString(16);
options.uid = uid;
options.pidFile = 'forever' + uid + '.pid';
options.logFile = argv.l || 'forever' + uid + '.log';
fullPath = path.join(forever.config.root, options.logFile);

forever.stat(fullPath, file, function (err) {
if (err) {
sys.puts('Cannot start forever: ' + err.message);
process.exit(0);
}

callback();
});
}

var loader = forever.load(config);
loader.on('load', function () {
var tidy = forever.cleanUp(action === 'cleanlogs');
tidy.on('cleanUp', function () {
if (action) {
switch (action) {
case 'start':
var fullPath, uid = forever.randomString(16);
options.uid = uid;
options.pidFile = 'forever' + uid + '.pid';
options.logFile = argv.l || 'forever' + uid + '.log';
fullPath = path.join(forever.config.root, options.logFile);

fs.stat(fullPath, function (err, stats) {
if (!err) {
sys.puts('Cannot start forever: ' + fullPath + ' exists.');
process.exit(0);
}

fs.stat(file, function (err, stats) {
if (err) {
sys.puts('Cannot start forever: ' + file + ' does not exist.');
process.exit(0);
}
forever.startDaemon(file, options);
});
});
tryStart(function () { forever.startDaemon(file, options); });
break;

case 'stop':
Expand Down Expand Up @@ -155,7 +154,9 @@ loader.on('load', function () {
}
}
else {
forever.start(file, options);
tryStart(function () {
forever.start(file, options).save().on('restart', function (fvr) { fvr.save() });
});
}
});
});
15 changes: 15 additions & 0 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ forever.load = function (options, callback) {
return emitter;
};

//
// function stat (logFile, script, callback)
// Ensures that the logFile doesn't exist and that
// the target script does exist before executing callback.
//
forever.stat = function (logFile, script, callback) {
fs.stat(logFile, function (err, stats) {
if (!err) return callback(new Error('log file ' + logFile + ' exists.'));
fs.stat(script, function (err, stats) {
if (err) return callback(new Error('script ' + script + ' does not exist.'));
callback(null);
});
});
};

//
// function start (file, options)
// Starts a script with forever
Expand Down

0 comments on commit 38177c4

Please sign in to comment.