12

Anyone know how to run Node Cluster on windows? I haven't been able to find any articles on the web and cannot seem to solve this problem:

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: write ENOTSUP
    at exports._errnoException (util.js:1007:11)
    at ChildProcess.target._send (internal/child_process.js:634:20)
    at ChildProcess.target.send (internal/child_process.js:521:19)
    at sendHelper (cluster.js:751:15)
    at send (cluster.js:534:12)
    at cluster.js:509:7
    at SharedHandle.add (cluster.js:99:3)
    at queryServer (cluster.js:501:12)
    at Worker.onmessage (cluster.js:449:7)
    at ChildProcess.<anonymous> (cluster.js:765:8)

And the code...

if (cluster.isMaster) {
    for (let i = 0; i < numCPUs; i++) {
        cluster.fork();
    }
    cluster.on('online', (worker) => {
        console.log('Worker ' + worker.process.pid + ' is online');
    });
    cluster.on('exit', (worker, code, signal) => {
        console.log(`Worker ${worker.process.pid} died with code ${code} and signal ${signal}`);
    });
} else {
    console.log('else part ');
    openPort();
}

function openPort() {
    let server = dgram.createSocket('udp4');
    server.bind(port, host);
    server.on('message', processMessage);
}
wayofthefuture
  • 8,339
  • 7
  • 36
  • 53
  • This code works for me - where is the rest of your code and what is your Node.js version. – AntonB Aug 31 '16 at 15:37
  • What version of node.js are you running. [This bug report](https://github.com/nodejs/node-v0.x-archive/issues/14382) suggests a bug on Windows in this area was fixed last year. I'm not sure what versions the fix was put into. – jfriend00 Aug 31 '16 at 17:09
  • Also, what does `openPort()` do? – jfriend00 Aug 31 '16 at 17:10
  • It looks like a root cause of this type of error when using clustering on Windows is use of a UDP socket in your clustering. Do you know if you're doing that or using a library that uses UDP? If so, it appears that there are some work-arounds by binding the UDP socket in an appropriate way. If you read [this discussion](https://github.com/nodejs/node-v0.x-archive/pull/8643), you get a lot more detail. – jfriend00 Aug 31 '16 at 17:20
  • Sorry for the delay in response. I edited the code to include the openPort function. Yes it is UDP. Also the Node version is 6.2.2. It's more important it works on Linux for production, wonder if there is a way to be compatible with both? @jfriend00 That link seems like they [fixed the problem](https://github.com/sivy/node-statsd/pull/58)? Thanks. – wayofthefuture Sep 01 '16 at 15:40
  • I don't know if they fixed it as I don't really understand the issue myself. You are using a pretty recent version of node.js so if they had fixed it, it seems like you wouldn't be still having the issue. I was under the impression that there might be some "config" of the UDP socket that might prevent the problem, but as I said, I don't really understand the technical details of the issue myself so I'm not sure. – jfriend00 Sep 01 '16 at 21:11
  • What is the name of this file ? – user2226755 Sep 08 '16 at 15:53

3 Answers3

2

Support for UDP clustering was added in v0.11.14 (for Linux and OSX). Check file on node.js master, which says "dgram clustering is currently not supported on windows"

Vibhaj
  • 189
  • 6
1

In the current node js version I am using below code to create cluster on windows.

var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }


  cluster.on('exit', function(worker, code, signal) {
    console.log("worker ${worker.process.pid} died");
    cluster.fork();
  });
} else {
  var express = require('express');
  var http = require('http');

  // init app
  var app = express();

  function createServer(app) {
    return http.createServer(app);
  }

  app.locals.server = createServer(app);

  app.locals.server.listen(port, function() {
    console.info("server online");
  });
}

This will create clusters on same port.

Sachin
  • 2,912
  • 16
  • 25
1

So, in order to use UDP with Node cluster on Windows, you have to call server.bind like this:

server.bind({port: 1900, exclusive: true}, function () {
        console.log('PORT BIND SUCCESS');
        server.setBroadcast(true);
        server.setMulticastTTL(128);
        server.addMembership(multicastAddress, myIp);
    });

The key part is to pass in the object {port: PORT, exclusive: true} to the bind function. I found the answer here: https://github.com/misterdjules/node/commit/1a87a95d3d7ccc67fd74145c6f6714186e56f571

Blighty
  • 199
  • 5
  • 20
  • To complement: Clustering on windows works fine as long as you dont bind to a UDP port. If you do so you must ensure that you specify the "exclusive" property as true just like @Blighty suggested. Also because each cluster will try to bind() to the same port only the first will successfully connect. The others will probably throw an error saying that the address is already in use (bind EADDRINUSE 0.0.0.0:5007). Handle the error and it should work just fine. – Lothre1 Jun 26 '17 at 12:21