From 2af6ddbfb152987aa794a511fa6e955b7d827abc Mon Sep 17 00:00:00 2001 From: oleg Date: Thu, 14 May 2015 23:37:44 +0300 Subject: [PATCH] better build data output --- resources/projects.js | 63 +++++++++++++++++-------- static/js/app/components/builds/item.js | 2 +- static/js/app/stores/terminal.js | 24 ++++------ static/js/dataio.js | 12 ++++- 4 files changed, 66 insertions(+), 35 deletions(-) diff --git a/resources/projects.js b/resources/projects.js index 16dcf82..2aaa88d 100644 --- a/resources/projects.js +++ b/resources/projects.js @@ -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')); }); diff --git a/static/js/app/components/builds/item.js b/static/js/app/components/builds/item.js index 91fd821..6c0e653 100644 --- a/static/js/app/components/builds/item.js +++ b/static/js/app/components/builds/item.js @@ -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'); diff --git a/static/js/app/stores/terminal.js b/static/js/app/stores/terminal.js index 73f2202..75f5db7 100644 --- a/static/js/app/stores/terminal.js +++ b/static/js/app/stores/terminal.js @@ -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 }); }); } diff --git a/static/js/dataio.js b/static/js/dataio.js index 4a774dd..8986cf3 100644 --- a/static/js/dataio.js +++ b/static/js/dataio.js @@ -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; }; }); \ No newline at end of file