2015-04-10 19:23:52 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-04-12 22:19:42 +00:00
|
|
|
var _ = require('underscore'),
|
2015-05-01 11:11:29 +00:00
|
|
|
project = require('../lib/project'),
|
|
|
|
Distributor = require('../lib/distributor').Distributor;
|
2015-04-12 22:19:42 +00:00
|
|
|
|
2015-05-01 11:11:29 +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;
|
2015-05-01 11:11:29 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2015-05-01 11:11:29 +00:00
|
|
|
module.exports = function(app) {
|
2015-05-03 22:58:04 +00:00
|
|
|
var buildsSequnce = 0;
|
2015-05-03 15:23:01 +00:00
|
|
|
|
|
|
|
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 >');
|
|
|
|
});
|
|
|
|
}
|
2015-05-03 15:23:01 +00:00
|
|
|
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);
|
2015-05-03 15:23:01 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-05-01 11:11:29 +00:00
|
|
|
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
|
|
|
});
|
2015-05-01 11:11:29 +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) {
|
2015-05-03 15:23:01 +00:00
|
|
|
console.log('>>> err, build = ', err && err.stack || err, build);
|
2015-05-01 11:11:29 +00:00
|
|
|
});
|
2015-05-03 15:23:01 +00:00
|
|
|
res.send();
|
2015-05-01 11:11:29 +00:00
|
|
|
});
|
2015-05-03 15:23:01 +00:00
|
|
|
|
|
|
|
return resource;
|
2015-04-10 19:23:52 +00:00
|
|
|
};
|