mirror of
https://gitlab.silvrtree.co.uk/martind2000/nci.git
synced 2025-03-13 08:20:00 +00:00
inroduce builds collection
This commit is contained in:
parent
14a14d3785
commit
65fde3532a
6
app.js
6
app.js
@ -11,6 +11,7 @@ var env = process.env.NODE_ENV || 'development',
|
||||
reader = require('./lib/reader'),
|
||||
notifier = require('./lib/notifier'),
|
||||
ProjectsCollection = require('./lib/project').ProjectsCollection,
|
||||
BuildsCollection = require('./lib/build').BuildsCollection,
|
||||
libLogger = require('./lib/logger'),
|
||||
EventEmitter = require('events').EventEmitter,
|
||||
validateConfig = require('./lib/validateConfig');
|
||||
@ -217,7 +218,10 @@ Steppy(
|
||||
require('./distributor').init(app, this.slot());
|
||||
},
|
||||
function(err, distributor) {
|
||||
app.distributor = distributor;
|
||||
app.builds = new BuildsCollection({
|
||||
db: db,
|
||||
distributor: distributor
|
||||
});
|
||||
|
||||
// register other plugins
|
||||
require('./lib/notifier/console').register(app);
|
||||
|
@ -63,7 +63,7 @@ module.exports = function(app) {
|
||||
if (project) {
|
||||
res.statusCode = 204;
|
||||
logger.log('Run project "%s"', projectName);
|
||||
app.distributor.run({
|
||||
app.builds.create({
|
||||
projectName: projectName,
|
||||
withScmChangesOnly: req.body.withScmChangesOnly,
|
||||
queueQueued: req.body.queueQueued,
|
||||
|
87
lib/build.js
Normal file
87
lib/build.js
Normal file
@ -0,0 +1,87 @@
|
||||
'use strict';
|
||||
|
||||
var Steppy = require('twostep').Steppy,
|
||||
EventEmitter = require('events').EventEmitter,
|
||||
inherits = require('util').inherits;
|
||||
|
||||
/**
|
||||
* Facade entity which accumulates operations with currently running and
|
||||
* db saved builds.
|
||||
*/
|
||||
function BuildsCollection(params) {
|
||||
this.db = params.db;
|
||||
this.distributor = params.distributor;
|
||||
}
|
||||
|
||||
exports.BuildsCollection = BuildsCollection;
|
||||
|
||||
inherits(BuildsCollection, EventEmitter);
|
||||
|
||||
BuildsCollection.prototype.create = function(params, callback) {
|
||||
this.distributor.run(params, callback);
|
||||
};
|
||||
|
||||
BuildsCollection.prototype.cancel = function(params, callback) {
|
||||
this.distributor.cancel(params, callback);
|
||||
};
|
||||
|
||||
BuildsCollection.prototype.get = function(id, callback) {
|
||||
this.find({start: {id: id}}, function(err, builds) {
|
||||
callback(err, builds && builds[0]);
|
||||
});
|
||||
};
|
||||
|
||||
BuildsCollection.prototype.find = function(params, callback) {
|
||||
this.db.builds.find(params, callback);
|
||||
};
|
||||
|
||||
BuildsCollection.prototype.getLogLines = function(params, callback) {
|
||||
var self = this;
|
||||
|
||||
Steppy(
|
||||
function() {
|
||||
var findParams = {
|
||||
start: {buildId: params.buildId},
|
||||
end: {buildId: params.buildId}
|
||||
};
|
||||
if (params.from) findParams.start.number = params.from;
|
||||
if (params.to) findParams.end.number = params.to;
|
||||
|
||||
var count = params.from && params.to ? params.to - params.from + 1: 0;
|
||||
|
||||
self.db.logLines.find(findParams, this.slot());
|
||||
|
||||
this.pass(count);
|
||||
},
|
||||
function(err, logLines, count) {
|
||||
this.pass({
|
||||
lines: logLines,
|
||||
isLast: count ? logLines.length < count : true
|
||||
});
|
||||
},
|
||||
callback
|
||||
);
|
||||
};
|
||||
|
||||
BuildsCollection.prototype.getLogLinesTail = function(params, callback) {
|
||||
var self = this;
|
||||
|
||||
Steppy(
|
||||
function() {
|
||||
var findParams = {
|
||||
reverse: true,
|
||||
start: {buildId: params.buildId},
|
||||
limit: params.limit
|
||||
};
|
||||
|
||||
self.db.logLines.find(findParams, this.slot());
|
||||
},
|
||||
function(err, logLines) {
|
||||
var lines = logLines.reverse(),
|
||||
total = logLines.length ? logLines[logLines.length - 1].number : 0;
|
||||
|
||||
this.pass({lines: lines, total: total});
|
||||
},
|
||||
callback
|
||||
);
|
||||
};
|
@ -2,13 +2,10 @@
|
||||
|
||||
var Steppy = require('twostep').Steppy,
|
||||
_ = require('underscore'),
|
||||
db = require('../db'),
|
||||
utils = require('../lib/utils'),
|
||||
logger = require('../lib/logger')('builds resource');
|
||||
|
||||
module.exports = function(app) {
|
||||
var resource = app.dataio.resource('builds'),
|
||||
distributor = app.distributor;
|
||||
var resource = app.dataio.resource('builds');
|
||||
|
||||
resource.use('readAll', function(req, res, next) {
|
||||
Steppy(
|
||||
@ -26,7 +23,7 @@ module.exports = function(app) {
|
||||
findParams.start = start;
|
||||
findParams.limit = findParams.limit || 20;
|
||||
|
||||
db.builds.find(findParams, this.slot());
|
||||
app.builds.find(findParams, this.slot());
|
||||
},
|
||||
function(err, builds) {
|
||||
// omit big fields not needed for list
|
||||
@ -49,12 +46,10 @@ module.exports = function(app) {
|
||||
resource.use('read', function(req, res, next) {
|
||||
Steppy(
|
||||
function() {
|
||||
var findParams = {};
|
||||
findParams.start = _(req.data).pick('id');
|
||||
db.builds.find(findParams, this.slot());
|
||||
app.builds.get(req.data.id, this.slot());
|
||||
},
|
||||
function(err, build) {
|
||||
res.send(build[0]);
|
||||
res.send(build);
|
||||
},
|
||||
next
|
||||
);
|
||||
@ -63,19 +58,13 @@ module.exports = function(app) {
|
||||
resource.use('getBuildLogTail', function(req, res, next) {
|
||||
Steppy(
|
||||
function() {
|
||||
var findParams = {
|
||||
reverse: true,
|
||||
start: {buildId: req.data.buildId},
|
||||
app.builds.getLogLinesTail({
|
||||
buildId: req.data.buildId,
|
||||
limit: req.data.length
|
||||
};
|
||||
|
||||
db.logLines.find(findParams, this.slot());
|
||||
}, this.slot());
|
||||
},
|
||||
function(err, logLines) {
|
||||
var lines = logLines.reverse(),
|
||||
total = logLines.length ? logLines[0].number : 0;
|
||||
|
||||
res.send({lines: lines, total: total});
|
||||
function(err, tail) {
|
||||
res.send(tail);
|
||||
},
|
||||
next
|
||||
);
|
||||
@ -84,23 +73,13 @@ module.exports = function(app) {
|
||||
resource.use('getBuildLogLines', function(req, res, next) {
|
||||
Steppy(
|
||||
function() {
|
||||
var buildId = req.data.buildId,
|
||||
from = req.data.from,
|
||||
to = req.data.to,
|
||||
count = to - from;
|
||||
|
||||
db.logLines.find({
|
||||
start: {buildId: buildId, number: from},
|
||||
end: {buildId: buildId, number: to}
|
||||
}, this.slot());
|
||||
|
||||
this.pass(count);
|
||||
app.builds.getLogLines(
|
||||
_(req.data).pick('buildId', 'from', 'to'),
|
||||
this.slot()
|
||||
);
|
||||
},
|
||||
function(err, logLines, count) {
|
||||
res.send({
|
||||
lines: logLines,
|
||||
isLast: logLines.length < count
|
||||
});
|
||||
function(err, logLinesResult) {
|
||||
res.send(logLinesResult);
|
||||
},
|
||||
next
|
||||
);
|
||||
@ -111,7 +90,7 @@ module.exports = function(app) {
|
||||
function() {
|
||||
var buildId = req.data.buildId;
|
||||
logger.log('Cancel build: "%s"', buildId);
|
||||
distributor.cancel({buildId: buildId}, this.slot());
|
||||
app.builds.cancel({buildId: buildId}, this.slot());
|
||||
},
|
||||
function() {
|
||||
res.send();
|
||||
|
@ -3,13 +3,10 @@
|
||||
var Steppy = require('twostep').Steppy,
|
||||
_ = require('underscore'),
|
||||
createBuildDataResource = require('../distributor').createBuildDataResource,
|
||||
logger = require('../lib/logger')('projects resource'),
|
||||
db = require('../db');
|
||||
logger = require('../lib/logger')('projects resource');
|
||||
|
||||
module.exports = function(app) {
|
||||
|
||||
var resource = app.dataio.resource('projects'),
|
||||
distributor = app.distributor;
|
||||
var resource = app.dataio.resource('projects');
|
||||
|
||||
resource.use('createBuildDataResource', function(req, res) {
|
||||
createBuildDataResource(req.data.buildId);
|
||||
@ -40,7 +37,7 @@ module.exports = function(app) {
|
||||
app.projects.getAvgBuildDuration(project.name, this.slot());
|
||||
|
||||
// get last done build
|
||||
db.builds.find({
|
||||
app.builds.find({
|
||||
start: {
|
||||
projectName: project.name,
|
||||
status: 'done',
|
||||
@ -53,7 +50,7 @@ module.exports = function(app) {
|
||||
var doneBuildsStreakCallback = _(this.slot()).once(),
|
||||
doneBuildsStreak = 0;
|
||||
|
||||
db.builds.find({
|
||||
app.builds.find({
|
||||
start: {
|
||||
projectName: project.name,
|
||||
descCreateDate: ''
|
||||
@ -117,7 +114,7 @@ module.exports = function(app) {
|
||||
resource.use('run', function(req, res) {
|
||||
var projectName = req.data.projectName;
|
||||
logger.log('Run the project: "%s"', projectName);
|
||||
distributor.run({
|
||||
app.builds.create({
|
||||
projectName: projectName,
|
||||
initiator: {type: 'user'},
|
||||
queueQueued: true
|
||||
|
@ -21,7 +21,7 @@ exports.init = function(app, callback) {
|
||||
cronTime: time,
|
||||
onTick: function() {
|
||||
logger.log('Run project "%s"', project.name);
|
||||
app.distributor.run({
|
||||
app.builds.create({
|
||||
projectName: project.name,
|
||||
withScmChangesOnly: project.buildEvery.withScmChangesOnly,
|
||||
initiator: {type: 'scheduler'}
|
||||
|
Loading…
Reference in New Issue
Block a user