better build data output

This commit is contained in:
oleg 2015-05-14 23:37:44 +03:00
parent 7742e09bd8
commit 2af6ddbfb1
4 changed files with 66 additions and 35 deletions

View File

@ -10,6 +10,8 @@ var Steppy = require('twostep').Steppy,
module.exports = function(app) {
var resource = app.dataio.resource('projects');
var projects, projectsHash;
project.loadAll(app.config.paths.projects, function(err, loadedProjects) {
@ -39,27 +41,47 @@ module.exports = function(app) {
}
});
var getBuildDataFilePath = function(build) {
return path.join(app.config.paths.builds, build.id + '.log');
var getBuildLogPath = function(buildId) {
return path.join(app.config.paths.builds, buildId + '.log');
};
var buildDataResourcesHash = {};
// create resource for build data
var createBuildDataResource = function(build) {
if (build.id in buildDataResourcesHash) {
return;
}
var buildDataResource = app.dataio.resource('build' + build.id);
buildDataResource.on('connection', function(client) {
var callback = this.async();
var stream = fs.createReadStream(
getBuildLogPath(build.id),
{encoding: 'utf8'}
);
stream
.on('readable', function() {
var data = stream.read();
while (data) {
client.emit('sync', 'data', data);
data = stream.read();
}
})
.on('end', callback)
.on('error', function(err) {
console.log(err.stack || err);
});
});
buildDataResourcesHash[build.id] = buildDataResource;
};
distributor.on('buildUpdate', function(build, changes) {
var buildsResource = app.dataio.resource('builds');
if (build.status === 'queued') {
// create resource for build data
var buildDataResource = app.dataio.resource('build' + build.id);
buildDataResource.on('connection', function(client) {
var callback = this.async();
fs.createReadStream(getBuildDataFilePath(build), {encoding: 'utf8'})
.on('data', function(data) {
client.emit('sync', 'data', data);
})
.on('end', callback)
.on('error', function(err) {
console.log(err.stack || err);
});
});
// remove prev log if it exists - for development
fs.unlink(getBuildLogPath(build.id));
createBuildDataResource(build);
}
buildsResource.clientEmitSync('change', {
@ -67,6 +89,11 @@ module.exports = function(app) {
});
});
resource.use('createBuildDataResource', function(req, res) {
createBuildDataResource({id: req.data.id});
res.send();
});
var writeStreamsHash = {};
distributor.on('buildData', function(build, data) {
@ -74,10 +101,10 @@ module.exports = function(app) {
data += '\n';
}
var filePath = getBuildDataFilePath(build);
var filePath = getBuildLogPath(build.id);
writeStreamsHash[filePath] = (
writeStreamsHash[filePath] ||
fs.createWriteStream(getBuildDataFilePath(build), {encoding: 'utf8'})
fs.createWriteStream(getBuildLogPath(build.id), {encoding: 'utf8'})
);
// TODO: close ended files
writeStreamsHash[filePath]
@ -89,8 +116,6 @@ module.exports = function(app) {
app.dataio.resource('build' + build.id).clientEmitSync('data', data);
});
var resource = app.dataio.resource('projects');
resource.use('read', function(req, res) {
res.send(_(projects).pluck('config'));
});

View File

@ -25,7 +25,7 @@ define([
},
onShowTerminal: function(build) {
this.setState({showTerminal: !this.state.showTerminal});
BuildActions.readTerminalOutput(this.props.build.id);
BuildActions.readTerminalOutput(this.props.build);
},
onBuildSelect: function(buildId) {
console.log('on build select');

View File

@ -7,27 +7,23 @@ define([
var Store = Reflux.createStore({
listenables: BuildActions,
output: '',
init: function() {
console.log('init builds console output');
},
onReadTerminalOutput: function(buildId) {
var self = this;
onReadTerminalOutput: function(build) {
var self = this,
output = '',
resourceName = 'build' + build.id;
self.output = '';
var resourceName = 'build' + buildId;
connect.resource(resourceName).unsubscribeAll();
connect.resource(resourceName).subscribe(function(data) {
self.output += data;
connect.resource(resourceName).reconnect();
connect.resource(resourceName).subscribe('data', function(data) {
output += data;
self.trigger({
buildId: buildId,
name: 'Console for build #' + buildId,
data: self.output
buildId: build.id,
name: 'Console for build #' + build.id,
data: output
});
});
}

View File

@ -10,10 +10,20 @@ define(['_dataio'], function(dataio) {
var resource = connect.resource('__someResource__'),
resourcePrototype = Object.getPrototypeOf(resource);
resourcePrototype.unsubscribeAll = function() {
resourcePrototype.disconnect = function() {
this.socket.disconnect();
this.socket.removeAllListeners();
};
resourcePrototype.connect = function() {
this.socket.connect();
};
resourcePrototype.reconnect = function() {
this.disconnect();
this.connect();
};
return connect;
};
});