Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Node Cluster on windows?

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);
}
like image 420
wayofthefuture Avatar asked Aug 31 '16 15:08

wayofthefuture


People also ask

How do I start a node cluster?

The Start-ClusterNode cmdlet starts the Cluster service on a node in a failover cluster. If this is the first node started, then it will wait for other nodes to join. The cluster will begin to run when a quorum has formed.

How do I start Windows Cluster service?

To start / stop all services (cluster services and servers), right-click on the Services Status node in the left pane and select Start all services or Stop all services.

How do I add a new node to a Windows cluster?

To add a node to an existing failover cluster instance, click Installation in the left-hand pane. Then, select Add node to a SQL Server failover cluster. The System Configuration Checker will run a discovery operation on your computer. To continue, select OK.


3 Answers

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"

like image 101
Vibhaj Avatar answered Oct 13 '22 23:10

Vibhaj


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.

like image 45
Sachin Avatar answered Oct 14 '22 00:10

Sachin


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

like image 37
Blighty Avatar answered Oct 14 '22 00:10

Blighty