nci/lib/distributor.js

93 lines
2.0 KiB
JavaScript
Raw Normal View History

2015-04-09 18:55:29 +00:00
'use strict';
var Steppy = require('twostep').Steppy,
_ = require('underscore'),
Node = require('./node').Node;
function Distributor(params) {
var self = this;
// nodes to execute builds
self.nodes = _(params.nodes).map(function(nodeParams) {
return self._createNode(nodeParams);
});
// queued projects to build
self.queue = [];
2015-04-09 19:39:22 +00:00
self.onBuildUpdate = params.onBuildUpdate || function(build, callback) {
callback(null, build);
2015-04-09 19:39:22 +00:00
};
2015-04-09 18:55:29 +00:00
}
exports.Distributor = Distributor;
Distributor.prototype._createNode = function(nodeParams) {
return new Node(nodeParams);
};
Distributor.prototype._runNext = function(callback) {
var self = this;
Steppy(
function() {
var node;
var queueItemIndex = _(self.queue).findIndex(function(item) {
node = _(self.nodes).find(function(node) {
return node.hasFreeExecutor(item.project);
});
return node;
});
// quit if we have no suitable project
if (queueItemIndex) {
return callback();
}
this.pass(node);
var queueItem = self.queue[queueItemIndex];
this.pass(queueItemIndex, queueItem);
queueItem.build.status = 'in-progress';
self._updateBuild(queueItem.build, this.slot());
},
function(err, node, queueItemIndex, queueItem, build) {
self.queue.splice(queueItemIndex, 1);
var stepCallback = this.slot();
var executor = node.run(queueItem.project, build.params, function(err) {
2015-04-09 18:55:29 +00:00
build.status = err ? 'error' : 'done';
2015-04-09 19:39:22 +00:00
build.error = err;
2015-04-09 18:55:29 +00:00
self._updateBuild(build, stepCallback);
});
executor.on('data', function(data) {
console.log('>>> executor`s data = ', data);
});
2015-04-09 18:55:29 +00:00
},
callback
);
};
Distributor.prototype._updateBuild = function(build, callback) {
2015-04-09 19:39:22 +00:00
this.onBuildUpdate(build, callback);
2015-04-09 18:55:29 +00:00
};
Distributor.prototype.run = function(project, params, callback) {
var self = this;
Steppy(
function() {
self._updateBuild({
project: project,
params: params,
status: 'waiting'
}, this.slot());
},
function(err, build) {
self.queue.push({project: project, build: build});
self._runNext(this.slot());
},
callback
);
};