Skip to content

Commit e05db8d

Browse files
committed
Merge pull request #733 from tkambler/master
Adds support for JSON configuration values
2 parents 804b5b1 + 7e7d1bb commit e05db8d

7 files changed

Lines changed: 144 additions & 11 deletions

File tree

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[*.js]
2+
indent_style = space
3+
indent_size = 2

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,41 @@ There are [several examples][1] designed to test the fault tolerance of forever.
9898
$ forever -m 5 examples/error-on-timer.js
9999
```
100100

101+
### JSON Configuration Files
102+
103+
In addition to passing forever the path to a script (along with accompanying options, described above), you may also pass forever the path to a JSON file, in which these options are defined. For example, consider an application with the following file structure:
104+
105+
```
106+
.
107+
├── forever
108+
│ └── development.json
109+
└── index.js
110+
111+
// forever/development.json
112+
{
113+
// Comments are supported
114+
"uid": "app",
115+
"append": true,
116+
"watch": true,
117+
"script": "index.js",
118+
"sourceDir": "/home/myuser/app"
119+
}
120+
```
121+
122+
This application could be started with forever, as shown below:
123+
124+
``` bash
125+
$ forever start ./forever/development.json
126+
```
127+
128+
Absolute paths to such configuration files are also supported:
129+
130+
``` bash
131+
$ forever start /home/myuser/app/forever/development.json
132+
```
133+
134+
**Note:** Forever parses JSON configuration files using [shush](https://github.com/krakenjs/shush), allowing the use of in-line comments within such files.
135+
101136
### Using In Your Code
102137
The forever module exposes some useful methods to use in your code. Each method returns an instance of an EventEmitter which emits when complete. See the [forever cli commands][2] for sample usage.
103138

lib/forever/cli.js

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ var fs = require('fs'),
1212
colors = require('colors'),
1313
cliff = require('cliff'),
1414
flatiron = require('flatiron'),
15+
shush = require('shush'),
16+
objectAssign = require('object-assign'),
1517
forever = require('../forever');
1618

1719
var cli = exports;
@@ -196,7 +198,31 @@ function checkColumn(name) {
196198
// `forever.startDaemon`
197199
//
198200
var getOptions = cli.getOptions = function (file) {
199-
var options = {};
201+
var options = {},
202+
absFile = path.isAbsolute(file) ? file : path.resolve(process.cwd(), file),
203+
configKeys = [
204+
'pidFile', 'logFile', 'errFile', 'watch', 'minUptime', 'append',
205+
'silent', 'outFile', 'max', 'command', 'path', 'spinSleepTime',
206+
'sourceDir', 'workingDir', 'uid', 'watchDirectory', 'watchIgnore',
207+
'killTree', 'killSignal', 'id'
208+
],
209+
jsonConfig = {};
210+
211+
//
212+
// Load JSON configuration values
213+
//
214+
if (path.extname(file) === '.json') {
215+
jsonConfig = path.isAbsolute(file) ? shush(file) : shush(absFile);
216+
jsonConfig = Object.keys(jsonConfig).reduce(function (acc, curr, i, arr) {
217+
if (!!~configKeys.indexOf(curr) || curr === 'script') { acc[curr] = jsonConfig[curr]; }
218+
return acc;
219+
}, {});
220+
if (!jsonConfig.script) {
221+
forever.log.error('The \'script\' option is required when using JSON configuration files');
222+
process.exit(1);
223+
}
224+
}
225+
200226
//
201227
// First isolate options which should be passed to file
202228
//
@@ -209,12 +235,7 @@ var getOptions = cli.getOptions = function (file) {
209235
app.config.stores.argv.store = {};
210236
app.config.use('argv', argvOptions);
211237

212-
[
213-
'pidFile', 'logFile', 'errFile', 'watch', 'minUptime', 'append',
214-
'silent', 'outFile', 'max', 'command', 'path', 'spinSleepTime',
215-
'sourceDir', 'workingDir', 'uid', 'watchDirectory', 'watchIgnore',
216-
'killTree', 'killSignal', 'id'
217-
].forEach(function (key) {
238+
configKeys.forEach(function (key) {
218239
options[key] = app.config.get(key);
219240
});
220241

@@ -240,6 +261,8 @@ var getOptions = cli.getOptions = function (file) {
240261
options.workingDir = options.workingDir || options.sourceDir;
241262
options.spawnWith = { cwd: options.workingDir };
242263

264+
objectAssign(options, jsonConfig);
265+
243266
return options;
244267
};
245268

@@ -264,6 +287,8 @@ app.cmd(/start (.+)/, cli.startDaemon = function () {
264287
var file = app.argv._[1],
265288
options = getOptions(file);
266289

290+
file = options.script ? options.script : file;
291+
267292
forever.log.info('Forever processing file: ' + file.grey);
268293
tryStart(file, options, function () {
269294
forever.startDaemon(file, options);
@@ -559,6 +584,8 @@ cli.run = function () {
559584
var file = app.argv._[0],
560585
options = getOptions(file);
561586

587+
file = options.script ? options.script : file;
588+
562589
tryStart(file, options, function () {
563590
var monitor = forever.start(file, options);
564591
monitor.on('start', function () {

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
"tools"
2020
],
2121
"dependencies": {
22-
"colors": "~0.6.2",
2322
"cliff": "~0.1.9",
23+
"colors": "~0.6.2",
2424
"flatiron": "~0.4.2",
2525
"forever-monitor": "~1.5.1",
2626
"nconf": "~0.6.9",
2727
"nssocket": "~0.5.1",
28+
"object-assign": "^3.0.0",
2829
"optimist": "~0.6.0",
30+
"shush": "^1.0.0",
2931
"timespan": "~2.3.0",
3032
"utile": "~0.2.1",
3133
"winston": "~0.8.1"

test/core/start-stop-json-test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* start-stop-json-test.js: start or stop forever using relative paths, the script path could be start with './', '../' ...
3+
*
4+
* (C) 2010 Charlie Robbins & the Contributors
5+
* MIT LICENCE
6+
*
7+
*/
8+
9+
var assert = require('assert'),
10+
path = require('path'),
11+
fs = require('fs'),
12+
spawn = require('child_process').spawn,
13+
vows = require('vows'),
14+
forever = require('../../lib/forever');
15+
16+
function runCmd(cmd, args) {
17+
var proc = spawn(process.execPath, [
18+
path.resolve(__dirname, '../../', 'bin/forever'),
19+
cmd
20+
].concat(args), {detached: true});
21+
22+
proc.unref();
23+
}
24+
25+
vows.describe('forever/core/start-stop-json').addBatch({
26+
"When using forever" : {
27+
"to start process using JSON configuration file" : {
28+
topic: function () {
29+
runCmd('start', [
30+
'./test/fixtures/server.json'
31+
]);
32+
setTimeout(function (that) {
33+
forever.list(false, that.callback);
34+
}, 2000, this);
35+
},
36+
"the startup should works fine": function (err, procs) {
37+
assert.isNull(err);
38+
assert.isArray(procs);
39+
assert.equal(procs.length, 1);
40+
}
41+
}
42+
}
43+
}).addBatch({
44+
"When the script is running" : {
45+
"try to stop by uid" : {
46+
topic: function () {
47+
runCmd('stop', [
48+
'server'
49+
]);
50+
setTimeout(function (that) {
51+
forever.list(false, that.callback);
52+
}, 2000, this);
53+
},
54+
"the shut down should works fine": function (err, procs) {
55+
assert.isNull(err);
56+
assert.isNull(procs);
57+
}
58+
}
59+
}
60+
}).export(module);

test/core/start-stop-relative-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* start-stop-relative.js: start or stop forever using relative paths, the script path could be start with './', '../' ...
2+
* start-stop-relative-test.js: start or stop forever using relative paths, the script path could be start with './', '../' ...
33
*
44
* (C) 2010 Charlie Robbins & the Contributors
55
* MIT LICENCE
@@ -22,7 +22,7 @@ function runCmd(cmd, args) {
2222
proc.unref();
2323
}
2424

25-
vows.describe('forever/core/start-stop-peaceful').addBatch({
25+
vows.describe('forever/core/start-stop-relative').addBatch({
2626
"When using forever" : {
2727
"to run script with relative script path" : {
2828
topic: function () {
@@ -57,4 +57,4 @@ vows.describe('forever/core/start-stop-peaceful').addBatch({
5757
}
5858
}
5959
}
60-
}).export(module);
60+
}).export(module);

test/fixtures/server.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"uid": "server",
3+
"append": true,
4+
"script": "server.js",
5+
"sourceDir": "./test/fixtures"
6+
}

0 commit comments

Comments
 (0)