Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Hello, Mixture.

  • Loading branch information...
commit c994fbb85d70ef6629258bed4e5c814f24e652c1 0 parents
@dshaw authored
8 .gitignore
@@ -0,0 +1,8 @@
+*.swp
+.DS_Store
+.idea
+nohup.out
+npm-debug.log
+logs
+node_modules
+pids
17 index.js
@@ -0,0 +1,17 @@
+/*!
+ * mixture
+ * Copyright(c) 2011 Daniel D. Shaw <dshaw@dshaw.com>
+ * MIT Licensed
+ */
+
+/**
+ * Mixture
+ */
+
+exports = module.exports = require('./lib/mixture.js');
+
+/**
+ * Version
+ */
+
+exports.version = '0.0.0';
61 lib/mixture.js
@@ -0,0 +1,61 @@
+/*!
+ * mixture
+ * Copyright(c) 2011 Daniel D. Shaw <dshaw@dshaw.com>
+ * MIT Licensed
+ */
+
+/**
+ * Module dependencies.
+ */
+
+var assert = require('assert')
+ , util = require('util')
+ , EventEmitter = require('events').EventEmitter
+ , Task = require('./task')
+
+/**
+ * Exports
+ */
+
+exports = module.exports = new Master();
+
+/**
+ * Mix Master
+ */
+
+function Master () {
+ this.ids = 0
+ this.tasks = {}
+ this.workers = []
+}
+
+/**
+ * Inherit EventEmitter
+ */
+
+util.inherit(Master, EventEmitter)
+
+/**
+ * Task mix
+ *
+ * @param name
+ * @return Task
+ * @api public
+ */
+
+Master.prototype.mix = function (name) {
+ if (!this.tasks[name]) {
+ this.tasks[name] = new Task({ name: name })
+ }
+ return this.tasks[name]
+}
+
+var debug;
+if (process.env.NODE_DEBUG && /cluster/.test(process.env.NODE_DEBUG)) {
+ debug = function(x) {
+ var prefix = process.pid + ',' + (process.env.NODE_WORKER_ID ? 'Worker' : 'Master');
+ console.error(prefix, x);
+ };
+} else {
+ debug = function() { };
+}
105 lib/task.js
@@ -0,0 +1,105 @@
+/*!
+ * mixture
+ * Copyright(c) 2011 Daniel D. Shaw <dshaw@dshaw.com>
+ * MIT Licensed
+ */
+
+/**
+ * Module dependencies.
+ */
+
+var assert = require('assert')
+ , fork = require('fork')
+ , util = require('util')
+ , EventEmitter = require('events').EventEmitter
+
+/**
+ * Exports
+ */
+
+exports = module.exports = Task;
+
+/**
+ * Cluster-like debug handler
+ */
+
+var debug;
+if (process.env.NODE_DEBUG && /cluster/.test(process.env.NODE_DEBUG)) {
+ debug = function(x) {
+ var prefix = process.pid + ',' + (process.env.NODE_WORKER_ID ? 'Worker' : 'Master');
+ console.error(prefix, x);
+ };
+} else {
+ debug = function() { };
+}
+
+/**
+ * Task
+ *
+ * @param options
+ * @api public
+ */
+function Task (options) {
+ this.master = options.master
+ this.name = options.name
+ this.filename = options.filname
+ this.args = options.args
+ this.options = options.options
+ this.workers = []
+ this.initiazed = false
+}
+
+/**
+ * Inherit EventEmitter
+ */
+
+util.inherits(Task, EventEmitter);
+
+/**
+ * fork it real good. dun duh duh dun duh duh dunna dunaa.
+ *
+ * @param filename
+ * @param args
+ * @param options
+ */
+
+Task.prototype.fork = function(filename, args, options) {
+ var self = this
+ , id = ++this.master.ids
+ , envCopy = {}
+ , options = {}
+
+ for (var x in process.env) {
+ envCopy[x] = process.env[x]
+ }
+
+ // Node's `NODE_WORKER_ID` has too much behavior attached to it.
+ envCopy['MIXTURE_WORKER_ID'] = id
+
+ for (var x in this.options) {
+ options[x] = this.options[x]
+ }
+
+ options.env = envCopy
+
+ var worker = fork(this.filename, this.args, this.options)
+ workers.push(worker)
+ master.workers.push(workers)
+
+ worker.on('message', function(message) {
+ self.master.onWorkerMessage(worker, self, message)
+ });
+
+ worker.on('exit', function() {
+ debug('worker id=' + id + ' died')
+ delete this.workers[id]
+ delete workers[id]
+ self.master.emit('death', worker, self)
+ });
+
+ this.master.emit('online', worker, self)
+ this.initialized = true
+
+ return this
+}
+
6 package.json
@@ -0,0 +1,6 @@
+{
+ "name": "mixture"
+ , "version": "0.0.0"
+ , "description": "Heterogeneous Cluster"
+ , "author": "Daniel D. Shaw <dshaw@dshaw.com> (http://dshaw.com)"
+}
Please sign in to comment.
Something went wrong with that request. Please try again.