Skip to content

Instantly share code, notes, and snippets.

@jbielick
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save jbielick/e5ee2ef638ccf18cbd88 to your computer and use it in GitHub Desktop.

Select an option

Save jbielick/e5ee2ef638ccf18cbd88 to your computer and use it in GitHub Desktop.

Revisions

  1. jbielick renamed this gist Nov 29, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. jbielick revised this gist Nov 29, 2014. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,4 @@
    var path = require('path');
    var utilities = require('utilities');
    var file = utilities.file;
    var child_process = require('child_process');
    var currentFork;

  3. jbielick revised this gist Nov 29, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -53,9 +53,9 @@ results:
    ┌[josh@jarvice] [130]
    └[~/code/clustertest]> node --debug app.js
    Debugger listening on port 5858
    Debugger listening on port 5859
    Debugger listening on port 5859
    Debugger listening on port 5858 -- this is the master
    Debugger listening on port 5859 -- fork
    Debugger listening on port 5859 -- ^^
    Debugger listening on port 5859
    Debugger listening on port 5859
  4. jbielick revised this gist Nov 29, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,7 @@ if (!process.send) {
    fork();
    // cycle fork simulation
    setInterval(function() {
    currentFork.kill('SIGINT');
    currentFork.kill();
    }, 3000);
    }

  5. jbielick revised this gist Nov 29, 2014. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -18,9 +18,6 @@ function fork(){
    execArgs.push('--debug=5859');
    }

    // give arg flagging child process as a worker
    args.push('--worker');

    // fork this file with args + '--worker' and sameish execArgs
    currentFork = child_process.fork(__filename, args);

    @@ -40,7 +37,8 @@ setInterval(function failsafe() {


    // fork if this is the master
    if (process.argv.indexOf('--worker') < 0) {
    // process will not have .send if it isn't a fork
    if (!process.send) {
    fork();
    // cycle fork simulation
    setInterval(function() {
  6. jbielick created this gist Nov 29, 2014.
    64 changes: 64 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    var path = require('path');
    var utilities = require('utilities');
    var file = utilities.file;
    var child_process = require('child_process');
    var currentFork;

    var timesStarted = 0;

    function fork(){
    var args = process.argv.slice(2),
    execArgs = process.execArgv,
    isDebug = process.execArgv.indexOf('--debug');

    // rudimentary example of making sure we bind to a different
    // debug port than the master process
    if (isDebug > -1) {
    execArgs.splice(isDebug, 1);
    execArgs.push('--debug=5859');
    }

    // give arg flagging child process as a worker
    args.push('--worker');

    // fork this file with args + '--worker' and sameish execArgs
    currentFork = child_process.fork(__filename, args);

    timesStarted++;

    currentFork.on('close', fork);
    }

    setInterval(function failsafe() {
    if (timesStarted > 1) {
    console.error("loop detected");
    currentFork.kill();
    process.exit(0);
    }
    timesStarted = 0;
    }, 500);


    // fork if this is the master
    if (process.argv.indexOf('--worker') < 0) {
    fork();
    // cycle fork simulation
    setInterval(function() {
    currentFork.kill('SIGINT');
    }, 3000);
    }


    /*
    results:
    ┌[josh@jarvice] [130]
    └[~/code/clustertest]> node --debug app.js
    Debugger listening on port 5858
    Debugger listening on port 5859
    Debugger listening on port 5859
    Debugger listening on port 5859
    Debugger listening on port 5859
    */