Skip to content

Commit

Permalink
Added example of clustering. [Closes #8]
Browse files Browse the repository at this point in the history
  • Loading branch information
rschmukler committed Jan 23, 2014
1 parent 2f89c3d commit 872c76f
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions README.md
Expand Up @@ -434,6 +434,67 @@ Ultimately if enough people want a Redis driver instead of Mongo, I will write
one. (Please open an issue requesting it). For now, Agenda decided to focus on
guaranteed persistence.

### Spawning / forking processes.

Ultimately Agenda can work from a single job queue across multiple machines, node processes, or forks. If you are interested in having more than one worker, @Bars3s has written up a fantastic example of how one might do it:

```js
var cluster = require('cluster'),
cpuCount = require('os').cpus().length,
jobWorkers = [],
webWorkers = [];

if (cluster.isMaster) {

// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
addJobWorker();
addWebWorker();
}

cluster.on('exit', function (worker, code, signal) {

if (jobWorkers.indexOf(worker.id) != -1) {
console.log('job worker ' + worker.process.pid + ' died. Trying to respawn...');
removeJobWorker(worker.id);
addJobWorker();
}

if (webWorkers.indexOf(worker.id) != -1) {
console.log('http worker ' + worker.process.pid + ' died. Trying to respawn...');
removeWebWorker(worker.id);
addWebWorker();
}
});

} else {
if (process.env.web) {
console.log('start http server: ' + cluster.worker.id);
require('./app/web-http');//initialize the http server here
}

if (process.env.job) {
console.log('start job server: ' + cluster.worker.id);
require('./app/job-worker');//initialize the agenda here
}
}

function addWebWorker() {
webWorkers.push(cluster.fork({web: 1}).id);
}

function addJobWorker() {
webWorkers.push(cluster.fork({job: 1}).id);
}

function removeWebWorker(id) {
webWorkers.splice(webWorkers.indexOf(id), 1);
}

function removeJobWorker(id) {
jobWorkers.splice(jobWorkers.indexOf(id), 1);
}
```



Expand Down

0 comments on commit 872c76f

Please sign in to comment.