2018-06-27 03:23:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-02-19 13:09:10 -05:00
|
|
|
class StuckMergeJobsWorker # rubocop:disable Scalability/IdempotentWorker
|
2017-11-28 11:08:30 -05:00
|
|
|
include ApplicationWorker
|
2021-04-30 14:10:09 -04:00
|
|
|
|
2021-07-21 08:09:35 -04:00
|
|
|
data_consistency :always
|
|
|
|
|
2020-02-04 10:08:40 -05:00
|
|
|
include CronjobQueue # rubocop:disable Scalability/CronWorkerContext
|
2017-07-31 18:01:36 -04:00
|
|
|
|
2020-12-02 13:09:35 -05:00
|
|
|
feature_category :code_review
|
2019-10-18 07:11:44 -04:00
|
|
|
|
2018-12-18 13:29:48 -05:00
|
|
|
def self.logger
|
2020-09-08 05:08:31 -04:00
|
|
|
Gitlab::AppLogger
|
2018-12-18 13:29:48 -05:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-07-31 18:01:36 -04:00
|
|
|
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
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-07-31 18:01:36 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-07-31 18:01:36 -04:00
|
|
|
def apply_current_state!(completed_jids, completed_ids)
|
|
|
|
merge_requests = MergeRequest.where(id: completed_ids)
|
|
|
|
|
2019-10-18 07:11:44 -04:00
|
|
|
merge_requests.where.not(merge_commit_sha: nil).update_all(state_id: MergeRequest.available_states[:merged])
|
2017-12-11 16:00:11 -05:00
|
|
|
|
|
|
|
merge_requests_to_reopen = merge_requests.where(merge_commit_sha: nil)
|
|
|
|
|
|
|
|
# Do not reopen merge requests using direct queries.
|
|
|
|
# We rely on state machine callbacks to update head_pipeline_id
|
|
|
|
merge_requests_to_reopen.each(&:unlock_mr)
|
2017-07-31 18:01:36 -04:00
|
|
|
|
2018-12-18 13:29:48 -05:00
|
|
|
self.class.logger.info("Updated state of locked merge jobs. JIDs: #{completed_jids.join(', ')}")
|
2017-07-31 18:01:36 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-07-31 18:01:36 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-07-31 18:01:36 -04:00
|
|
|
def stuck_merge_requests
|
|
|
|
MergeRequest.select('id, merge_jid').with_state(:locked).where.not(merge_jid: nil).reorder(nil)
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-07-31 18:01:36 -04:00
|
|
|
end
|