Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ _Synchronously_ sets the specified configuration (config) for the forever module
Starts a script with forever.

### forever.startDaemon (file, options)
Starts a script with forever as a daemon. WARNING: Will daemonize the current process.
Starts a script with forever as a daemon. WARNING: Will daemonize the current process. `file` field can be ommited when `options.command` is set.

### forever.stop (index)
Stops the forever daemon script at the specified index. These indices are the same as those returned by forever.list(). This method returns an EventEmitter that raises the 'stop' event when complete.
Expand Down
39 changes: 34 additions & 5 deletions bin/monitor
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ function start(options) {
// would still be running in the background unaffected.
//
forever.startServer(monitor);

//
// Disconnect the IPC channel, letting this monitor's parent process know
// that the child has started successfully.
//
process.disconnect();

//
// Write the pidFile to disk
//
writePid(options.pidFile, monitor.child.pid);
});

//
// When the monitor restarts update the pid in the pidFile
//
Expand All @@ -64,7 +64,36 @@ function start(options) {
catch(e){}
}
monitor.on('stop', cleanUp);
monitor.on('exit', cleanUp);
monitor.on('exit', function()
{
cleanUp()

// Start fallback command
var onexit = options.onexit
if(onexit)
{
var env = options.env || {}

// Service has a environment defined, override current process.env
if(onexit.env)
onexit.env.__proto__ = env

// Service has not defined an environment, use current process.env
else
onexit.env = env;

onexit.stdio = onexit.stdio || options.stdio

onexit.uid = onexit.uid || options.uid
onexit.logFile = onexit.logFile || options.logFile
onexit.pidFile = onexit.pidFile || options.pidFile

// Start service
console.log(onexit)
var child = forever.startDaemon(onexit)
console.log(child)
}
});
}

//
Expand All @@ -80,4 +109,4 @@ process.on('message', function (data) {
started = true;
start(options);
}
});
});
52 changes: 52 additions & 0 deletions bin/starter
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env node

var resolve = require('path').resolve

var forever = require('..')


// Get path of configuration file. By default, search on the user's $HOME
var servicesPath;
if(process.argv.length > 2)
servicesPath = process.argv[2]
else
servicesPath = resolve(process.env.HOME, 'etc', 'forever-starter.json')


// Load configuration file
var services;
try
{
services = require(servicesPath)
}
catch(exception)
{
console.error(exception)
process.exit(-1)
}


// Start services
services.forEach(function(service)
{
if(typeof service == 'string')
service =
{
command: service,
enabled: true
};

if(service.enabled)
{
// Service has a environment defined, override current process.env
if(service.env)
service.env.__proto__ = process.env

// Service has not defined an environment, use current process.env
else
service.env = process.env;

// Start service
forever.startDaemon(service)
}
})
15 changes: 13 additions & 2 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,15 @@ forever.start = function (script, options) {
// Starts a script with forever as a daemon
//
forever.startDaemon = function (script, options) {
if (script && !Array.isArray(script) && typeof script != 'string') {
options = script;
script = undefined;
}

if (!script && !(options && options.command)) {
throw new SyntaxError("One of 'script' or 'options.command' fields must be defined")
}

options = options || {};
options.uid = options.uid || utile.randomString(4).replace(/^\-/, '_');
options.logFile = forever.logFilePath(options.logFile || forever.config.get('logFile') || options.uid + '.log');
Expand All @@ -399,9 +408,11 @@ forever.startDaemon = function (script, options) {
//
outFD = fs.openSync(options.logFile, 'a');
errFD = fs.openSync(options.logFile, 'a');
monitorPath = path.resolve(__dirname, '..', 'bin', 'monitor');

monitor = spawn(process.execPath, [monitorPath, script], {
var args = [path.resolve(__dirname, '..', 'bin', 'monitor')];
if(script) args.push(script);

monitor = spawn(process.execPath, args, {
stdio: ['ipc', outFD, errFD],
detached: true
});
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"vows": "0.7.x"
},
"bin": {
"forever": "./bin/forever"
"forever": "bin/forever",
"forever-starter": "bin/starter"
},
"main": "./lib/forever",
"scripts": {
Expand Down