nci/resources/projects.js

65 lines
1.7 KiB
JavaScript
Raw Normal View History

2015-04-10 19:23:52 +00:00
'use strict';
2015-04-12 22:19:42 +00:00
var _ = require('underscore'),
project = require('../lib/project'),
Distributor = require('../lib/distributor').Distributor;
2015-04-12 22:19:42 +00:00
var projects, projectsHash;
2015-04-12 22:41:09 +00:00
2015-04-12 22:19:42 +00:00
project.loadAll('projects', function(err, loadedProjects) {
if (err) throw err;
projects = loadedProjects;
projectsHash = _(projects).indexBy(function(project) {
return project.config.name;
});
2015-04-30 15:15:23 +00:00
console.log(
'Loaded projects: ',
_(projects).chain().pluck('config').pluck('name').value()
);
2015-04-12 22:19:42 +00:00
});
module.exports = function(app) {
2015-05-03 22:58:04 +00:00
var buildsSequnce = 0;
var distributor = new Distributor({
nodes: [{type: 'local', maxExecutorsCount: 1}],
onBuildUpdate: function(build, callback) {
var buildsResource = app.dataio.resource('builds');
2015-05-03 22:58:04 +00:00
if (build.status === 'waiting') {
build.id = ++buildsSequnce;
// create resource for build data
var buildDataResource = app.dataio.resource('build' + build.id);
buildDataResource.on('connection', function(client) {
client.emit('sync', 'data', '< collected data >');
});
}
buildsResource.clientEmitSync(
build.status === 'waiting' ? 'create' : 'update',
build
);
callback(null, build);
2015-05-03 22:58:04 +00:00
},
onBuildData: function(build, data) {
app.dataio.resource('build' + build.id).clientEmitSync('data', data);
}
});
var resource = app.dataio.resource('projects');
2015-04-12 22:41:09 +00:00
resource.use('read', function(req, res) {
2015-04-30 15:15:23 +00:00
res.send(_(projects).pluck('config'));
2015-04-12 22:41:09 +00:00
});
resource.use('run', function(req, res) {
var projectName = req.data.projectName,
project = projectsHash[projectName];
console.log('Run the project: %j', project || projectName);
distributor.run(project.config, {}, function(err, build) {
console.log('>>> err, build = ', err && err.stack || err, build);
});
res.send();
});
return resource;
2015-04-10 19:23:52 +00:00
};