Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Discovery.prototype.updateExisting = function(name, data, interval, available,
rinfo) {
// this is an existing entry
var oldAvail = this.services[name].available;
// update the lanst announcement time to now
this.services[name].interval = interval;
this.services[name].data = data;
// if there is an rinfo, copy it and place it on the service
// we don't need the size parameter, though.
if (is.obj(rinfo) && is.str(rinfo.address) && !this.services[name].addr)
this.services[name].addr = rinfo.address;
// if the availability changed, send an event
if (available !== oldAvail) {
this.services[name].available = available;
var evName = available ? 'available' : 'unavailable';
this.emit(evName, name, this.services[name], 'availabilityChange');
}
return true;
};
Worker.prototype.runGithub = function(task, cb) {
if (!is.nonEmptyObj(task))
return asyncerr(new Error('No task received.'), cb);
if (!is.str(task.user))
return asyncerr(new Error('Task has no user: '+ inspect(task)), cb);
if (!is.str(task.repo))
return asyncerr(new Error('Task has no repo: '+ inspect(task)), cb);
if (!is.str(task.dir))
return asyncerr(new Error('Task has no dir: '+ inspect(task)), cb);
if (!is.obj(task.cmds))
return asyncerr(new Error('Task has no cmds: '+inspect(task)), cb);
// get the dir and make the directory
var dir = path.dirname(task.fileName);
var output = { stdout: '', stderr: '' };
var origCwd = process.cwd();
var targetDir = path.join(dir, task.repo);
var runGithub = require('./WorkerRunGithub');
async.series([
function RoleBase(role) {
var self = this;
// check the role parameter for correctness
if (!is.str(role)) return debug('Bad type for role param: '+typeof role);
if (role !== 'worker' && role !== 'master')
return debug('RoleBase contructor, bad role: '+role+', returning.');
self.role = role; // our role in this network.
self.nodes = {}; // storage for storage of discovered nodes
self.nodes.worker = {}; // local storage for all discovered workers
self.nodes.master = {}; // local storage for all discovered masters
// The data we announce to all the other nodes.
self.data = {
role: self.role,
port: self.port,
restApiPort: self.restApiPort
};
// create a REST API upon which, we can receive commands
Worker.prototype.runGithub = function(task, cb) {
if (!is.nonEmptyObj(task))
return asyncerr(new Error('No task received.'), cb);
if (!is.str(task.user))
return asyncerr(new Error('Task has no user: '+ inspect(task)), cb);
if (!is.str(task.repo))
return asyncerr(new Error('Task has no repo: '+ inspect(task)), cb);
if (!is.str(task.dir))
return asyncerr(new Error('Task has no dir: '+ inspect(task)), cb);
if (!is.obj(task.cmds))
return asyncerr(new Error('Task has no cmds: '+inspect(task)), cb);
// get the dir and make the directory
var dir = path.dirname(task.fileName);
var output = { stdout: '', stderr: '' };
var origCwd = process.cwd();
var targetDir = path.join(dir, task.repo);
var runGithub = require('./WorkerRunGithub');
async.series([
// the following functions are in ./WorkerRunGithub.js
async.apply(runGithub.makedir, dir),
async.apply(runGithub.chdir, dir),
Worker.prototype.runGithub = function(task, cb) {
if (!is.nonEmptyObj(task))
return asyncerr(new Error('No task received.'), cb);
if (!is.str(task.user))
return asyncerr(new Error('Task has no user: '+ inspect(task)), cb);
if (!is.str(task.repo))
return asyncerr(new Error('Task has no repo: '+ inspect(task)), cb);
if (!is.str(task.dir))
return asyncerr(new Error('Task has no dir: '+ inspect(task)), cb);
if (!is.obj(task.cmds))
return asyncerr(new Error('Task has no cmds: '+inspect(task)), cb);
// get the dir and make the directory
var dir = path.dirname(task.fileName);
var output = { stdout: '', stderr: '' };
var origCwd = process.cwd();
var targetDir = path.join(dir, task.repo);
Worker.prototype.runFile = function(task, cb) {
debug('runFile task: '+inspect(task, {colors:true, depth:null}));
// ensure task is an object
if (!is.nonEmptyObj(task))
return asyncerr(new Error('No task received.'), cb);
// ensure the task has a filename
if (!is.str(task.fileName))
return asyncerr(new Error('Task has no fileName: '+inspect(task)), cb);
try {
// convert array representing a buffer into a string
if (is.array(task.file))
task.file = new Buffer(task.file);
} catch(err) {
return asyncerr(err, cb);
}
// ensure we have a string representing the file
if (!is.buffer(task.file))
return asyncerr(new Error('Task has no task.file: '+
inspect(task.file)), cb);
if (!is.array(task.args))
Master.prototype.taskResult = function(req, res, taskResult) {
res.write('{"success":true}', 'utf8');
res.end();
if (!taskResult.task || !is.str(taskResult.task.type))
debug('Master.taskResult missing task information: '+inspect(taskResult));
if (!taskResult.worker || !is.obj(taskResult.worker) ||
(!taskResult.err && !taskResult.result)) {
debug('Master.taskResult missing information: '+inspect(taskResult));
}
this.emit('workerTaskComplete', taskResult);
};
exports.spawnChild = function(task, dir, output, cb) {
if (!is.obj(task) || !is.str(task.fileName))
return cb(new Error('Bad task object: '+inspect(task)));
if (!is.nonEmptyStr(dir))
return cb(new Error('Bad dir object: '+inspect(task)));
if (!is.obj(output) || !is.str(output.stdout) || !is.str(output.stderr))
return cb(new Error('Bad output object: '+inspect(output)));
var args = task.args;
args.unshift(task.fileName);
var child = spawn('node', args, {cwd:dir, env:process.env});
debug('Spawned child process to run file.');
child.stdout.on('data', function (data) {
output.stdout += data;
debug('stdout: ' + data);
exports.spawnChild = function(task, dir, output, cb) {
if (!is.obj(task) || !is.str(task.fileName))
return cb(new Error('Bad task object: '+inspect(task)));
if (!is.nonEmptyStr(dir))
return cb(new Error('Bad dir object: '+inspect(task)));
if (!is.obj(output) || !is.str(output.stdout) || !is.str(output.stderr))
return cb(new Error('Bad output object: '+inspect(output)));
var args = task.args;
args.unshift(task.fileName);
var child = spawn('node', args, {cwd:dir, env:process.env});
debug('Spawned child process to run file.');
child.stdout.on('data', function (data) {
output.stdout += data;
debug('stdout: ' + data);
});
child.stderr.on('data', function (data) {
output.stderr += data;
debug('stderr: ' + data);
});