Reputation: 6487
I am trying to terminate cluster workers when they are not needed to free up some resources. Here is the code I use in worker:
server.on("close", function() {
process.exit();
});
server.close();
This code is executed when the worker gets the command from master. The "close" callback IS called, but I can't access any ports that the workers were using. So I have this code
var server = require('http').createServer(function(req, res) {
res.writeHead(200);
res.end('');
});
server.listen(8080);
And if I restart the worker after it exits, I get 'EADDRINUSE' for the listen() on 8080 port.
Upvotes: 0
Views: 877
Reputation: 6487
It turns out Socket.io (what is getting listening on the port) is not releasing the port until it is terminated. It is done in order to allow port-reuse. I was able to re-create a socket.io instance using the exactly same port as was used before. So if I use port 8080 for socket.io connection to shopping cart and port 8081 for socket responsible for the recommendations, I would have to call listen(8080)
the next time I create the shopping cart socket instance.
I wanted to use different ports for different connections at different times. I was trying to announce the connections as they were available, but I guess I will have to hardcode the ports for different services now. At least the EADDRINUSE does not show up any more.
Upvotes: 0
Reputation: 51
Please run this script to kill node related process. and run your code again
(function(){
"use strict";
var exec = require('child_process').exec,
lCmd = 'kill -9' + ' ' + /* kill finded process */
'`ps ax' + '|' + /* show all process */
'grep node-openshift' + '|' + /* find node-openshift */
'grep -v grep' + '|' + /* exlude grep command */
'awk "{print $1}"`'; /* show first collumn */
exec(lCmd, function(error, stdout, stderr){
console.log(error || stdout || stderr);
});
})();
Upvotes: 1