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
|
|
|
|
|
|
|
|
2016-02-29 21:11:58 +00:00
|
|
|
exports.create = function(app, callback) {
|
2015-07-05 20:35:57 +00:00
|
|
|
var distributor = new Distributor({
|
|
|
|
nodes: app.config.nodes,
|
|
|
|
projects: app.projects,
|
2016-01-10 16:37:19 +00:00
|
|
|
notifier: app.notifier,
|
2015-07-05 20:35:57 +00:00
|
|
|
saveBuild: function(build, callback) {
|
|
|
|
Steppy(
|
|
|
|
function() {
|
2015-11-19 17:28:51 +00:00
|
|
|
if (_(build.project).has('avgBuildDuration')) {
|
2016-01-07 19:02:59 +00:00
|
|
|
this.pass(null);
|
2015-11-19 17:28:51 +00:00
|
|
|
} else {
|
2016-01-07 19:02:59 +00:00
|
|
|
app.builds.getRecent({
|
|
|
|
projectName: build.project.name,
|
|
|
|
status: 'done',
|
|
|
|
limit: 10
|
2016-01-07 08:25:06 +00:00
|
|
|
}, this.slot());
|
2015-07-28 20:40:26 +00:00
|
|
|
}
|
|
|
|
},
|
2016-01-07 19:02:59 +00:00
|
|
|
function(err, doneBuilds) {
|
|
|
|
if (doneBuilds) {
|
|
|
|
build.project.avgBuildDuration = (
|
|
|
|
app.builds.getAvgBuildDuration(doneBuilds)
|
|
|
|
);
|
|
|
|
}
|
2015-07-28 20:40:26 +00:00
|
|
|
|
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);
|
|
|
|
};
|