From 6588f596d505192b7238932a7e90d80bd4a8bc14 Mon Sep 17 00:00:00 2001 From: indexzero Date: Fri, 6 Jan 2012 01:45:15 -0500 Subject: [PATCH] [api test] Expose `.forkShim` for communicating between `0.6.x` and `0.4.x` processes --- lib/forever/monitor.js | 11 ++++++++++- test/monitor/fork-test.js | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/lib/forever/monitor.js b/lib/forever/monitor.js index acf23a72..84bb8e72 100644 --- a/lib/forever/monitor.js +++ b/lib/forever/monitor.js @@ -9,7 +9,8 @@ var events = require('events'), fs = require('fs'), path = require('path'), - fork = require('node-fork').fork, + nodeFork = require('node-fork'), + fork = nodeFork.fork, spawn = require('child_process').spawn, broadway = require('broadway'), psTree = require('ps-tree'), @@ -84,6 +85,7 @@ var Monitor = exports.Monitor = function (script, options) { this.spawnWith = options.spawnWith || {}; this.sourceDir = options.sourceDir; this.fork = options.fork || false; + this.forkShim = options.forkShim || false; this.cwd = options.cwd || null; this.hideEnv = options.hideEnv || []; this._env = options.env || {}; @@ -224,7 +226,14 @@ Monitor.prototype.trySpawn = function () { this.spawnWith.env = this._getEnv(); if (this.fork) { + this.spawnWith.silent = true; this.spawnWith.command = this.command; + + if (this.forkShim) { + this.spawnWith.env['FORK_SHIM'] = true; + return nodeFork.shim.fork(this.args[0], this.args.slice(1), this.spawnWith); + } + return fork(this.args[0], this.args.slice(1), this.spawnWith); } diff --git a/test/monitor/fork-test.js b/test/monitor/fork-test.js index 49d359c5..bad30802 100644 --- a/test/monitor/fork-test.js +++ b/test/monitor/fork-test.js @@ -14,16 +14,37 @@ var assert = require('assert'), vows.describe('forever/monitor/fork').addBatch({ "When using forever": { "and spawning a script that uses `process.send()`": { - topic: function () { - var script = path.join(__dirname, '..', '..', 'examples', 'process-send.js'), - child = new (forever.Monitor)(script, { silent: false, minUptime: 2000, max: 1, fork: true }); + "using the 'native' fork": { + topic: function () { + var script = path.join(__dirname, '..', '..', 'examples', 'process-send.js'), + child = new (forever.Monitor)(script, { silent: false, minUptime: 2000, max: 1, fork: true }); - child.on('message', this.callback.bind(null, null)); - child.start(); + child.on('message', this.callback.bind(null, null)); + child.start(); + }, + "should reemit the message correctly": function (err, msg) { + assert.isObject(msg); + assert.deepEqual(msg, { from: 'child' }); + } }, - "should reemit the message correctly": function (err, msg) { - assert.isObject(msg); - assert.deepEqual(msg, { from: 'child' }); + "with `forkShim` true": { + topic: function () { + var script = path.join(__dirname, '..', '..', 'examples', 'process-send.js'), + child = new (forever.Monitor)(script, { + silent: false, + minUptime: 2000, + max: 1, + fork: true, + forkShim: true + }); + + child.on('message', this.callback.bind(null, null)); + child.start(); + }, + "should reemit the message correctly": function (err, msg) { + assert.isObject(msg); + assert.deepEqual(msg, { from: 'child' }); + } } } }