I have a SailsJs (http://sailsjs.org/) based application that has to deal with some CPU intensive tasks. In short, I want to use the cluster
(https://nodejs.org/api/cluster.html) module to delegate the processing of these tasks to worker processes so that the main event loop of the Sails application isn't blocked (and so can respond to requests as normal).
When creating a worker, I'm getting an EADDRINUSE
error as Sails is trying to run again and bind to the same port.
Sample code:
// SomeSailsService.js
var cluster = require('cluster');
var Queue = require('bull');
var myQueue = Queue('myQueue', 'connection stuff', 'etc');
var numWorkers = 2;
var i;
if (cluster.isMaster) {
// Spawn some workers
for (i = 0; i < numWorkers; i++) {
cluster.fork();
}
} else {
// This is a worker, so bind to new job event
myQueue.process(function processJob(job, done) {
// CPU intensive shenanigans
});
}
module.exports = {
addToQueue: function(foo) {
myQueue.add({foo: foo});
}
};
When running the above, the Sails app starts up, then on starting the two workers tries to start the app another two times. This results in two of the following errors:
events.js:72
throw er; // Unhandled 'error' event
^
Error: bind EADDRINUSE
at errnoException (net.js:904:11)
at net.js:1084:30
at Object.1:1 (cluster.js:594:5)
at handleResponse (cluster.js:171:41)
at respond (cluster.js:192:5)
at handleMessage (cluster.js:202:5)
at process.emit (events.js:117:20)
at handleMessage (child_process.js:322:10)
at child_process.js:396:7
at process.handleConversion.net.Native.got (child_process.js:91:7)
at process.<anonymous> (child_process.js:395:13)
at process.emit (events.js:117:20)
at handleMessage (child_process.js:322:10)
at Pipe.channel.onread (child_process.js:347:11)
Is there a way around this? Or an alternative way of tackling this problem?