Skip to content
This repository has been archived by the owner on Jul 21, 2022. It is now read-only.

Commit

Permalink
refactor to not use net-cluster and underscore
Browse files Browse the repository at this point in the history
  • Loading branch information
bjrmatos committed Jul 9, 2019
1 parent 5a4c9c3 commit b2350bb
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 158 deletions.
69 changes: 69 additions & 0 deletions lib/getRandomPort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
var net = require('net');

var DEFAULT_MIN = 1025;
var DEFAULT_MAX = 65535;
var DEFAULT_MAX_ATTEMPTS = 50;

function getRandomPort (opts, cb) {
var min = DEFAULT_MIN;
var max = DEFAULT_MAX;
var maxAttempts = DEFAULT_MAX_ATTEMPTS;
var options = opts || {};
var host;
var port;
var server;

if (options.min != null) {
min = options.min;
}

if (options.max != null) {
max = options.max;
}

if (options.maxAttempts != null) {
maxAttempts = options.maxAttempts;
}

if (options.host != null) {
host = options.host;
}

port = getRandomNumber(min, max);

server = net.createServer();

server.listen(port, host, function () {
server.once('close', function () { cb(null, port) });
server.close();
});

server.on('error', function () {
if (--maxAttempts) {
return getRandomPort({
min: min,
max: max,
maxAttempts: maxAttempts
}, cb);
}

cb(new Error('Could not find an available port'));
});
}

function getRandomNumber (minimum, maximum) {
if (maximum === undefined) {
maximum = minimum;
minimum = 0;
}

if (typeof minimum !== 'number' || typeof maximum !== 'number') {
throw new TypeError('Expected all arguments to be numbers');
}

return Math.floor(
(Math.random() * (maximum - minimum + 1)) + minimum
);
}

module.exports = getRandomPort;
11 changes: 5 additions & 6 deletions lib/phantomManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
* and distributing pdf rendering tasks to them.
*/

var events = require("events"),
util = require("util"),
_ = require("underscore"),
var events = require('events'),
util = require('util'),
find = require('lodash.find'),
numCPUs = require('os').cpus().length,
cluster = require("cluster"),
PhantomWorker = require("./phantomWorker.js");

var PhantomManager = module.exports = function (options) {
Expand Down Expand Up @@ -86,7 +85,7 @@ PhantomManager.prototype.kill = function() {
PhantomManager.prototype.execute = function (options, cb) {
var self = this;

var freePhantomInstance = _.findWhere(this._phantomInstances, {
var freePhantomInstance = find(this._phantomInstances, {
isBusy: false
});

Expand Down Expand Up @@ -146,7 +145,7 @@ PhantomManager.prototype.tryFlushQueue = function () {
if (this.tasksQueue.length === 0)
return;

var freePhantomInstance = _.findWhere(this._phantomInstances, {
var freePhantomInstance = find(this._phantomInstances, {
isBusy: false
});

Expand Down
45 changes: 16 additions & 29 deletions lib/phantomWorker.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
var childProcess = require('child_process'),
cluster = require("cluster"),
cluster = require('cluster'),
http = require('http'),
netCluster = require("net-cluster"),
checkPortStatus = require("./checkPortStatus.js"),
portScanner = require('portscanner'),
checkPortStatus = require('./checkPortStatus'),
getRandomPort = require('./getRandomPort'),
objectAssign = require('object-assign');


var findFreePort = function (host, cb) {
var server = netCluster.createServer();
var port = 0;
server.on('listening', function () {
port = server.address().port;
server.close();
});
server.on('close', function () {
cb(null, port);
});
server.listen(0, host);
getRandomPort({
host: host
}, cb);
};

var findFreePortInRange = function (host, portLeftBoundary, portRightBoundary, cb) {

//in cluster we don't want ports to collide, so we make a special space for every worker assuming max number of cluster workers is 5
if (cluster.worker) {
portLeftBoundary = portLeftBoundary + (((portRightBoundary - portLeftBoundary) / 5) * (cluster.worker.id - 1));
}

portScanner.findAPortNotInUse(portLeftBoundary, portRightBoundary, host, function(error, port) {
cb(error, port);
})
getRandomPort({
min: portLeftBoundary,
max: portRightBoundary,
host: host
}, cb);
};


Expand Down Expand Up @@ -175,13 +162,13 @@ PhantomWorker.prototype.execute = function (options, cb) {
result += chunk;
});
res.on("end", function () {
self.isBusy = false;
self.isBusy = false;
try {
var parsedResult = result ? JSON.parse(result) : null
cb(null, parsedResult)
} catch (e) {
cb(new Error('Unparsable response from the phantomjs ' + result))
}
}
});
});

Expand All @@ -190,17 +177,17 @@ PhantomWorker.prototype.execute = function (options, cb) {
req.setHeader('Content-Length', Buffer.byteLength(json));
req.write(json);

req.on("error", function (e) {
req.on("error", function (e) {
// recycle phantomjs if it cannot accept connections to avoid getting it freezed in the invalid state
if (e.code === 'ECONNREFUSED' || e.code === 'ECONNRESET') {
return self.recycle(function (recycleError) {
self.isBusy = false;
cb(recycleError || e);
});
}
}

self.isBusy = false;
cb(e);
cb(e);
});

req.end();
Expand Down
Loading

0 comments on commit b2350bb

Please sign in to comment.