add after build trigger

This commit is contained in:
oleg 2015-06-13 21:59:32 +03:00
parent 9e0af72475
commit a832398859
5 changed files with 67 additions and 28 deletions

13
app.js
View File

@ -8,7 +8,8 @@ var http = require('http'),
Steppy = require('twostep').Steppy, Steppy = require('twostep').Steppy,
_ = require('underscore'), _ = require('underscore'),
reader = require('./lib/reader'), reader = require('./lib/reader'),
notifier = require('./lib/notifier'); notifier = require('./lib/notifier'),
project = require('./lib/project');
var staticServer = new nodeStatic.Server('./static'); var staticServer = new nodeStatic.Server('./static');
var server = http.createServer(function(req, res, next) { var server = http.createServer(function(req, res, next) {
@ -70,6 +71,16 @@ Steppy(
console.log('Server config:', JSON.stringify(app.config, null, 4)); console.log('Server config:', JSON.stringify(app.config, null, 4));
notifier.init(app.config.notify, this.slot()); notifier.init(app.config.notify, this.slot());
},
function() {
project.loadAll(app.config.paths.projects, this.slot());
},
function(err, projects) {
app.projects = projects;
console.log(
'Loaded projects: ',
_(projects).chain().pluck('config').pluck('name').value()
);
// init resources // init resources
require('./resources')(app); require('./resources')(app);

View File

@ -18,6 +18,11 @@ notify:
# jabber: # jabber:
# - oleg.korobenko@gmail.com # - oleg.korobenko@gmail.com
trigger:
after:
- status: done
project: project2
steps: steps:
- cmd: > - cmd: >
echo "long multiline string" && echo "long multiline string" &&

View File

@ -20,6 +20,10 @@ function Distributor(params) {
self.saveBuild = params.saveBuild || function(build, callback) { self.saveBuild = params.saveBuild || function(build, callback) {
callback(null, build); callback(null, build);
}; };
self.projectsHash = _(params.projects).indexBy(function(project) {
return project.config.name;
});
} }
inherits(Distributor, EventEmitter); inherits(Distributor, EventEmitter);
@ -70,9 +74,7 @@ Distributor.prototype._runNext = function(callback) {
error: err ? err.message : null error: err ? err.message : null
}, },
function(err, build) { function(err, build) {
notifier.send(build); self._onBuildComplete(err, build, stepCallback)
// try to run next project from the queue
self._runNext(stepCallback);
} }
); );
}); });
@ -93,6 +95,35 @@ Distributor.prototype._runNext = function(callback) {
); );
}; };
Distributor.prototype._onBuildComplete = function(err, build, callback) {
var self = this;
Steppy(
function() {
// process after build triggers
var triggerAfterGroup = this.makeGroup();
var after = build.project.trigger && build.project.trigger.after;
if (after) {
_(after).each(function(item) {
if (!item.status || item.status === build.status) {
self.run(item.project, {}, triggerAfterGroup.slot());
}
});
}
},
function(err, triggerAfterGroupResults) {
// notify about build
notifier.send(build);
// try to run next project from the queue
self._runNext(this.slot());
},
callback
);
};
Distributor.prototype._updateBuild = function(build, changes, callback) { Distributor.prototype._updateBuild = function(build, changes, callback) {
var self = this; var self = this;
callback = callback || _.noop; callback = callback || _.noop;
@ -127,10 +158,12 @@ Distributor.prototype._updateBuild = function(build, changes, callback) {
); );
}; };
Distributor.prototype.run = function(project, params, callback) { Distributor.prototype.run = function(projectName, params, callback) {
var self = this; var self = this,
project;
Steppy( Steppy(
function() { function() {
project = self.projectsHash[projectName].config;
self._updateBuild({}, { self._updateBuild({}, {
project: project, project: project,
params: params, params: params,

View File

@ -12,22 +12,9 @@ module.exports = function(app) {
var resource = app.dataio.resource('projects'); var resource = app.dataio.resource('projects');
var projects, projectsHash;
project.loadAll(app.config.paths.projects, function(err, loadedProjects) {
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()
);
});
var distributor = new Distributor({ var distributor = new Distributor({
nodes: app.config.nodes, nodes: app.config.nodes,
projects: app.projects,
saveBuild: function(build, callback) { saveBuild: function(build, callback) {
Steppy( Steppy(
function() { function() {
@ -117,14 +104,13 @@ module.exports = function(app) {
}); });
resource.use('readAll', function(req, res) { resource.use('readAll', function(req, res) {
res.send(_(projects).pluck('config')); res.send(_(app.projects).pluck('config'));
}); });
resource.use('run', function(req, res) { resource.use('run', function(req, res) {
var projectName = req.data.projectName, var projectName = req.data.projectName;
project = projectsHash[projectName]; console.log('Run the project: %j', projectName);
console.log('Run the project: %j', project || projectName); distributor.run(projectName, {}, function(err, build) {
distributor.run(project.config, {}, function(err, build) {
console.log('>>> err, build = ', err && err.stack || err, build); console.log('>>> err, build = ', err && err.stack || err, build);
}); });
res.send(); res.send();

View File

@ -9,7 +9,9 @@ var Distributor = require('../lib/distributor').Distributor,
describe('Distributor', function() { describe('Distributor', function() {
var distributor, var distributor,
project1 = {name: 'project1'}; projects = [{
config: {name: 'project1'}
}];
var createNodeMock = function(executorRun) { var createNodeMock = function(executorRun) {
return function(params) { return function(params) {
@ -46,13 +48,14 @@ describe('Distributor', function() {
it('instance should be created without errors', function() { it('instance should be created without errors', function() {
distributor = new Distributor({ distributor = new Distributor({
projects: projects,
nodes: [{type: 'local', maxExecutorsCount: 1}] nodes: [{type: 'local', maxExecutorsCount: 1}]
}); });
updateBuildSpy = sinon.spy(distributor, '_updateBuild'); updateBuildSpy = sinon.spy(distributor, '_updateBuild');
}); });
it('should run without errors', function(done) { it('should run without errors', function(done) {
distributor.run(project1, {}, function(err) { distributor.run('project1', {}, function(err) {
expect(err).not.ok(); expect(err).not.ok();
done(); done();
}); });
@ -108,13 +111,14 @@ describe('Distributor', function() {
it('instance should be created without errors', function() { it('instance should be created without errors', function() {
distributor = new Distributor({ distributor = new Distributor({
projects: projects,
nodes: [{type: 'local', maxExecutorsCount: 1}] nodes: [{type: 'local', maxExecutorsCount: 1}]
}); });
updateBuildSpy = sinon.spy(distributor, '_updateBuild'); updateBuildSpy = sinon.spy(distributor, '_updateBuild');
}); });
it('should run without errors', function(done) { it('should run without errors', function(done) {
distributor.run(project1, {}, function(err) { distributor.run('project1', {}, function(err) {
expect(err).not.ok(); expect(err).not.ok();
done(); done();
}); });