move creating of build data resource to resources index
This commit is contained in:
parent
b34f618adc
commit
4da569083b
@ -41,71 +41,7 @@ exports.init = function(app, callback) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var buildDataResourcesHash = {};
|
|
||||||
|
|
||||||
// create resource for build data
|
|
||||||
var createBuildDataResource = function(buildId) {
|
|
||||||
if (buildId in buildDataResourcesHash) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var buildDataResource = app.dataio.resource('build' + buildId);
|
|
||||||
buildDataResource.on('connection', function(client) {
|
|
||||||
var callback = this.async();
|
|
||||||
Steppy(
|
|
||||||
function() {
|
|
||||||
db.logLines.find({
|
|
||||||
start: {buildId: buildId},
|
|
||||||
}, this.slot());
|
|
||||||
},
|
|
||||||
function(err, lines) {
|
|
||||||
client.emit('sync', 'data', {lines: lines});
|
|
||||||
this.pass(true);
|
|
||||||
},
|
|
||||||
function(err) {
|
|
||||||
if (err) {
|
|
||||||
logger.error(
|
|
||||||
'error during read log for "' + buildId + '":',
|
|
||||||
err.stack || err
|
|
||||||
);
|
|
||||||
}
|
|
||||||
callback();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
|
||||||
buildDataResourcesHash[buildId] = buildDataResource;
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.createBuildDataResource = createBuildDataResource;
|
|
||||||
|
|
||||||
var buildsResource = app.dataio.resource('builds');
|
|
||||||
|
|
||||||
distributor.on('buildUpdate', function(build, changes) {
|
|
||||||
if (build.status === 'queued') {
|
|
||||||
createBuildDataResource(build.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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(build.project.name);
|
|
||||||
}
|
|
||||||
|
|
||||||
buildsResource.clientEmitSync('change', {
|
|
||||||
buildId: build.id, changes: changes
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
distributor.on('buildCancel', function(build) {
|
|
||||||
buildsResource.clientEmitSync('cancel', {buildId: build.id});
|
|
||||||
});
|
|
||||||
|
|
||||||
distributor.on('buildLogLines', function(build, lines) {
|
distributor.on('buildLogLines', function(build, lines) {
|
||||||
app.dataio.resource('build' + build.id).clientEmitSync(
|
|
||||||
'data',
|
|
||||||
{lines: lines}
|
|
||||||
);
|
|
||||||
|
|
||||||
// write build logs to db
|
// write build logs to db
|
||||||
db.logLines.put(lines, function(err) {
|
db.logLines.put(lines, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
13
lib/build.js
13
lib/build.js
@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var Steppy = require('twostep').Steppy,
|
var Steppy = require('twostep').Steppy,
|
||||||
|
_ = require('underscore'),
|
||||||
EventEmitter = require('events').EventEmitter,
|
EventEmitter = require('events').EventEmitter,
|
||||||
inherits = require('util').inherits;
|
inherits = require('util').inherits;
|
||||||
|
|
||||||
@ -11,12 +12,24 @@ var Steppy = require('twostep').Steppy,
|
|||||||
function BuildsCollection(params) {
|
function BuildsCollection(params) {
|
||||||
this.db = params.db;
|
this.db = params.db;
|
||||||
this.distributor = params.distributor;
|
this.distributor = params.distributor;
|
||||||
|
|
||||||
|
this._proxyDistributorEvent('buildUpdate', 'buildUpdated');
|
||||||
|
this._proxyDistributorEvent('buildCancel', 'buildCanceled');
|
||||||
|
this._proxyDistributorEvent('buildLogLines', 'buildLogLines');
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.BuildsCollection = BuildsCollection;
|
exports.BuildsCollection = BuildsCollection;
|
||||||
|
|
||||||
inherits(BuildsCollection, EventEmitter);
|
inherits(BuildsCollection, EventEmitter);
|
||||||
|
|
||||||
|
BuildsCollection.prototype._proxyDistributorEvent = function(source, dest) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
self.distributor.on(source, function() {
|
||||||
|
self.emit.apply(self, [dest].concat(_(arguments).toArray()));
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
BuildsCollection.prototype.create = function(params, callback) {
|
BuildsCollection.prototype.create = function(params, callback) {
|
||||||
this.distributor.run(params, callback);
|
this.distributor.run(params, callback);
|
||||||
};
|
};
|
||||||
|
@ -78,8 +78,8 @@ module.exports = function(app) {
|
|||||||
this.slot()
|
this.slot()
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
function(err, logLinesResult) {
|
function(err, logLinesData) {
|
||||||
res.send(logLinesResult);
|
res.send(logLinesData);
|
||||||
},
|
},
|
||||||
next
|
next
|
||||||
);
|
);
|
||||||
|
37
resources/helpers.js
Normal file
37
resources/helpers.js
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var Steppy = require('twostep').Steppy,
|
||||||
|
logger = require('../lib/logger')('create build resource');
|
||||||
|
|
||||||
|
var buildDataResourcesHash = {};
|
||||||
|
|
||||||
|
// create resource for build data
|
||||||
|
exports.createBuildDataResource = function(app, buildId) {
|
||||||
|
if (buildId in buildDataResourcesHash) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var buildDataResource = app.dataio.resource('build' + buildId);
|
||||||
|
buildDataResource.on('connection', function(client) {
|
||||||
|
var callback = this.async();
|
||||||
|
Steppy(
|
||||||
|
function() {
|
||||||
|
app.builds.getLogLines({buildId: buildId}, this.slot());
|
||||||
|
},
|
||||||
|
function(err, logLinesData) {
|
||||||
|
client.emit('sync', 'data', {lines: logLinesData.lines});
|
||||||
|
|
||||||
|
this.pass(null);
|
||||||
|
},
|
||||||
|
function(err) {
|
||||||
|
if (err) {
|
||||||
|
logger.error(
|
||||||
|
'error during read log for "' + buildId + '":',
|
||||||
|
err.stack || err
|
||||||
|
);
|
||||||
|
}
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
buildDataResourcesHash[buildId] = buildDataResource;
|
||||||
|
};
|
@ -1,11 +1,42 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var _ = require('underscore'),
|
var _ = require('underscore'),
|
||||||
errorHandler = require('./errorHandler');
|
errorHandler = require('./errorHandler'),
|
||||||
|
createBuildDataResource = require('./helpers').createBuildDataResource;
|
||||||
|
|
||||||
module.exports = function(app) {
|
module.exports = function(app) {
|
||||||
_(['builds', 'projects']).each(function(resource) {
|
_(['builds', 'projects']).each(function(resource) {
|
||||||
var resource = require('./' + resource)(app);
|
var resource = require('./' + resource)(app);
|
||||||
resource.use(errorHandler);
|
resource.use(errorHandler);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var buildsResource = app.dataio.resource('builds');
|
||||||
|
|
||||||
|
app.builds.on('buildUpdated', function(build, changes) {
|
||||||
|
if (build.status === 'queued') {
|
||||||
|
createBuildDataResource(app, build.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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(build.project.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
buildsResource.clientEmitSync('change', {
|
||||||
|
buildId: build.id, changes: changes
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
app.builds.on('buildCanceled', function(build) {
|
||||||
|
buildsResource.clientEmitSync('cancel', {buildId: build.id});
|
||||||
|
});
|
||||||
|
|
||||||
|
app.builds.on('buildLogLines', function(build, lines) {
|
||||||
|
app.dataio.resource('build' + build.id).clientEmitSync(
|
||||||
|
'data',
|
||||||
|
{lines: lines}
|
||||||
|
);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
@ -2,14 +2,14 @@
|
|||||||
|
|
||||||
var Steppy = require('twostep').Steppy,
|
var Steppy = require('twostep').Steppy,
|
||||||
_ = require('underscore'),
|
_ = require('underscore'),
|
||||||
createBuildDataResource = require('../distributor').createBuildDataResource,
|
createBuildDataResource = require('./helpers').createBuildDataResource,
|
||||||
logger = require('../lib/logger')('projects resource');
|
logger = require('../lib/logger')('projects resource');
|
||||||
|
|
||||||
module.exports = function(app) {
|
module.exports = function(app) {
|
||||||
var resource = app.dataio.resource('projects');
|
var resource = app.dataio.resource('projects');
|
||||||
|
|
||||||
resource.use('createBuildDataResource', function(req, res) {
|
resource.use('createBuildDataResource', function(req, res) {
|
||||||
createBuildDataResource(req.data.buildId);
|
createBuildDataResource(app, req.data.buildId);
|
||||||
res.send();
|
res.send();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user