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) { module.exports = function(app) {
var resource = app.dataio.resource('projects');
var projects, projectsHash; var projects, projectsHash;
project.loadAll(app.config.paths.projects, function(err, loadedProjects) { project.loadAll(app.config.paths.projects, function(err, loadedProjects) {
@ -39,27 +41,47 @@ module.exports = function(app) {
} }
}); });
var getBuildDataFilePath = function(build) { var getBuildLogPath = function(buildId) {
return path.join(app.config.paths.builds, build.id + '.log'); 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) { distributor.on('buildUpdate', function(build, changes) {
var buildsResource = app.dataio.resource('builds'); var buildsResource = app.dataio.resource('builds');
if (build.status === 'queued') { if (build.status === 'queued') {
// create resource for build data // remove prev log if it exists - for development
var buildDataResource = app.dataio.resource('build' + build.id); fs.unlink(getBuildLogPath(build.id));
buildDataResource.on('connection', function(client) { createBuildDataResource(build);
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);
});
});
} }
buildsResource.clientEmitSync('change', { 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 = {}; var writeStreamsHash = {};
distributor.on('buildData', function(build, data) { distributor.on('buildData', function(build, data) {
@ -74,10 +101,10 @@ module.exports = function(app) {
data += '\n'; data += '\n';
} }
var filePath = getBuildDataFilePath(build); var filePath = getBuildLogPath(build.id);
writeStreamsHash[filePath] = ( writeStreamsHash[filePath] = (
writeStreamsHash[filePath] || writeStreamsHash[filePath] ||
fs.createWriteStream(getBuildDataFilePath(build), {encoding: 'utf8'}) fs.createWriteStream(getBuildLogPath(build.id), {encoding: 'utf8'})
); );
// TODO: close ended files // TODO: close ended files
writeStreamsHash[filePath] writeStreamsHash[filePath]
@ -89,8 +116,6 @@ module.exports = function(app) {
app.dataio.resource('build' + build.id).clientEmitSync('data', data); app.dataio.resource('build' + build.id).clientEmitSync('data', data);
}); });
var resource = app.dataio.resource('projects');
resource.use('read', function(req, res) { resource.use('read', function(req, res) {
res.send(_(projects).pluck('config')); res.send(_(projects).pluck('config'));
}); });

View File

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

View File

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

View File

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