Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cluster support #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 22 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var events = require('events')
var spawn = require('child_process').spawn
var cluster = require('cluster')
var exec = require('child_process').exec
var ps = require('ps-tree')
var util = require('util')
Expand Down Expand Up @@ -42,12 +43,30 @@ var Monitor = function(command, opts) {
this.command = command
this.cwd = opts.cwd || '.'
this.env = opts.env || {}
this.cluster = opts.mode === 'cluster'
this.uid = opts.uid
this.gid = opts.gid
this.pid = 0
this.stdio = opts.stdio
this.windowsVerbatimArguments = opts.windowsVerbatimArguments

if (this.cluster) {
process.cwd(this.cwd);
var settings = {};
if (this.uid) {
process.setuid(this.uid);
settings.uid = this.uid;
}
if (this.gid) {
process.setgid(this.gid);
settings.gid = this.gid;
}
var args = command.slice(1);
settings.exec = args.shift();
settings.args = args;
cluster.setupMaster(settings);
}

this.crashed = false
this.sleep = typeof opts.sleep === 'function' ? opts.sleep : defaultSleep(opts.sleep)
this.maxRestarts = opts.maxRestarts === 0 ? 0 : opts.maxRestarts || -1
Expand Down Expand Up @@ -100,7 +119,8 @@ Monitor.prototype.start = function() {
var clock = 60000

var loop = function() {
var child = spawn(self.command[0], self.command.slice(1), {
var child = self.cluster ? cluster.fork(xtend(process.env, self.env)) :
spawn(self.command[0], self.command.slice(1), {
cwd: self.cwd,
env: xtend(process.env, self.env),
uid: self.uid,
Expand Down Expand Up @@ -176,6 +196,7 @@ Monitor.prototype.toJSON = function() {
id: this.id,
status: this.status,
started: this.started,
cluster: this.cluster,
pid: this.pid,
command: this.command,
cwd: this.cwd,
Expand Down