socket.data(['spawn'], function (data) { if (!data.script) { return socket.send(['spawn', 'error'], { error: new Error('No script given') }); } if (self.monitor) { return socket.send(['spawn', 'error'], { error: new Error("Already running") }); } var monitor = new (forever.Monitor)(data.script, data.options); monitor.start(); monitor.on('start', function () { socket.send(['spawn', 'start'], monitor.data); }); });
child.on('stop', function () { monitor.pid.process = null; // Do not emit if the deamon is in restart mode if (daemonInRestart) return; monitor.emit('process', 'stop'); });
// // ### @function start (options) // #### @options {Object} Options for the `forever.Monitor` instance. // Starts the child process and disconnects from the IPC channel. // function start(options) { var script = process.argv[2], monitor = new forever.Monitor(script, options); forever.logEvents(monitor); monitor.start(); monitor.on('start', function () { // // This starts an nssocket server, which the forever CLI uses to // communicate with this monitor process after it's detached. // Without this, `forever list` won't show the process, even though it // would still be running in the background unaffected. // forever.startServer(monitor); // // Write the pidFile to disk // writePid(options.pidFile, monitor.child.pid); }); // // When the monitor restarts update the pid in the pidFile // monitor.on('restart', function () { writePid(options.pidFile, monitor.child.pid); }); monitor.on('start', function(childp) { try { process.send(JSON.stringify({ error: null, childPid: childp.childData.pid })); process.disconnect(); // Disconnect the IPC channel, letting this monitor's parent process know that the child has started successfully. } catch(e) {} }); function cleanUp(reason) { // // When the monitor stops or exits, remove the pid and log files // return function cleanUp(){ try { fs.unlinkSync(options.pidFile); process.send(JSON.stringify({ error: reason })); process.disconnect(); // Disconnect the IPC channel, letting this monitor's parent process know that the child has started successfully. } catch(e) {} } } monitor.on('stop', cleanUp('stop')); monitor.on('exit', cleanUp('exit')); }
var setupMonitor = function (emits, message, daemonPid) { var monitor = new Monitor(message, options, daemonPid, shutdown, child, function () { // emit events before starting child Object.keys(emits).forEach(function (name) { // skip the process event until the child spawns if (name === 'process') return; monitor.emit.apply(monitor, [name].concat( emits[name] )); }); // spawn child when monitor is ready child.spawn(); // start pumping streams to monitor child.pump(monitor); }); // once the child has started emit the process event child.once('start', function () { monitor.pid.process = child.pid; monitor.emit.apply(monitor, ['process'].concat( emits.process )); }); // relay stop and restart to process event on monitor child.on('stop', function () { monitor.pid.process = null; // Do not emit if the deamon is in restart mode if (daemonInRestart) return; monitor.emit('process', 'stop'); }); child.on('restart', function () { monitor.pid.process = child.pid; monitor.emit('process', 'restart'); }); };
'use strict'; /* global require, process, __dirname */ var Monitor = require(__dirname + '/monitor.js').Monitor; // Execute the script. var watchList = process.env.WATCH || 'apps,dev_apps', monitor = new Monitor(process.cwd(), {'directories': watchList.split(',')}); monitor.watch();
child.on('restart', function () { monitor.pid.process = child.pid; monitor.emit('process', 'restart'); });