Skip to content

Commit

Permalink
fix: use fork child node processes
Browse files Browse the repository at this point in the history
* heisian-master:
  fix: get tests to pass
  fix: travis CI build before-install
  test: fork child node processes
  • Loading branch information
remy committed Dec 12, 2017
2 parents 6d760f4 + 0628f26 commit 62a361c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
13 changes: 12 additions & 1 deletion lib/monitor/run.js
Expand Up @@ -4,6 +4,7 @@ var bus = utils.bus;
var childProcess = require('child_process');
var spawn = childProcess.spawn;
var exec = childProcess.exec;
var fork = childProcess.fork;
var watch = require('./watch').watch;
var config = require('../config');
var child = null; // the actual child process we spawn
Expand Down Expand Up @@ -82,7 +83,17 @@ function run(options) {
});
}

child = spawn.apply(null, spawnArgs);
if (executable === 'node') {
var forkArgs = cmd.args.slice(1);
var env = utils.merge(options.execOptions.env, process.env);
stdio.push('ipc');
child = fork(options.execOptions.script, forkArgs, {
env: env,
stdio: stdio,
});
} else {
child = spawn.apply(null, spawnArgs);
}

if (config.required) {
var emit = {
Expand Down
3 changes: 2 additions & 1 deletion lib/monitor/watch.js
Expand Up @@ -79,7 +79,8 @@ function watch() {
}

watchedFiles.push(file);
total++;
watchedFiles = Array.from(new Set(watchedFiles)); // ensure no dupes
total = watchedFiles.length;
debug('watching dir: %s', file);
});
watcher.on('ready', function () {
Expand Down
53 changes: 25 additions & 28 deletions test/monitor/run.test.js
Expand Up @@ -153,32 +153,29 @@ describe('when nodemon runs (2)', function () {
}, 1500);
});

// it('should kill child on SIGINT', function (done) {
// fs.writeFileSync(tmp, 'setTimeout(function () { var n = 10; }, 10000)');

// nodemon({ script: tmp, verbose: true }).on('start', function () {
// assert(true, 'nodemon is waiting for a change');

// setTimeout(function () {
// // process.once('SIGINT', function () {
// // // do nothing
// // console.log('not going to exit');
// // });

// process.kill(process.pid, 'SIGINT');
// }, 1000);
// }).on('crash', function () {
// assert(false, 'detected crashed state');
// }).on('exit', function () {
// assert(true, 'quit correctly');
// nodemon.reset();
// done();

// setTimeout(function () {
// process.kill(process.pid, 'SIGINT');
// }, 1000);

// });

// });
it('should kill child on SIGINT', function (done) {
fs.writeFileSync(tmp, 'setTimeout(function () { var n = 10; }, 10000)');

nodemon({ script: tmp, verbose: true }).on('start', function () {
assert(true, 'nodemon is waiting for a change');

setTimeout(function () {
process.once('SIGINT', function () {
// do nothing
});

process.kill(process.pid, 'SIGINT');
}, 1000);
}).on('crash', function () {
assert(false, 'detected crashed state');
}).on('exit', function () {
assert(true, 'quit correctly');
nodemon.reset();
done();

setTimeout(function () {
process.kill(process.pid, 'SIGINT');
}, 1000);
});
});
});

0 comments on commit 62a361c

Please sign in to comment.