Skip to content

Commit cc3d465

Browse files
committed
[api] Make forever.load() sync and not required for default configurations. Grossly simplifies saving / reloading semantics
1 parent fd1b9a6 commit cc3d465

File tree

4 files changed

+122
-93
lines changed

4 files changed

+122
-93
lines changed

bin/forever

+80-75
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ if (!options.sourceDir) options.sourceDir = process.cwd();
103103

104104
// Setup configurations for forever
105105
var config = {
106-
root: argv.p || forever.path
106+
root: argv.p
107107
};
108108

109109
function tryStart (callback) {
@@ -124,79 +124,84 @@ function tryStart (callback) {
124124
});
125125
}
126126

127-
// Log starting...
128-
sys.puts('Loading forever with config: ');
129-
eyes.inspect(config)
130-
131-
var loader = forever.load(config);
132-
loader.on('load', function () {
133-
sys.puts('Loaded forever. Tidying /tmp/forever.');
134-
var tidy = forever.cleanUp(action === 'cleanlogs');
135-
tidy.on('cleanUp', function () {
136-
sys.puts('/tmp/forever tidied.');
137-
if (file) {
138-
sys.puts('Working with file: ' + file);
139-
}
140-
141-
if (options) {
142-
sys.puts('Using forever options:');
143-
eyes.inspect(options);
144-
}
145-
146-
if (action) {
147-
sys.puts('Running action: ' + action.yellow);
148-
switch (action) {
149-
case 'start':
150-
tryStart(function () { forever.startDaemon(file, options); });
151-
break;
152-
153-
case 'stop':
154-
var runner = forever.stop(file, true);
155-
runner.on('stop', function (process) {
156-
sys.puts('Forever stopped process:');
157-
sys.puts(process);
158-
});
159-
runner.on('error', function (err) {
160-
sys.puts('Forever cannot find process with index: ' + file)
161-
})
162-
break;
163-
164-
case 'stopall':
165-
var runner = forever.stopAll(true);
166-
runner.on('stopAll', function (processes) {
167-
if (processes) {
168-
sys.puts('Forever stopped processes:');
169-
sys.puts(processes);
170-
}
171-
else {
172-
sys.puts('No forever processes running');
173-
}
174-
});
175-
break;
176-
177-
case 'restart':
178-
var runner = forever.restart(file, true);
179-
runner.on('restart', function (processes) {
180-
if (processes) {
181-
sys.puts('Forever restarted processes:');
182-
sys.puts(processes);
183-
}
184-
else {
185-
sys.puts('No forever processes running');
186-
}
187-
});
188-
break;
189-
190-
case 'list':
191-
var processes = forever.list(true);
192-
sys.puts(processes ? processes : 'No forever processes running');
193-
break;
194-
}
195-
}
196-
else {
197-
tryStart(function () {
198-
forever.start(file, options).save().on('restart', function (fvr) { fvr.save() });
199-
});
127+
//
128+
// Only call `forever.load()` if the root path is different than
129+
// the default root exposed by forever.
130+
//
131+
if (config.root && config.root !== forever.root) {
132+
sys.puts('Loading forever with config: ');
133+
eyes.inspect(config);
134+
135+
forever.load(config);
136+
sys.puts('Loaded forever successfully.');
137+
}
138+
139+
sys.puts('Tidying ' + forever.config.root);
140+
var tidy = forever.cleanUp(action === 'cleanlogs');
141+
tidy.on('cleanUp', function () {
142+
sys.puts(forever.config.root + ' tidied.');
143+
if (file) {
144+
sys.puts('Working with file: ' + file);
145+
}
146+
147+
if (options) {
148+
sys.puts('Using forever options:');
149+
eyes.inspect(options);
150+
}
151+
152+
if (action) {
153+
sys.puts('Running action: ' + action.yellow);
154+
switch (action) {
155+
case 'start':
156+
tryStart(function () { forever.startDaemon(file, options); });
157+
break;
158+
159+
case 'stop':
160+
var runner = forever.stop(file, true);
161+
runner.on('stop', function (process) {
162+
sys.puts('Forever stopped process:');
163+
sys.puts(process);
164+
});
165+
runner.on('error', function (err) {
166+
sys.puts('Forever cannot find process with index: ' + file)
167+
})
168+
break;
169+
170+
case 'stopall':
171+
var runner = forever.stopAll(true);
172+
runner.on('stopAll', function (processes) {
173+
if (processes) {
174+
sys.puts('Forever stopped processes:');
175+
sys.puts(processes);
176+
}
177+
else {
178+
sys.puts('No forever processes running');
179+
}
180+
});
181+
break;
182+
183+
case 'restart':
184+
var runner = forever.restart(file, true);
185+
runner.on('restart', function (processes) {
186+
if (processes) {
187+
sys.puts('Forever restarted processes:');
188+
sys.puts(processes);
189+
}
190+
else {
191+
sys.puts('No forever processes running');
192+
}
193+
});
194+
break;
195+
196+
case 'list':
197+
var processes = forever.list(true);
198+
sys.puts(processes ? processes : 'No forever processes running');
199+
break;
200200
}
201-
});
201+
}
202+
else {
203+
tryStart(function () {
204+
forever.start(file, options);
205+
});
206+
}
202207
});

lib/forever.js

+39-17
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,56 @@ var forever = exports, config;
2323
// ### Export Components / Settings
2424
// Export `version` and important Prototypes from `lib/forever/*`
2525
//
26-
forever.version = [0, 4, 0];
27-
forever.path = path.join('/tmp', 'forever');
28-
forever.Forever = forever.Monitor = require('forever/monitor').Monitor;
26+
forever.version = [0, 4, 0];
27+
forever.initialized = false;
28+
forever.root = path.join('/tmp', 'forever');
29+
forever.Forever = forever.Monitor = require('forever/monitor').Monitor;
2930

3031
//
3132
// ### function load (options, [callback])
3233
// #### @options {Object} Options to load into the forever module
33-
// #### [@callback] {function} Continuation to pass control back to
3434
// Initializes configuration for forever module
3535
//
36-
forever.load = function (options, callback) {
37-
var emitter = new events.EventEmitter();
36+
forever.load = function (options) {
37+
//
38+
// Setup the incoming options with default options.
39+
//
3840
options = options || {};
39-
options.root = options.root || forever.path,
41+
options.root = options.root || forever.root,
4042
options.pidPath = options.pidPath || path.join(options.root, 'pids');
41-
forever.config = config = options;
4243

43-
// Create the two directories, ignoring errors
44-
fs.mkdir(config.root, 0755, function (err) {
45-
fs.mkdir(config.pidPath, 0755, function (err2) {
46-
if (callback) callback();
47-
emitter.emit('load');
48-
});
49-
});
44+
//
45+
// If forever is initalized and the config directories are identical
46+
// simply return without creating directories
47+
//
48+
if (forever.initialized && forever.config.root === options.root &&
49+
forever.config.pidPath === options.pidPath) {
50+
return;
51+
}
5052

51-
return emitter;
53+
forever.config = config = options;
54+
55+
//
56+
// Syncronously create the `root` directory
57+
// and the `pid` directory for forever. Although there is
58+
// an additional overhead here of the sync action. It simplifies
59+
// the setup of forever dramatically.
60+
//
61+
function tryCreate (dir) {
62+
try { fs.mkdirSync(dir, 0755); }
63+
catch (ex) { }
64+
}
65+
66+
tryCreate(config.root);
67+
tryCreate(config.pidPath);
68+
forever.initialized = true;
5269
};
5370

71+
//
72+
// Ensure forever will always be loaded the first time it is required.
73+
//
74+
forever.load();
75+
5476
//
5577
// ### function stat (logFile, script, callback)
5678
// #### @logFile {string} Path to the log file for this script
@@ -102,7 +124,7 @@ forever.startDaemon = function (script, options) {
102124
// });
103125

104126
process.pid = pid;
105-
runner.start().save().on('restart', function (fvr) { fvr.save() });
127+
runner.start();
106128
});
107129

108130
return runner;

lib/forever/monitor.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ Monitor.prototype.start = function (restart) {
8888
this.ctime = Date.now();
8989
this.child = child;
9090
this.running = true;
91-
self.emit(restart ? 'restart' : 'start', self);
91+
this.emit(restart ? 'restart' : 'start', self);
92+
this.save()
9293

9394
// Hook all stream data and process it
9495
function listenTo (stream) {

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
"keywords": ["cli", "fault tolerant", "sysadmin", "tools"],
1515
"dependencies": {
16+
"async": ">= 0.1.8",
1617
"colors": ">= 0.3.0",
1718
"daemon": ">= 0.3.0",
1819
"optimist": ">= 0.0.6",

0 commit comments

Comments
 (0)