Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function waitUntilUsed(port, retryTimeMs, timeOutMs) {
// the first arument may be an object, if it is not, make an object
var opts;
if (is.obj(port)) {
opts = port;
opts.host = '127.0.0.1';
opts.inUse = true;
} else {
opts = makeOptionsObj(port, '127.0.0.1', true, retryTimeMs, timeOutMs);
}
return waitUntilUsedOnHost(opts);
}
function Discovery(options) {
var self = this;
if (options && !is.obj(options))
debug('Dicovery constructor bad options argument: '+inspect(options));
// Create a dgram socket and bind it
self.dgramType = (options && options.dgramType) ?
options.dgramType.toLowerCase() : DEFAULT_DGRAM_TYPE;
self.reuseaddr = (options && options.reuseaddr) ? options.reuseaddr : DEFAULT_REUSE_ADDR;
self.socket = dgram.createSocket({type: self.dgramType, reuseAddr: self.reuseaddr});
self.port = (options && options.port) ? options.port : DEFAULT_UDP_PORT;
self.bindAddr = (options && options.bindAddr) ? options.bindAddr :
undefined;
self.socket.bind(self.port, self.bindAddr);
// create an interval task to check for announcements that have timed out
self.timeOutInt = (options && options.timeOutInt) ? options.timeOutInt :
DEFAULT_TIMEOUT;
self.timeOutId = setInterval(function() { self.handleTimeOut(); },
self.RestApi.addRoute('post', '/addTask', function(req, res, taskInfo) {
if (!is.obj(taskInfo) || !is.obj(taskInfo.master)) {
debug('self.RestApi.addRoute received bad taskInfo:', taskInfo);
return;
}
// send reply to client - we got the task - the rest of the work will be
// done asynchronously from here. We'll tell the client how it went via
// a REST call.
res.json({success:true});
// create easy to use return address
var address = req.socket.address().address + ':' +
taskInfo.master.restApiPort;
// process the task on the next tick
process.nextTick(function() {
if (is.obj(taskInfo) && is.obj(taskInfo.master) &&
function waitForStatus(port, host, inUse, retryTimeMs, timeOutMs) {
var deferred = getDeferred();
var timeoutId;
var timedout = false;
var retryId;
// the first arument may be an object, if it is not, make an object
var opts;
if (is.obj(port)) {
opts = port;
} else {
opts = makeOptionsObj(port, host, inUse, retryTimeMs, timeOutMs);
}
//debug('opts:'+util.inspect(opts);
if (!is.bool(opts.inUse)) {
deferred.reject(new Error('inUse must be a boolean'));
return deferred.promise;
}
if (!is.positiveInt(opts.retryTimeMs)) {
opts.retryTimeMs = RETRYTIME;
debug('set retryTime to default '+RETRYTIME+'ms');
}
function waitUntilFreeOnHost(port, host, retryTimeMs, timeOutMs) {
// the first arument may be an object, if it is not, make an object
var opts;
if (is.obj(port)) {
opts = port;
opts.inUse = false;
} else {
opts = makeOptionsObj(port, host, false, retryTimeMs, timeOutMs);
}
return waitForStatus(opts);
}
process.nextTick(function() {
if (is.obj(taskInfo) && is.obj(taskInfo.master) &&
is.obj(taskInfo.task) && is.nonEmptyStr(taskInfo.task.type)) {
self.emit('taskReceived', taskInfo.master, taskInfo.task);
self.doTask(taskInfo, address);
} else {
debug('/addTask bad taskInfo:',taskInfo);
}
});
});
Worker.prototype.doTask = function(taskInfo, address) {
debug('Worker.doTask taskInfo: '+inspect(taskInfo));
var self = this;
var cbFunc = function(err, result) {
debug('cbFunc result: '+inspect(result));
self.taskReply(err, taskInfo, address, result);
};
if (!is.obj(taskInfo) || !is.obj(taskInfo.task) ||
!is.nonEmptyStr(taskInfo.task.type)) {
debug('Bad task Info received in Worker.doTask: '+inspect(taskInfo));
return;
}
switch(taskInfo.task.type) {
case 'function':
self.runFunc(taskInfo.task, cbFunc);
break;
case 'file':
self.runFile(taskInfo.task, cbFunc);
break;
case 'github':
self.runGithub(taskInfo.task, cbFunc);
break;
default:
process.nextTick(function() {
if (is.func(task)) {
self.sendFuncToWorker(worker, task, args, cb);
} else if (is.obj(task)) {
switch(task.type) {
case 'github':
self.sendGithubToWorker(worker, task, cb);
break;
case 'file':
self.sendFileToWorker(worker, task, cb);
break;
default:
debug('Unknown task.type in task: '+inspect(task));
cb(new Error('Unknown task.type in task: '+inspect(task)));
break;
}
}
});
};