soldair / node-forkfriend

worker child process manager. respawn children. load balance work amongst children.
10 stars 4 forks source link

Build Status

forkfriend

dead simple worker process manager. respawn children. load balance work amongst children.

there are a some obvious use cases.

example

Process Send / Manager api. This is useful if you just need to keep a set of child processes running but do not need to send a high volume of data. You may send as much data as you want but know you should pause if send returns false.


var forkfriend = require('friend');
var friend = forkfriend();

// make 3 somefriend.js worker with arguments
friend.add('somefriend.js',['-a',1],3);

// workers for the same file get the same args as the first. 
// multiple calls to add add that many more to the pool
friend.add('somefriend.js')

// if send returns false you should wait for the drain event before sending more data.
friend.send('hey one of you work on this');

friend.on('message',function(message,workername,worker){
  // message is the data the child sent.
  // workername === 'somefriend.js'
  // worker instanceof ChildProcess
})

// when you are all done
friend.stop();

Stream api.

If you are going to do any meaningful amount of work you already have a stream. A stream abstraction is essential because the IPC channel WILL get backed up until the process is killed or you run out of memory and everything starts to suck. The manager is a Duplex Stream so just pipe in and pipe out the results. Workers need not send back results. But if they do those will be emitted as message/data events on the manager.

var fs = require('fs');
// you will need some sort of transform stream to change the buffers into distinct messages.
// the child process manager load balances "messages" ie calls to send()
// each data event sent to the manager must be a distinct message in this 
var lineStream = require('line-stream');

var forkfriend = require('forkfriend');

var manager = forkfriend();
// fork 4 processes of worker.js
// the messages will be distributed across all 4 processes in a round robin/first available scheme
manager.add('./worker.js',4);

fs.createReadStream('application.log')
.pipe(lineStream())
.pipe(manager)
.pipe(fs.createWriteStream('processed.log'));

api

forkfriend

forkfriend(options)

friend

friend events

stream

what does a worker look like


process.on('message',function(data){
  // do somthing
  // tell friend about it
  process.send('hey i worked onn '+data);
});

woo hooo.

let me know if this is helpful or if you have any issues.