nci/lib/node.js

109 lines
2.4 KiB
JavaScript
Raw Normal View History

2015-03-30 21:06:08 +00:00
'use strict';
var _ = require('underscore'),
createExecutor = require('./executor').createExecutor;
function Node(params) {
this.type = params.type;
this.maxExecutorsCount = params.maxExecutorsCount;
this.executors = {};
}
exports.Node = Node;
2015-06-15 19:16:46 +00:00
Node.prototype._getBlockerExecutor = function(getBlockers, getTarget) {
return _(this.executors).find(function(executor) {
var target = getTarget(executor);
return _(getBlockers(executor)).find(function(blocker) {
if (_(blocker).isRegExp()) {
return blocker.test(target);
} else {
return blocker === target;
}
});
})
};
2015-06-14 23:27:58 +00:00
Node.prototype.getExecutorWaitReason = function(project) {
var waitReason;
if (_(this.executors).size() >= this.maxExecutorsCount) {
waitReason = 'All executors are busy';
} else if (project.name in this.executors) {
waitReason = 'Project already running on node';
2015-06-15 19:16:46 +00:00
} else {
var blockerExecutor;
if (project.blockedBy) {
blockerExecutor = this._getBlockerExecutor(
function(executor) {
return project.blockedBy;
},
function(executor) {
return executor.project.name;
}
);
}
if (!blockerExecutor) {
blockerExecutor = this._getBlockerExecutor(
function(executor) {
return executor.project.blocks;
},
function(executor) {
return project.name;
}
);
}
if (blockerExecutor) {
waitReason = (
'Blocked by currently running "' +
blockerExecutor.project.name + '"'
);
}
2015-06-14 23:27:58 +00:00
}
return waitReason;
};
2015-03-30 21:06:08 +00:00
Node.prototype.hasFreeExecutor = function(project) {
2015-06-14 23:27:58 +00:00
return !this.getExecutorWaitReason(project);
};
2015-03-30 21:06:08 +00:00
Node.prototype.getFreeExecutorsCount = function() {
return this.maxExecutorsCount - _(this.executors).size();
};
2015-03-30 21:06:08 +00:00
Node.prototype._createExecutor = function(project) {
return createExecutor({
type: this.type,
project: project
});
};
Node.prototype.hasScmChanges = function(project, callback) {
this._createExecutor(project).hasScmChanges(callback);
};
2015-03-30 21:06:08 +00:00
Node.prototype.run = function(project, params, callback) {
var self = this;
2015-06-14 23:27:58 +00:00
var waitReason = this.getExecutorWaitReason(project);
if (waitReason) {
throw new Error(
'Project "' + project.name + '" should wait because: ' + waitReason
);
2015-03-30 21:06:08 +00:00
}
this.executors[project.name] = this._createExecutor(project);
this.executors[project.name].run(params, function(err) {
delete self.executors[project.name];
callback(err);
});
return this.executors[project.name];
2015-03-30 21:06:08 +00:00
};