summaryrefslogtreecommitdiff
path: root/app/workers
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2017-08-08 01:47:48 +0000
committerStan Hu <stanhu@gmail.com>2017-08-08 01:47:48 +0000
commitfd40bce9ccad94c3f8fe522a51a771335d95539f (patch)
tree0fb76c147480326f17fb77271628c24794255572 /app/workers
parent4490af8cf22416f12c8cf2e00f8b7c293be08167 (diff)
parenta0c22d1edafbd87d59dbf01acd610da97ec87912 (diff)
downloadgitlab-ce-fd40bce9ccad94c3f8fe522a51a771335d95539f.tar.gz
Merge branch '31207-clean-locked-merge-requests' into 'master'
Resolve "Store MergeWorker JID on merge request, and clean up stuck merges" Closes #31207 See merge request !13207
Diffstat (limited to 'app/workers')
-rw-r--r--app/workers/merge_worker.rb2
-rw-r--r--app/workers/stuck_merge_jobs_worker.rb34
2 files changed, 36 insertions, 0 deletions
diff --git a/app/workers/merge_worker.rb b/app/workers/merge_worker.rb
index 48e2da338f6..c3b58df92c1 100644
--- a/app/workers/merge_worker.rb
+++ b/app/workers/merge_worker.rb
@@ -7,6 +7,8 @@ class MergeWorker
current_user = User.find(current_user_id)
merge_request = MergeRequest.find(merge_request_id)
+ merge_request.update_column(:merge_jid, jid)
+
MergeRequests::MergeService.new(merge_request.target_project, current_user, params)
.execute(merge_request)
end
diff --git a/app/workers/stuck_merge_jobs_worker.rb b/app/workers/stuck_merge_jobs_worker.rb
new file mode 100644
index 00000000000..7843179d77c
--- /dev/null
+++ b/app/workers/stuck_merge_jobs_worker.rb
@@ -0,0 +1,34 @@
+class StuckMergeJobsWorker
+ include Sidekiq::Worker
+ include CronjobQueue
+
+ def perform
+ stuck_merge_requests.find_in_batches(batch_size: 100) do |group|
+ jids = group.map(&:merge_jid)
+
+ # Find the jobs that aren't currently running or that exceeded the threshold.
+ completed_jids = Gitlab::SidekiqStatus.completed_jids(jids)
+
+ if completed_jids.any?
+ completed_ids = group.select { |merge_request| completed_jids.include?(merge_request.merge_jid) }.map(&:id)
+
+ apply_current_state!(completed_jids, completed_ids)
+ end
+ end
+ end
+
+ private
+
+ def apply_current_state!(completed_jids, completed_ids)
+ merge_requests = MergeRequest.where(id: completed_ids)
+
+ merge_requests.where.not(merge_commit_sha: nil).update_all(state: :merged)
+ merge_requests.where(merge_commit_sha: nil).update_all(state: :opened)
+
+ Rails.logger.info("Updated state of locked merge jobs. JIDs: #{completed_jids.join(', ')}")
+ end
+
+ def stuck_merge_requests
+ MergeRequest.select('id, merge_jid').with_state(:locked).where.not(merge_jid: nil).reorder(nil)
+ end
+end