Skip to content

Commit

Permalink
[api] Make forever.load() sync and not required for default configura…
Browse files Browse the repository at this point in the history
…tions. Grossly simplifies saving / reloading semantics
  • Loading branch information
indexzero committed Feb 14, 2011
1 parent fd1b9a6 commit cc3d465
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 93 deletions.
155 changes: 80 additions & 75 deletions bin/forever
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ if (!options.sourceDir) options.sourceDir = process.cwd();

// Setup configurations for forever
var config = {
root: argv.p || forever.path
root: argv.p
};

function tryStart (callback) {
Expand All @@ -124,79 +124,84 @@ function tryStart (callback) {
});
}

// Log starting...
sys.puts('Loading forever with config: ');
eyes.inspect(config)

var loader = forever.load(config);
loader.on('load', function () {
sys.puts('Loaded forever. Tidying /tmp/forever.');
var tidy = forever.cleanUp(action === 'cleanlogs');
tidy.on('cleanUp', function () {
sys.puts('/tmp/forever tidied.');
if (file) {
sys.puts('Working with file: ' + file);
}
if (options) {
sys.puts('Using forever options:');
eyes.inspect(options);
}
if (action) {
sys.puts('Running action: ' + action.yellow);
switch (action) {
case 'start':
tryStart(function () { forever.startDaemon(file, options); });
break;
case 'stop':
var runner = forever.stop(file, true);
runner.on('stop', function (process) {
sys.puts('Forever stopped process:');
sys.puts(process);
});
runner.on('error', function (err) {
sys.puts('Forever cannot find process with index: ' + file)
})
break;

case 'stopall':
var runner = forever.stopAll(true);
runner.on('stopAll', function (processes) {
if (processes) {
sys.puts('Forever stopped processes:');
sys.puts(processes);
}
else {
sys.puts('No forever processes running');
}
});
break;

case 'restart':
var runner = forever.restart(file, true);
runner.on('restart', function (processes) {
if (processes) {
sys.puts('Forever restarted processes:');
sys.puts(processes);
}
else {
sys.puts('No forever processes running');
}
});
break;

case 'list':
var processes = forever.list(true);
sys.puts(processes ? processes : 'No forever processes running');
break;
}
}
else {
tryStart(function () {
forever.start(file, options).save().on('restart', function (fvr) { fvr.save() });
});
//
// Only call `forever.load()` if the root path is different than
// the default root exposed by forever.
//
if (config.root && config.root !== forever.root) {
sys.puts('Loading forever with config: ');
eyes.inspect(config);

forever.load(config);
sys.puts('Loaded forever successfully.');
}

sys.puts('Tidying ' + forever.config.root);
var tidy = forever.cleanUp(action === 'cleanlogs');
tidy.on('cleanUp', function () {
sys.puts(forever.config.root + ' tidied.');
if (file) {
sys.puts('Working with file: ' + file);
}

if (options) {
sys.puts('Using forever options:');
eyes.inspect(options);
}

if (action) {
sys.puts('Running action: ' + action.yellow);
switch (action) {
case 'start':
tryStart(function () { forever.startDaemon(file, options); });
break;

case 'stop':
var runner = forever.stop(file, true);
runner.on('stop', function (process) {
sys.puts('Forever stopped process:');
sys.puts(process);
});
runner.on('error', function (err) {
sys.puts('Forever cannot find process with index: ' + file)
})
break;

case 'stopall':
var runner = forever.stopAll(true);
runner.on('stopAll', function (processes) {
if (processes) {
sys.puts('Forever stopped processes:');
sys.puts(processes);
}
else {
sys.puts('No forever processes running');
}
});
break;

case 'restart':
var runner = forever.restart(file, true);
runner.on('restart', function (processes) {
if (processes) {
sys.puts('Forever restarted processes:');
sys.puts(processes);
}
else {
sys.puts('No forever processes running');
}
});
break;
case 'list':
var processes = forever.list(true);
sys.puts(processes ? processes : 'No forever processes running');
break;
}
});
}
else {
tryStart(function () {
forever.start(file, options);
});
}
});
56 changes: 39 additions & 17 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,56 @@ var forever = exports, config;
// ### Export Components / Settings
// Export `version` and important Prototypes from `lib/forever/*`
//
forever.version = [0, 4, 0];
forever.path = path.join('/tmp', 'forever');
forever.Forever = forever.Monitor = require('forever/monitor').Monitor;
forever.version = [0, 4, 0];
forever.initialized = false;
forever.root = path.join('/tmp', 'forever');
forever.Forever = forever.Monitor = require('forever/monitor').Monitor;

//
// ### function load (options, [callback])
// #### @options {Object} Options to load into the forever module
// #### [@callback] {function} Continuation to pass control back to
// Initializes configuration for forever module
//
forever.load = function (options, callback) {
var emitter = new events.EventEmitter();
forever.load = function (options) {
//
// Setup the incoming options with default options.
//
options = options || {};
options.root = options.root || forever.path,
options.root = options.root || forever.root,
options.pidPath = options.pidPath || path.join(options.root, 'pids');
forever.config = config = options;

// Create the two directories, ignoring errors
fs.mkdir(config.root, 0755, function (err) {
fs.mkdir(config.pidPath, 0755, function (err2) {
if (callback) callback();
emitter.emit('load');
});
});
//
// If forever is initalized and the config directories are identical
// simply return without creating directories
//
if (forever.initialized && forever.config.root === options.root &&
forever.config.pidPath === options.pidPath) {
return;
}

return emitter;
forever.config = config = options;

//
// Syncronously create the `root` directory
// and the `pid` directory for forever. Although there is
// an additional overhead here of the sync action. It simplifies
// the setup of forever dramatically.
//
function tryCreate (dir) {
try { fs.mkdirSync(dir, 0755); }
catch (ex) { }
}

tryCreate(config.root);
tryCreate(config.pidPath);
forever.initialized = true;
};

//
// Ensure forever will always be loaded the first time it is required.
//
forever.load();

//
// ### function stat (logFile, script, callback)
// #### @logFile {string} Path to the log file for this script
Expand Down Expand Up @@ -102,7 +124,7 @@ forever.startDaemon = function (script, options) {
// });

process.pid = pid;
runner.start().save().on('restart', function (fvr) { fvr.save() });
runner.start();
});

return runner;
Expand Down
3 changes: 2 additions & 1 deletion lib/forever/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ Monitor.prototype.start = function (restart) {
this.ctime = Date.now();
this.child = child;
this.running = true;
self.emit(restart ? 'restart' : 'start', self);
this.emit(restart ? 'restart' : 'start', self);
this.save()

// Hook all stream data and process it
function listenTo (stream) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"keywords": ["cli", "fault tolerant", "sysadmin", "tools"],
"dependencies": {
"async": ">= 0.1.8",
"colors": ">= 0.3.0",
"daemon": ">= 0.3.0",
"optimist": ">= 0.0.6",
Expand Down

0 comments on commit cc3d465

Please sign in to comment.