From 9b7bb39d9196feee42c3c1c77dc5d2a3f19e52d1 Mon Sep 17 00:00:00 2001 From: oleg Date: Wed, 14 Oct 2015 21:45:54 +0300 Subject: [PATCH] complete uncompleted builds on server start --- README.md | 2 +- app.js | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ee7011a..a74f814 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ work in progress... * ~~Better tests coverage~~ * ~~Compile client for production~~ * Semantic versioning and plugins -* Complete uncompleted builds on server start +* ~~Complete uncompleted builds on server start~~ Ui fixes diff --git a/app.js b/app.js index a51710e..4378657 100644 --- a/app.js +++ b/app.js @@ -62,6 +62,78 @@ var configDefaults = { http: {host: '127.0.0.1', port: 3000, url: 'http://127.0.0.1:3000'} }; +var completeUncompletedBuilds = function(callback) { + Steppy( + function() { + db.builds.find({ + start: {descCreateDate: ''}, + filter: function(build) { + return !build.completed; + }, + limit: 100 + }, this.slot()); + }, + function(err, uncompletedBuilds) { + var completeGroup = this.makeGroup(); + + if (uncompletedBuilds.length) { + var queuedAndOtherUncompletedBuilds = _(uncompletedBuilds).partition( + function(uncompletedBuild) { + return uncompletedBuild.status === 'queued'; + } + ); + + var queuedBuilds = queuedAndOtherUncompletedBuilds[0]; + uncompletedBuilds = queuedAndOtherUncompletedBuilds[1]; + + if (queuedBuilds.length) { + logger.log( + 'remove queued builds: %s', + _(queuedBuilds).pluck('id').join(', ') + ); + + db.builds.del(queuedBuilds, completeGroup.slot()); + } + + if (uncompletedBuilds.length) { + logger.log( + 'complete with interrupt error uncompleted builds: %s', + _(uncompletedBuilds).pluck('id').join(', ') + ); + + _(uncompletedBuilds).each(function(uncompletedBuild) { + var endDate = ( + uncompletedBuild.startDate || + uncompletedBuild.createDate + ); + + var sumDuration = _(uncompletedBuild.stepTimings).reduce( + function(sum, timing) { + return sum + timing.duration; + }, + 0 + ) || 0; + + endDate += sumDuration; + + db.builds.update( + {id: uncompletedBuild.id}, + { + endDate: endDate, + status: 'error', + completed: true, + error: {message: 'interrupted by server restart'} + }, + completeGroup.slot() + ); + }); + } + } + }, + callback + ); +}; + Steppy( function() { app.config = {}; @@ -116,6 +188,8 @@ Steppy( function() { // load all projects for the first time project.loadAll(app.config.paths.projects, this.slot()); + + completeUncompletedBuilds(this.slot()); }, function(err, projects) { // note that `app.projects` is live variable