2015-04-10 19:23:52 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-09 19:53:19 +00:00
|
|
|
var Steppy = require('twostep').Steppy,
|
|
|
|
_ = require('underscore'),
|
2015-05-01 11:11:29 +00:00
|
|
|
project = require('../lib/project'),
|
2015-05-09 19:53:19 +00:00
|
|
|
Distributor = require('../lib/distributor').Distributor,
|
2015-05-12 20:12:28 +00:00
|
|
|
db = require('../db'),
|
2015-05-12 21:07:03 +00:00
|
|
|
path = require('path'),
|
|
|
|
fs = require('fs');
|
2015-04-12 22:19:42 +00:00
|
|
|
|
2015-05-12 20:12:28 +00:00
|
|
|
module.exports = function(app) {
|
2015-04-12 22:41:09 +00:00
|
|
|
|
2015-05-14 20:37:44 +00:00
|
|
|
var resource = app.dataio.resource('projects');
|
|
|
|
|
2015-05-12 21:07:03 +00:00
|
|
|
var projects, projectsHash;
|
2015-04-12 22:19:42 +00:00
|
|
|
|
2015-05-12 21:07:03 +00:00
|
|
|
project.loadAll(app.config.paths.projects, function(err, loadedProjects) {
|
2015-05-12 20:12:28 +00:00
|
|
|
if (err) throw err;
|
|
|
|
projects = loadedProjects;
|
|
|
|
projectsHash = _(projects).indexBy(function(project) {
|
|
|
|
return project.config.name;
|
|
|
|
});
|
|
|
|
console.log(
|
|
|
|
'Loaded projects: ',
|
|
|
|
_(projects).chain().pluck('config').pluck('name').value()
|
2015-05-12 19:53:04 +00:00
|
|
|
);
|
2015-05-12 20:12:28 +00:00
|
|
|
});
|
2015-05-03 15:23:01 +00:00
|
|
|
|
2015-05-12 20:12:28 +00:00
|
|
|
var distributor = new Distributor({
|
2015-05-18 18:25:08 +00:00
|
|
|
nodes: app.config.nodes,
|
2015-05-12 20:12:28 +00:00
|
|
|
saveBuild: function(build, callback) {
|
|
|
|
Steppy(
|
|
|
|
function() {
|
|
|
|
db.builds.put(build, this.slot());
|
|
|
|
},
|
|
|
|
function() {
|
|
|
|
this.pass(build);
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2015-05-03 15:23:01 +00:00
|
|
|
|
2015-05-14 20:37:44 +00:00
|
|
|
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;
|
2015-05-12 21:07:03 +00:00
|
|
|
};
|
|
|
|
|
2015-05-10 10:04:54 +00:00
|
|
|
distributor.on('buildUpdate', function(build, changes) {
|
|
|
|
var buildsResource = app.dataio.resource('builds');
|
|
|
|
|
|
|
|
if (build.status === 'queued') {
|
2015-05-14 20:37:44 +00:00
|
|
|
// remove prev log if it exists - for development
|
|
|
|
fs.unlink(getBuildLogPath(build.id));
|
|
|
|
createBuildDataResource(build);
|
2015-05-10 10:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buildsResource.clientEmitSync('change', {
|
|
|
|
buildId: build.id, changes: changes
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-14 20:37:44 +00:00
|
|
|
resource.use('createBuildDataResource', function(req, res) {
|
|
|
|
createBuildDataResource({id: req.data.id});
|
|
|
|
res.send();
|
|
|
|
});
|
|
|
|
|
2015-05-12 21:07:03 +00:00
|
|
|
var writeStreamsHash = {};
|
|
|
|
|
2015-05-12 19:53:04 +00:00
|
|
|
distributor.on('buildData', function(build, data) {
|
2015-05-12 21:07:03 +00:00
|
|
|
if (!/\n$/.test(data)) {
|
|
|
|
data += '\n';
|
|
|
|
}
|
|
|
|
|
2015-05-14 20:37:44 +00:00
|
|
|
var filePath = getBuildLogPath(build.id);
|
2015-05-12 21:07:03 +00:00
|
|
|
writeStreamsHash[filePath] = (
|
|
|
|
writeStreamsHash[filePath] ||
|
2015-05-14 20:37:44 +00:00
|
|
|
fs.createWriteStream(getBuildLogPath(build.id), {encoding: 'utf8'})
|
2015-05-12 21:07:03 +00:00
|
|
|
);
|
|
|
|
// TODO: close ended files
|
|
|
|
writeStreamsHash[filePath]
|
|
|
|
.on('error', function(err) {
|
|
|
|
console.log(err.stack || err);
|
|
|
|
})
|
|
|
|
.write(data);
|
|
|
|
|
2015-05-12 19:53:04 +00:00
|
|
|
app.dataio.resource('build' + build.id).clientEmitSync('data', data);
|
|
|
|
});
|
|
|
|
|
2015-05-17 13:48:16 +00:00
|
|
|
resource.use('readAll', 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
|
|
|
};
|