nci/lib/distributor.js

182 lines
4.0 KiB
JavaScript
Raw Normal View History

2015-04-09 18:55:29 +00:00
'use strict';
var Steppy = require('twostep').Steppy,
_ = require('underscore'),
2015-05-10 10:04:54 +00:00
Node = require('./node').Node,
EventEmitter = require('events').EventEmitter,
2015-05-21 21:09:16 +00:00
inherits = require('util').inherits,
notifier = require('./notifier');
2015-04-09 18:55:29 +00:00
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
2015-05-10 10:04:54 +00:00
self.saveBuild = params.saveBuild || function(build, callback) {
callback(null, build);
2015-04-09 19:39:22 +00:00
};
2015-06-13 18:59:32 +00:00
self.projectsHash = _(params.projects).indexBy(function(project) {
return project.config.name;
});
2015-04-09 18:55:29 +00:00
}
2015-05-10 10:04:54 +00:00
inherits(Distributor, EventEmitter);
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.splice(queueItemIndex, 1)[0];
2015-04-09 18:55:29 +00:00
2015-05-10 10:04:54 +00:00
self._updateBuild(
queueItem.build,
{startDate: Date.now(), status: 'in-progress'},
this.slot()
);
2015-04-09 18:55:29 +00:00
},
function(err, node, build) {
2015-04-09 18:55:29 +00:00
var stepCallback = this.slot();
var executor = node.run(build.project, build.params, function(err) {
2015-05-10 10:04:54 +00:00
self._updateBuild(
build,
{
endDate: Date.now(),
status: err ? 'error' : 'done',
2015-05-21 19:04:38 +00:00
completed: true,
error: err ? err.message : null
2015-05-10 10:04:54 +00:00
},
function(err, build) {
2015-06-13 18:59:32 +00:00
self._onBuildComplete(err, build, stepCallback)
2015-05-10 10:04:54 +00:00
}
);
2015-04-09 18:55:29 +00:00
});
2015-05-20 20:20:51 +00:00
executor.on('currentStep', function(stepName) {
self._updateBuild(build, {currentStep: stepName});
2015-05-05 23:11:28 +00:00
});
executor.on('data', function(data) {
2015-05-12 19:53:04 +00:00
self.emit('buildData', build, data);
});
2015-05-09 16:59:27 +00:00
executor.once('scmData', function(scmData) {
2015-05-10 10:04:54 +00:00
self._updateBuild(build, {scm: scmData});
2015-05-09 16:59:27 +00:00
});
2015-04-09 18:55:29 +00:00
},
callback
);
};
2015-06-13 18:59:32 +00:00
Distributor.prototype._onBuildComplete = function(err, build, callback) {
var self = this;
Steppy(
function() {
// process after build triggers
var triggerAfterGroup = this.makeGroup();
var after = build.project.trigger && build.project.trigger.after;
if (after) {
_(after).each(function(item) {
if (!item.status || item.status === build.status) {
self.run(item.project, {}, triggerAfterGroup.slot());
}
});
}
},
function(err, triggerAfterGroupResults) {
// notify about build
notifier.send(build);
// try to run next project from the queue
self._runNext(this.slot());
},
callback
);
};
2015-05-10 10:04:54 +00:00
Distributor.prototype._updateBuild = function(build, changes, callback) {
var self = this;
2015-05-05 23:11:28 +00:00
callback = callback || _.noop;
2015-05-17 10:26:28 +00:00
var isWithNumber = Boolean(build.number);
2015-05-10 10:04:54 +00:00
Steppy(
function() {
_(build).extend(changes);
// skip saving to db of unimportant data
if (changes.currentStep && _(changes).keys().length === 1) {
this.pass(null);
} else {
self.saveBuild(build, this.slot());
}
},
function() {
2015-05-17 10:26:28 +00:00
// if number appear after save to db then add it to changes
// TODO: might be better to generate number right there (instead
// of hooks)
if (!isWithNumber && build.number) {
changes.number = build.number;
}
2015-05-10 10:04:54 +00:00
// emits only after get an id (at save build)
self.emit('buildUpdate', build, changes);
this.pass(build);
},
callback
);
2015-04-09 18:55:29 +00:00
};
2015-06-13 18:59:32 +00:00
Distributor.prototype.run = function(projectName, params, callback) {
var self = this,
project;
2015-04-09 18:55:29 +00:00
Steppy(
function() {
2015-06-13 18:59:32 +00:00
project = self.projectsHash[projectName].config;
2015-05-10 10:04:54 +00:00
self._updateBuild({}, {
2015-04-09 18:55:29 +00:00
project: project,
params: params,
createDate: Date.now(),
2015-05-21 19:04:38 +00:00
status: 'queued',
completed: false
2015-04-09 18:55:29 +00:00
}, this.slot());
},
function(err, build) {
self.queue.push({project: project, build: build});
self._runNext(this.slot());
},
callback
);
};