Skip to content

Commit

Permalink
worker mixin.
Browse files Browse the repository at this point in the history
  • Loading branch information
dshaw committed Nov 18, 2011
1 parent 244a836 commit cc8537c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/stock-quotes/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var path = require('path')
, connect = require('connect')
, sio = require('socket.io')
, RedisStore = sio.RedisStore
, worker = require('../../').worker

var port = process.argv[2] || 8880
, id = process.argv[3] || 0
Expand Down
1 change: 1 addition & 0 deletions examples/stock-quotes/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

var announce = require('socket.io-announce').createClient()
, worker = require('../../').worker

/* fake data stream */
var symbols = 'THOO GOOF EXIT BOP SDD ALPP RIGM OPPL HPBG'.split(' ')
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ exports = module.exports = require('./lib/mixture.js')
*/

exports.version = require('./package.json').version

/**
* Worker mixin
*/

exports.worker = require('./lib/worker')
1 change: 1 addition & 0 deletions lib/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Task.prototype.fork = function(filename, args, options) {

// Node's `NODE_WORKER_ID` has too much behavior attached to it.
envCopy['MIXTURE_WORKER_ID'] = id
envCopy['MIXTURE_TASK_NAME'] = this.name

for (var x in this.options) {
mergedOptions[x] = this.options[x]
Expand Down
37 changes: 37 additions & 0 deletions lib/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*!
* mixture
* Copyright(c) 2011 Daniel D. Shaw <dshaw@dshaw.com>
* MIT Licensed
*/

/**
* Exports.
*/

var send = exports.send = process.send || noop
var taskName = exports.taskName = process.env.MIXTURE_TASK_NAME
var workerId = exports.workerId = parseInt(process.env.MIXTURE_WORKER_ID)

/**
* Noop.
*/

function noop () { console.log('worker noop', arguments) }

/**
* Master Message handler.
*/

function onMasterMessage () { console.log('worker message', arguments) }

/**
* Receive messages from Mix Master.
*/

process.on('message', onMasterMessage)

/**
* Notify Mix Master.
*/

send({ 'worker:online' : workerId, task: taskName, pid: process.pid })

0 comments on commit cc8537c

Please sign in to comment.