2015-07-05 20:35:57 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Steppy = require('twostep').Steppy,
|
|
|
|
_ = require('underscore'),
|
|
|
|
Distributor = require('./lib/distributor').Distributor,
|
2015-07-28 20:40:26 +00:00
|
|
|
getAvgProjectBuildDuration = (
|
|
|
|
require('./lib/project').getAvgProjectBuildDuration
|
|
|
|
),
|
2015-07-05 20:35:57 +00:00
|
|
|
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 {
|
|
|
|
getAvgProjectBuildDuration(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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-11-19 17:28:51 +00:00
|
|
|
var buildDataResourcesHash = {};
|
2015-07-05 20:35:57 +00:00
|
|
|
|
|
|
|
// create resource for build data
|
2015-07-12 12:39:24 +00:00
|
|
|
var createBuildDataResource = function(buildId) {
|
|
|
|
if (buildId in buildDataResourcesHash) {
|
2015-07-05 20:35:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-07-12 12:39:24 +00:00
|
|
|
var buildDataResource = app.dataio.resource('build' + buildId);
|
2015-07-05 20:35:57 +00:00
|
|
|
buildDataResource.on('connection', function(client) {
|
2015-11-18 20:14:46 +00:00
|
|
|
var callback = this.async();
|
|
|
|
Steppy(
|
|
|
|
function() {
|
|
|
|
db.logLines.find({
|
2015-11-30 20:08:54 +00:00
|
|
|
start: {buildId: buildId},
|
2015-11-18 20:14:46 +00:00
|
|
|
}, this.slot());
|
|
|
|
},
|
|
|
|
function(err, lines) {
|
2015-12-02 21:27:09 +00:00
|
|
|
client.emit('sync', 'data', {lines: lines});
|
2015-11-18 20:14:46 +00:00
|
|
|
this.pass(true);
|
|
|
|
},
|
|
|
|
function(err) {
|
|
|
|
if (err) {
|
|
|
|
logger.error(
|
|
|
|
'error during read log for "' + buildId + '":',
|
|
|
|
err.stack || err
|
|
|
|
);
|
2015-07-05 20:35:57 +00:00
|
|
|
}
|
2015-11-18 20:14:46 +00:00
|
|
|
callback();
|
|
|
|
}
|
|
|
|
);
|
2015-07-05 20:35:57 +00:00
|
|
|
});
|
2015-07-12 12:39:24 +00:00
|
|
|
buildDataResourcesHash[buildId] = buildDataResource;
|
2015-07-05 20:35:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.createBuildDataResource = createBuildDataResource;
|
|
|
|
|
2015-11-24 22:41:20 +00:00
|
|
|
var buildsResource = app.dataio.resource('builds');
|
2015-07-05 20:35:57 +00:00
|
|
|
|
2015-11-24 22:41:20 +00:00
|
|
|
distributor.on('buildUpdate', function(build, changes) {
|
2015-07-05 20:35:57 +00:00
|
|
|
if (build.status === 'queued') {
|
2015-07-12 12:39:24 +00:00
|
|
|
createBuildDataResource(build.id);
|
2015-07-05 20:35:57 +00:00
|
|
|
}
|
|
|
|
|
2015-09-26 20:49:03 +00:00
|
|
|
// notify about build's project change, coz building affects project
|
|
|
|
// related stat (last build date, avg build time, etc)
|
|
|
|
if (changes.completed) {
|
|
|
|
var projectsResource = app.dataio.resource('projects');
|
|
|
|
projectsResource.clientEmitSyncChange({name: build.project.name});
|
|
|
|
}
|
|
|
|
|
2015-07-05 20:35:57 +00:00
|
|
|
buildsResource.clientEmitSync('change', {
|
|
|
|
buildId: build.id, changes: changes
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-11-24 22:41:20 +00:00
|
|
|
distributor.on('buildCancel', function(build) {
|
|
|
|
buildsResource.clientEmitSync('cancel', {buildId: build.id});
|
|
|
|
});
|
|
|
|
|
2015-12-02 21:27:09 +00:00
|
|
|
var buildLogLineNumbersHash = {},
|
|
|
|
lastLinesHash = {};
|
2015-10-03 14:14:41 +00:00
|
|
|
|
2015-07-05 20:35:57 +00:00
|
|
|
distributor.on('buildData', function(build, data) {
|
2015-12-02 21:27:09 +00:00
|
|
|
var cleanupText = function(text) {
|
|
|
|
return text.replace('\r', '');
|
|
|
|
};
|
|
|
|
|
|
|
|
var splittedData = data.split('\n'),
|
|
|
|
logLineNumber = buildLogLineNumbersHash[build.id] || 0;
|
|
|
|
|
|
|
|
lastLinesHash[build.id] = lastLinesHash[build.id] || '';
|
|
|
|
|
|
|
|
// if we don't have last line, so we start new line
|
|
|
|
if (!lastLinesHash[build.id]) {
|
|
|
|
logLineNumber++;
|
|
|
|
}
|
|
|
|
lastLinesHash[build.id] += _(splittedData).first();
|
|
|
|
|
|
|
|
var lines = [{
|
|
|
|
text: cleanupText(lastLinesHash[build.id]),
|
|
|
|
buildId: build.id,
|
|
|
|
number: logLineNumber
|
|
|
|
}];
|
|
|
|
|
|
|
|
if (splittedData.length > 1) {
|
|
|
|
// if we have last '' we have to take all except last
|
|
|
|
// this shown that string ends with eol
|
|
|
|
if (_(splittedData).last() === '') {
|
|
|
|
lastLinesHash[build.id] = '';
|
|
|
|
splittedData = _(splittedData.slice(1)).initial();
|
|
|
|
} else {
|
|
|
|
lastLinesHash[build.id] = _(splittedData).last();
|
|
|
|
splittedData = _(splittedData).tail();
|
|
|
|
}
|
|
|
|
|
|
|
|
lines = lines.concat(_(splittedData).map(function(line) {
|
|
|
|
return {
|
|
|
|
text: cleanupText(line),
|
|
|
|
buildId: build.id,
|
|
|
|
number: ++logLineNumber
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
}
|
2015-10-15 05:36:04 +00:00
|
|
|
|
2015-12-02 21:27:09 +00:00
|
|
|
buildLogLineNumbersHash[build.id] = logLineNumber;
|
2015-07-05 20:35:57 +00:00
|
|
|
app.dataio.resource('build' + build.id).clientEmitSync(
|
2015-10-15 05:36:04 +00:00
|
|
|
'data',
|
2015-12-02 21:27:09 +00:00
|
|
|
{lines: lines}
|
2015-07-05 20:35:57 +00:00
|
|
|
);
|
2015-10-03 14:14:41 +00:00
|
|
|
|
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);
|
|
|
|
};
|