2015-07-05 20:35:57 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Steppy = require('twostep').Steppy,
|
|
|
|
_ = require('underscore'),
|
|
|
|
Distributor = require('./lib/distributor').Distributor,
|
|
|
|
db = require('./db'),
|
2015-10-04 16:57:53 +00:00
|
|
|
logger = require('./lib/logger')('distributor');
|
2015-07-05 20:35:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
exports.init = function(app, callback) {
|
|
|
|
var distributor = new Distributor({
|
|
|
|
nodes: app.config.nodes,
|
|
|
|
projects: app.projects,
|
|
|
|
saveBuild: function(build, callback) {
|
|
|
|
Steppy(
|
|
|
|
function() {
|
2015-11-19 17:28:51 +00:00
|
|
|
if (_(build.project).has('avgBuildDuration')) {
|
2015-07-28 20:40:26 +00:00
|
|
|
this.pass(build.project.avgBuildDuration);
|
2015-11-19 17:28:51 +00:00
|
|
|
} else {
|
2016-01-07 08:25:06 +00:00
|
|
|
app.builds.getProjectAvgBuildDuration({
|
|
|
|
projectName: build.project.name
|
|
|
|
}, this.slot());
|
2015-07-28 20:40:26 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
function(err, avgBuildDuration) {
|
|
|
|
build.project.avgBuildDuration = avgBuildDuration;
|
|
|
|
|
2015-07-05 20:35:57 +00:00
|
|
|
db.builds.put(build, this.slot());
|
|
|
|
},
|
|
|
|
function() {
|
|
|
|
this.pass(build);
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
2015-11-24 22:41:20 +00:00
|
|
|
},
|
|
|
|
removeBuild: function(build, callback) {
|
|
|
|
Steppy(
|
|
|
|
function() {
|
|
|
|
db.builds.del([build.id], this.slot());
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
2015-07-05 20:35:57 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-01-06 21:11:14 +00:00
|
|
|
distributor.on('buildLogLines', function(build, lines) {
|
2015-11-19 17:20:35 +00:00
|
|
|
// write build logs to db
|
2015-12-02 21:27:09 +00:00
|
|
|
db.logLines.put(lines, function(err) {
|
2015-11-23 19:27:57 +00:00
|
|
|
if (err) {
|
|
|
|
logger.error(
|
|
|
|
'Error during write log line "' + logLineNumber +
|
|
|
|
'" for build "' + build.id + '":',
|
|
|
|
err.stack || err
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2015-07-05 20:35:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
callback(null, distributor);
|
|
|
|
};
|