diff --git a/plugins/architect.cluster/README.md b/plugins/architect.cluster/README.md new file mode 100644 index 0000000..9619e52 --- /dev/null +++ b/plugins/architect.cluster/README.md @@ -0,0 +1,23 @@ +`architect.cluster` +=================== + +This plugin wraps the node.js cluster API as an architect plugin. + +Usage +----- + +In your architect config: + + { + packagePath: "architect/plugins/architect.cluster", + pluginBasePath: __dirname + "../plugins", + numWorkers: 16, + workerConfig: require("./worker"), + // This one is optional: + masterConfig: require("./master") + } + +This will spin up 16 worker processes and one master process that manages those +workers, if you specify `masterConfig`, this architect config will be loaded for +the cluster master process. The `pluginBasePath` should point to the root path +of your plugin directory to make plugins with relative paths ("./bla.bla") work. \ No newline at end of file diff --git a/plugins/architect.cluster/cluster-plugin.js b/plugins/architect.cluster/cluster-plugin.js new file mode 100644 index 0000000..608eb46 --- /dev/null +++ b/plugins/architect.cluster/cluster-plugin.js @@ -0,0 +1,40 @@ +var assert = require("assert"); +var cluster = require("cluster"); +var architect = require("../../architect"); + +module.exports = function startup(options, imports, register) { + assert(options.pluginBaseDir, "Option 'pluginBaseDir' is required"); + assert(options.workerConfig, "Option 'workerConfig' is required"); + assert(options.numWorkers, "Option 'numWorkers' is required"); + + var numWorkers = options.numWorkers; + + if (cluster.isMaster) { + // Fork each worker onto its own thread + for (var i = 0; i < numWorkers; i++) { + cluster.fork(); + } + + // When a worker dies, fork a new one + cluster.on('death', function(worker) { + cluster.fork(); + }); + + if (options.masterConfig) { + var plugins = architect.resolveConfig(options.masterConfig, options.pluginBaseDir); + architect.createApp(plugins, onCreateApp); + } + } + else { + // If the worker is not the master process, run the worker config + var plugins = architect.resolveConfig(options.workerConfig, options.pluginBaseDir); + architect.createApp(plugins, onCreateApp); + } + + function onCreateApp(err) { + if (err) + throw err; + } + + register(null, {}); +}; diff --git a/plugins/architect.cluster/package.json b/plugins/architect.cluster/package.json new file mode 100644 index 0000000..7cc3683 --- /dev/null +++ b/plugins/architect.cluster/package.json @@ -0,0 +1,6 @@ +{ + "name": "architect.cluster", + "version": "0.0.1", + "main": "cluster-plugin.js", + "plugin": {} +} \ No newline at end of file