nci/resources/projects.js

121 lines
2.9 KiB
JavaScript
Raw Normal View History

2015-04-10 19:23:52 +00:00
'use strict';
var Steppy = require('twostep').Steppy,
_ = require('underscore'),
project = require('../lib/project'),
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 20:12:28 +00:00
var distributor = new Distributor({
2015-05-18 18:25:08 +00:00
nodes: app.config.nodes,
2015-06-13 18:59:32 +00:00
projects: app.projects,
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-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-06-13 18:59:32 +00:00
res.send(_(app.projects).pluck('config'));
2015-04-12 22:41:09 +00:00
});
resource.use('run', function(req, res) {
2015-06-13 18:59:32 +00:00
var projectName = req.data.projectName;
2015-06-13 19:08:13 +00:00
console.log('Run the project: %s', projectName);
distributor.run({projectName: projectName}, function(err, build) {
console.log('>>> err, build = ', err && err.stack || err, build);
});
res.send();
});
return resource;
2015-04-10 19:23:52 +00:00
};