Skip to content

Commit bde27e0

Browse files
committed
[refactor] Use the nssocket defined protocol for stopping and restarting worker processes
1 parent dc0b457 commit bde27e0

File tree

2 files changed

+95
-144
lines changed

2 files changed

+95
-144
lines changed

lib/forever.js

Lines changed: 82 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ function getAllProcesses(callback) {
105105
}
106106

107107
socket.dataOnce(['data'], function (data) {
108+
data.socket = fullPath;
108109
next(null, data);
109110
socket.end();
110111
});
@@ -146,6 +147,59 @@ function getAllPids(processes) {
146147
});
147148
}
148149

150+
function stopOrRestart(action, event, format, target) {
151+
var emitter = new events.EventEmitter(),
152+
results = [],
153+
pids;
154+
155+
function sendAction(proc, next) {
156+
var socket = new nssocket.NsSocket();
157+
158+
socket.connect(proc.socket, function (err) {
159+
if (err) {
160+
next(err);
161+
}
162+
163+
socket.dataOnce([action, 'ok'], function (data) {
164+
next();
165+
socket.end();
166+
});
167+
168+
socket.send([action]);
169+
});
170+
171+
socket.on('error', function (err) {
172+
next(err);
173+
});
174+
}
175+
176+
getAllProcesses(function (processes) {
177+
var procs = processes;
178+
179+
if (target) {
180+
procs = forever.findByIndex(target, processes)
181+
|| forever.findByScript(target, processes);
182+
}
183+
184+
if (procs && procs.length > 0) {
185+
async.map(procs, sendAction, function (err, results) {
186+
if (err) {
187+
emitter.emit('error', err);
188+
}
189+
190+
emitter.emit(event, forever.format(format, procs));
191+
});
192+
}
193+
else {
194+
process.nextTick(function () {
195+
emitter.emit('error', new Error('Cannot find forever process: ' + target));
196+
});
197+
}
198+
});
199+
200+
return emitter;
201+
}
202+
149203
//
150204
// ### function load (options, [callback])
151205
// #### @options {Object} Options to load into the forever module
@@ -383,33 +437,7 @@ forever.startServer = function () {
383437
// in the list of all processes
384438
//
385439
forever.stop = function (target, format) {
386-
var emitter = new events.EventEmitter(),
387-
results = [],
388-
pids;
389-
390-
getAllProcesses(function (processes) {
391-
var procs = forever.findByIndex(target, processes)
392-
|| forever.findByScript(target, processes);
393-
394-
if (procs && procs.length > 0) {
395-
pids = procs.reduce(function (agg, proc) {
396-
return agg.concat(proc.foreverPid, proc.pid);
397-
}, []);
398-
399-
async.forEach(pids, function (pid, next) {
400-
forever.kill(pid, true, next);
401-
}, function () {
402-
emitter.emit('stop', forever.format(format, procs));
403-
});
404-
}
405-
else {
406-
process.nextTick(function () {
407-
emitter.emit('error', new Error('Cannot find forever process: ' + target));
408-
});
409-
}
410-
});
411-
412-
return emitter;
440+
return stopOrRestart('stop', 'stop', format, target);
413441
};
414442

415443
//
@@ -420,88 +448,7 @@ forever.stop = function (target, format) {
420448
// in the list of all processes
421449
//
422450
forever.restart = function (target, format) {
423-
var emitter = new events.EventEmitter(),
424-
runner = forever.stop(target, false);
425-
426-
runner.on('stop', function (procs) {
427-
if (procs && procs.length > 0) {
428-
async.forEach(procs, function (proc, next) {
429-
//
430-
// We need to spawn a new process running the forever CLI
431-
// here because we want each process to daemonize separately
432-
// without the main process running `forever restart myscript.js`
433-
// daemonizing itself.
434-
//
435-
var restartCommand = [
436-
'forever',
437-
'start',
438-
'--sourceDir', proc.sourceDir,
439-
'-l', proc.logFile,
440-
'--append true'
441-
];
442-
443-
if (proc.silent) {
444-
restartCommand.push('--silent true');
445-
}
446-
447-
if (proc.command) {
448-
restartCommand.push('-c', command);
449-
}
450-
451-
if (proc.outFile) {
452-
restartCommand.push('-o', path.join(proc.sourceDir, proc.outFile));
453-
}
454-
455-
if (proc.errFile) {
456-
restartCommand.push('-e', path.join(proc.sourceDir, proc.outFile));
457-
}
458-
459-
restartCommand.push(proc.file, proc.options.join(' '));
460-
forever.log.silly('Restarting with options', { options: restartCommand.join(' ') });
461-
462-
exec(restartCommand.join(' '), proc.spawnWith, function (err, stdout, stderr) {
463-
next();
464-
});
465-
}, function () {
466-
emitter.emit('restart', forever.format(format, procs));
467-
});
468-
}
469-
else {
470-
emitter.emit('error', new Error('Cannot find forever process: ' + target));
471-
}
472-
});
473-
474-
// Bubble up the error to the appropriate EventEmitter instance.
475-
runner.on('error', function (err) {
476-
emitter.emit('error', err);
477-
});
478-
479-
return emitter;
480-
};
481-
482-
//
483-
// ### function findByIndex (index, processes)
484-
// #### @index {string} Index of the process to find.
485-
// #### @processes {Array} Set of processes to find in.
486-
// Finds the process with the specified index.
487-
//
488-
forever.findByIndex = function (index, processes) {
489-
var proc = processes && processes[parseInt(index, 10)];
490-
return proc ? [proc] : null;
491-
};
492-
493-
//
494-
// ### function findByScript (script, processes)
495-
// #### @script {string} The name of the script to find.
496-
// #### @processes {Array} Set of processes to find in.
497-
// Finds the process with the specified script name.
498-
//
499-
forever.findByScript = function (script, processes) {
500-
return !processes
501-
? null
502-
: processes.filter(function (p) {
503-
return p.file === script;
504-
});
451+
return stopOrRestart('restart', 'restart', format, target);
505452
};
506453

507454
//
@@ -510,36 +457,7 @@ forever.findByScript = function (script, processes) {
510457
// Stops all processes managed by forever.
511458
//
512459
forever.stopAll = function (format) {
513-
var emitter = new events.EventEmitter();
514-
515-
getAllProcesses(function (processes) {
516-
var pids = getAllPids(processes);
517-
518-
if (format) {
519-
processes = forever.format(format, processes);
520-
}
521-
522-
if (pids && processes) {
523-
pids = pids.reduce(function (agg, proc) {
524-
return agg.concat(proc.foreverPid, proc.pid);
525-
}, []);
526-
527-
async.forEach(pids, function (pid, next) {
528-
if (pid !== process.pid) {
529-
forever.kill(pid, true, next);
530-
}
531-
}, function () {
532-
emitter.emit('stopAll', processes);
533-
});
534-
}
535-
else {
536-
process.nextTick(function () {
537-
emitter.emit('stopAll', null);
538-
});
539-
}
540-
});
541-
542-
return emitter;
460+
return stopOrRestart('stop', 'stopAll', format);
543461
};
544462

545463
//
@@ -603,6 +521,31 @@ forever.tail = function (target, length, callback) {
603521
});
604522
};
605523

524+
//
525+
// ### function findByIndex (index, processes)
526+
// #### @index {string} Index of the process to find.
527+
// #### @processes {Array} Set of processes to find in.
528+
// Finds the process with the specified index.
529+
//
530+
forever.findByIndex = function (index, processes) {
531+
var proc = processes && processes[parseInt(index, 10)];
532+
return proc ? [proc] : null;
533+
};
534+
535+
//
536+
// ### function findByScript (script, processes)
537+
// #### @script {string} The name of the script to find.
538+
// #### @processes {Array} Set of processes to find in.
539+
// Finds the process with the specified script name.
540+
//
541+
forever.findByScript = function (script, processes) {
542+
return !processes
543+
? null
544+
: processes.filter(function (p) {
545+
return p.file === script;
546+
});
547+
};
548+
606549
//
607550
// ### function format (format, procs)
608551
// #### @format {Boolean} Value indicating if processes should be formatted

lib/forever/worker.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,21 @@ Worker.prototype.start = function (callback) {
6868
});
6969
});
7070

71-
socket.data(['kill'], function () {
72-
self.monitor.on('stop', function () {
73-
socket.send(['kill', 'stop']);
74-
this.exitOnKill && process.exit();
71+
socket.data(['stop'], function () {
72+
self.monitor.once('stop', function () {
73+
socket.send(['stop', 'ok']);
74+
process.exit();
7575
});
7676

77-
self.monitor.kill();
77+
self.monitor.stop();
78+
});
79+
80+
socket.data(['restart'], function () {
81+
self.monitor.once('restart', function () {
82+
socket.send(['restart', 'ok']);
83+
});
84+
85+
self.monitor.restart();
7886
});
7987
});
8088

0 commit comments

Comments
 (0)