summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKamil Trzciński <ayufan@ayufan.eu>2015-11-03 13:56:43 +0000
committerKamil Trzciński <ayufan@ayufan.eu>2015-11-03 13:56:43 +0000
commit77ef4855e0b69fa9e7f5501b64d08d3fc0fe1205 (patch)
tree1a373db3309564b59fa85ce9d7615c5d79b7a738
parentb12e17ff5574dd960550a7a4cdf92f22a6e67fb1 (diff)
parent8d2758e02d634fd8518893f39dcc3359284e890f (diff)
downloadgitlab-ce-77ef4855e0b69fa9e7f5501b64d08d3fc0fe1205.tar.gz
Merge branch 'drop-old-builds' into 'master'
Cleanup stuck CI builds Fixes #3143 /cc @dzaporozhets @jacobvosmaer See merge request !1655
-rw-r--r--CHANGELOG1
-rw-r--r--app/workers/stuck_ci_builds_worker.rb18
-rw-r--r--spec/workers/stuck_ci_builds_worker_spec.rb44
3 files changed, 63 insertions, 0 deletions
diff --git a/CHANGELOG b/CHANGELOG
index a3fab2f27f0..0f4ff5a6c8a 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -37,6 +37,7 @@ v 8.1.0
- Fix duplicate repositories in GitHub import page (Stan Hu)
- Redirect to a default path if HTTP_REFERER is not set (Stan Hu)
- Adds ability to create directories using the web editor (Ben Ford)
+ - Cleanup stuck CI builds
v 8.1.0 (unreleased)
- Send an email to admin email when a user is reported for spam (Jonathan Rochkind)
diff --git a/app/workers/stuck_ci_builds_worker.rb b/app/workers/stuck_ci_builds_worker.rb
new file mode 100644
index 00000000000..ad02a3b16d9
--- /dev/null
+++ b/app/workers/stuck_ci_builds_worker.rb
@@ -0,0 +1,18 @@
+class StuckCiBuildsWorker
+ include Sidekiq::Worker
+ include Sidetiq::Schedulable
+
+ BUILD_STUCK_TIMEOUT = 1.day
+
+ recurrence { daily }
+
+ def perform
+ Rails.logger.info 'Cleaning stuck builds'
+
+ builds = Ci::Build.running_or_pending.where('updated_at < ?', BUILD_STUCK_TIMEOUT.ago)
+ builds.find_each(batch_size: 50).each do |build|
+ Rails.logger.debug "Dropping stuck #{build.status} build #{build.id} for runner #{build.runner_id}"
+ build.drop
+ end
+ end
+end
diff --git a/spec/workers/stuck_ci_builds_worker_spec.rb b/spec/workers/stuck_ci_builds_worker_spec.rb
new file mode 100644
index 00000000000..f9d87d97014
--- /dev/null
+++ b/spec/workers/stuck_ci_builds_worker_spec.rb
@@ -0,0 +1,44 @@
+require "spec_helper"
+
+describe StuckCiBuildsWorker do
+ let!(:build) { create :ci_build }
+
+ subject do
+ build.reload
+ build.status
+ end
+
+ %w(pending running).each do |status|
+ context "#{status} build" do
+ before do
+ build.update!(status: status)
+ end
+
+ it 'gets dropped if it was updated over 2 days ago' do
+ build.update!(updated_at: 2.day.ago)
+ StuckCiBuildsWorker.new.perform
+ is_expected.to eq('failed')
+ end
+
+ it "is still #{status}" do
+ build.update!(updated_at: 1.minute.ago)
+ StuckCiBuildsWorker.new.perform
+ is_expected.to eq(status)
+ end
+ end
+ end
+
+ %w(success failed canceled).each do |status|
+ context "#{status} build" do
+ before do
+ build.update!(status: status)
+ end
+
+ it "is still #{status}" do
+ build.update!(updated_at: 2.day.ago)
+ StuckCiBuildsWorker.new.perform
+ is_expected.to eq(status)
+ end
+ end
+ end
+end