Skip to content

Commit

Permalink
[api] Added ctime property to forever instances to track uptime
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Feb 14, 2011
1 parent bc07f95 commit 85b0a02
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
9 changes: 8 additions & 1 deletion bin/forever
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var path = require('path'),
fs = require('fs'),
eyes = require('eyes'),
sys = require('sys');

var accepts = ['start', 'stop', 'stopall', 'list', 'cleanlogs'], action;
Expand Down Expand Up @@ -95,7 +96,7 @@ if (typeof options['max'] === 'undefined') {

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

function tryStart (callback) {
Expand All @@ -115,10 +116,16 @@ 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. Cleaning logs...');
var tidy = forever.cleanUp(action === 'cleanlogs');
tidy.on('cleanUp', function () {
sys.puts('Cleaned logs. Running action: ' + action.yellow);
if (action) {
switch (action) {
case 'start':
Expand Down
9 changes: 6 additions & 3 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var fs = require('fs'),
path = require('path'),
events = require('events'),
exec = require('child_process').exec,
timespan = require('timespan'),
daemon = require('daemon');

var forever = exports, config;
Expand All @@ -22,6 +23,7 @@ var forever = exports, config;
// 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;

//
Expand All @@ -33,7 +35,7 @@ forever.Forever = forever.Monitor = require('forever/monitor').Monitor;
forever.load = function (options, callback) {
var emitter = new events.EventEmitter();
options = options || {};
options.root = options.root || path.join('/tmp', 'forever'),
options.root = options.root || forever.path,
options.pidPath = options.pidPath || path.join(options.root, 'pids');
forever.config = config = options;

Expand Down Expand Up @@ -82,10 +84,10 @@ forever.start = function (script, options) {
// #### @options {Object} Configuration for forever instance.
// Starts a script with forever as a daemon
//
forever.startDaemon = function (file, options) {
forever.startDaemon = function (script, options) {
options.logFile = path.join(config.root, options.logFile || 'forever.log');
options.pidFile = path.join(config.pidPath, options.pidFile);
var runner = new forever.Monitor(file, options);
var runner = new forever.Monitor(script, options);

daemon.daemonize(options.logFile, options.pidFile, function (err, pid) {
if (err) return runner.emit('error', err);
Expand Down Expand Up @@ -339,6 +341,7 @@ function formatProcess (proc, index, padding) {
.concat(proc.options.map(function (opt) { return opt.green }))
.concat([padding + '[' + proc.pid + ',', proc.foreverPid + ']'])
.concat(proc.logFile.magenta)
.concat(timespan.fromDates(new Date(proc.ctime), new Date()).toString().yellow)
.join(' ');
};

Expand Down
3 changes: 3 additions & 0 deletions lib/forever/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

var sys = require('sys'),
fs = require('fs'),
path = require('path'),
events = require('events'),
spawn = require('child_process').spawn,
forever = require('forever');
Expand Down Expand Up @@ -79,6 +80,7 @@ Monitor.prototype.start = function (restart) {
return this;
}

this.ctime = Date.now();
this.child = child;
this.running = true;
self.emit(restart ? 'restart' : 'start', self);
Expand Down Expand Up @@ -163,6 +165,7 @@ Monitor.prototype.save = function () {
}

var childData = {
ctime: this.ctime,
pid: this.child.pid,
foreverPid: process.pid,
logFile: this.logFile,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"colors": ">= 0.3.0",
"daemon": ">= 0.3.0",
"optimist": ">= 0.0.6",
"timespan": ">= 2.0.0",
"vows": ">= 0.5.2"
},
"bin": { "forever": "./bin/forever" },
Expand Down

0 comments on commit 85b0a02

Please sign in to comment.