2021-06-09 05:10:18 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module WebHooks
|
|
|
|
class LogExecutionService
|
2022-02-22 22:17:13 -05:00
|
|
|
include ::Gitlab::ExclusiveLeaseHelpers
|
|
|
|
|
|
|
|
LOCK_TTL = 15.seconds.freeze
|
|
|
|
LOCK_SLEEP = 0.25.seconds.freeze
|
|
|
|
LOCK_RETRY = 65
|
|
|
|
|
2021-06-09 05:10:18 -04:00
|
|
|
attr_reader :hook, :log_data, :response_category
|
|
|
|
|
|
|
|
def initialize(hook:, log_data:, response_category:)
|
|
|
|
@hook = hook
|
|
|
|
@log_data = log_data
|
|
|
|
@response_category = response_category
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2022-02-22 22:17:13 -05:00
|
|
|
update_hook_failure_state
|
2021-06-09 05:10:18 -04:00
|
|
|
log_execution
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def log_execution
|
|
|
|
WebHookLog.create!(web_hook: hook, **log_data.transform_keys(&:to_sym))
|
|
|
|
end
|
|
|
|
|
2022-02-22 22:17:13 -05:00
|
|
|
# Perform this operation within an `Gitlab::ExclusiveLease` lock to make it
|
|
|
|
# safe to be called concurrently from different workers.
|
|
|
|
def update_hook_failure_state
|
|
|
|
in_lock(lock_name, ttl: LOCK_TTL, sleep_sec: LOCK_SLEEP, retries: LOCK_RETRY) do |retried|
|
|
|
|
hook.reset # Reload within the lock so properties are guaranteed to be current.
|
|
|
|
|
|
|
|
case response_category
|
|
|
|
when :ok
|
|
|
|
hook.enable!
|
|
|
|
when :error
|
|
|
|
hook.backoff!
|
|
|
|
when :failed
|
|
|
|
hook.failed!
|
|
|
|
end
|
2021-06-09 05:10:18 -04:00
|
|
|
end
|
|
|
|
end
|
2022-02-22 22:17:13 -05:00
|
|
|
|
|
|
|
def lock_name
|
|
|
|
"web_hooks:update_hook_failure_state:#{hook.id}"
|
|
|
|
end
|
2021-06-09 05:10:18 -04:00
|
|
|
end
|
|
|
|
end
|