diff options
Diffstat (limited to 'app/services/merge_requests/merge_service.rb')
-rw-r--r-- | app/services/merge_requests/merge_service.rb | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/app/services/merge_requests/merge_service.rb b/app/services/merge_requests/merge_service.rb index 27f474b0fe7..5e7eee4f1c3 100644 --- a/app/services/merge_requests/merge_service.rb +++ b/app/services/merge_requests/merge_service.rb @@ -8,16 +8,22 @@ module MergeRequests # Executed when you do merge via GitLab UI # class MergeService < MergeRequests::MergeBaseService + include Gitlab::Utils::StrongMemoize + GENERIC_ERROR_MESSAGE = 'An error occurred while merging' + LEASE_TIMEOUT = 15.minutes.to_i delegate :merge_jid, :state, to: :@merge_request def execute(merge_request, options = {}) if project.merge_requests_ff_only_enabled && !self.is_a?(FfMergeService) - FfMergeService.new(project, current_user, params).execute(merge_request) + FfMergeService.new(project: project, current_user: current_user, params: params).execute(merge_request) return end + return if merge_request.merged? + return unless exclusive_lease(merge_request.id).try_obtain + @merge_request = merge_request @options = options @@ -34,6 +40,8 @@ module MergeRequests log_info("Merge process finished on JID #{merge_jid} with state #{state}") rescue MergeError => e handle_merge_error(log_message: e.message, save_message_on_model: true) + ensure + exclusive_lease(merge_request.id).cancel end private @@ -96,14 +104,14 @@ module MergeRequests rescue Gitlab::Git::PreReceiveError => e raise MergeError, "Something went wrong during merge pre-receive hook. #{e.message}".strip - rescue => e + rescue StandardError => e handle_merge_error(log_message: e.message) raise_error(GENERIC_ERROR_MESSAGE) end def after_merge log_info("Post merge started on JID #{merge_jid} with state #{state}") - MergeRequests::PostMergeService.new(project, current_user).execute(merge_request) + MergeRequests::PostMergeService.new(project: project, current_user: current_user).execute(merge_request) log_info("Post merge finished on JID #{merge_jid} with state #{state}") if delete_source_branch? @@ -146,5 +154,13 @@ module MergeRequests # loaded from the database they're strings params.with_indifferent_access[:sha] == merge_request.diff_head_sha end + + def exclusive_lease(merge_request_id) + strong_memoize(:"exclusive_lease_#{merge_request_id}") do + lease_key = ['merge_requests_merge_service', merge_request_id].join(':') + + Gitlab::ExclusiveLease.new(lease_key, timeout: LEASE_TIMEOUT) + end + end end end |