-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function to force and forbid respawning
- Loading branch information
Showing
8 changed files
with
335 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,46 @@ | ||
const reorder = require('./lib/reorder'); | ||
const respawn = require('./lib/respawn'); | ||
const remover = require('./lib/remover'); | ||
|
||
module.exports = function (flags, argv, execute) { | ||
const FORBID_RESPAWNING_FLAG = '--no-respawning'; | ||
|
||
module.exports = function (flags, argv, forcedFlags, execute) { | ||
if (!flags) { | ||
throw new Error('You must specify flags to respawn with.'); | ||
} | ||
if (!argv) { | ||
throw new Error('You must specify an argv array.'); | ||
} | ||
|
||
if (typeof forcedFlags === 'function') { | ||
execute = forcedFlags; | ||
forcedFlags = []; | ||
} else if (!Array.isArray(forcedFlags)) { | ||
forcedFlags = []; | ||
} | ||
|
||
var index = argv.indexOf(FORBID_RESPAWNING_FLAG); | ||
if (index >= 0) { | ||
argv = remover([FORBID_RESPAWNING_FLAG], argv); | ||
argv = remover(flags, argv); | ||
execute(true, process, argv); | ||
return; | ||
} | ||
|
||
var proc = process; | ||
var reordered = reorder(flags, argv); | ||
var ready = JSON.stringify(argv) === JSON.stringify(reordered); | ||
|
||
if (forcedFlags.length) { | ||
reordered = reordered.slice(0, 1) | ||
.concat(forcedFlags) | ||
.concat(reordered.slice(1)); | ||
ready = false; | ||
} | ||
|
||
if (!ready) { | ||
reordered.push(FORBID_RESPAWNING_FLAG); | ||
proc = respawn(reordered); | ||
} | ||
execute(ready, proc); | ||
execute(ready, proc, reordered); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module.exports = function(flags, argv) { | ||
var args = argv.slice(0, 1); | ||
for (var i = 1, n = argv.length; i < n; i++) { | ||
var arg = argv[i]; | ||
var flag = arg.split('=')[0]; | ||
if (flags.indexOf(flag) < 0) { | ||
args.push(arg); | ||
} | ||
} | ||
return args; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/usr/bin/env node | ||
|
||
const flaggedRespawn = require('../../'); | ||
const v8flags = require('v8flags'); | ||
|
||
// get a list of all possible v8 flags for the running version of node | ||
v8flags(function(err, flags) { | ||
if (err) { | ||
console.error(err); | ||
return; | ||
} | ||
|
||
var argv = process.argv.concat('--no-respawning'); | ||
|
||
flaggedRespawn(flags, argv, function (ready, child) { | ||
if (ready) { | ||
console.log('Running!'); | ||
} else { | ||
console.log('Special flags found, respawning.'); | ||
} | ||
if (child.pid !== process.pid) { | ||
console.log('Respawned to PID:', child.pid); | ||
} | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/usr/bin/env node | ||
|
||
const flaggedRespawn = require('../../'); | ||
const v8flags = require('v8flags'); | ||
|
||
// get a list of all possible v8 flags for the running version of node | ||
v8flags(function(err, flags) { | ||
if (err) { | ||
console.error(err); | ||
return; | ||
} | ||
|
||
var argv = process.argv.concat('--no-respawning'); | ||
|
||
flaggedRespawn(flags, argv, ['--trace-warnings'], function (ready, child) { | ||
if (ready) { | ||
console.log('Running!'); | ||
} else { | ||
console.log('Respawning!'); | ||
} | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/env node | ||
|
||
const flaggedRespawn = require('../../'); | ||
const v8flags = require('v8flags'); | ||
|
||
// get a list of all possible v8 flags for the running version of node | ||
v8flags(function(err, flags) { | ||
if (err) { | ||
console.error(err); | ||
return; | ||
} | ||
|
||
flaggedRespawn(flags, process.argv, ['--trace-warnings'], function (ready, child) { | ||
if (ready) { | ||
console.log('Running!'); | ||
} else { | ||
console.log('Respawning!'); | ||
} | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/usr/bin/env node | ||
|
||
const flaggedRespawn = require('../..'); | ||
const v8flags = require('v8flags'); | ||
const path = require('path'); | ||
|
||
v8flags(function(err, flags) { | ||
if (err) { | ||
console.error(err); | ||
return; | ||
} | ||
|
||
flaggedRespawn(flags, process.argv, [ | ||
'--trace-warnings', | ||
'--require', | ||
'v8flags', | ||
'--v8-pool-size=2', | ||
], function (ready, child, args) { | ||
if (ready) { | ||
console.log('cli args passed to app:', args.join(' ')); | ||
} else { | ||
console.log('Respawning!'); | ||
} | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters