Skip to content
This repository has been archived by the owner on Apr 6, 2021. It is now read-only.

Moved the cluster plugin from cloud9 and adapted it slightly #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions plugins/architect.cluster/README.md
Original file line number Diff line number Diff line change
@@ -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.
40 changes: 40 additions & 0 deletions plugins/architect.cluster/cluster-plugin.js
Original file line number Diff line number Diff line change
@@ -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, {});
};
6 changes: 6 additions & 0 deletions plugins/architect.cluster/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "architect.cluster",
"version": "0.0.1",
"main": "cluster-plugin.js",
"plugin": {}
}