debian-mirror-gitlab/app/services/web_hooks/log_execution_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

88 lines
2.4 KiB
Ruby
Raw Normal View History

2021-09-04 01:27:46 +05:30
# frozen_string_literal: true
module WebHooks
class LogExecutionService
2022-05-07 20:08:51 +05:30
include ::Gitlab::ExclusiveLeaseHelpers
LOCK_TTL = 15.seconds.freeze
LOCK_SLEEP = 0.25.seconds.freeze
LOCK_RETRY = 65
2021-09-04 01:27:46 +05:30
attr_reader :hook, :log_data, :response_category
def initialize(hook:, log_data:, response_category:)
@hook = hook
2022-05-07 20:08:51 +05:30
@log_data = log_data.transform_keys(&:to_sym)
2021-09-04 01:27:46 +05:30
@response_category = response_category
2022-05-07 20:08:51 +05:30
@prev_state = hook.active_state(ignore_flag: true)
2021-09-04 01:27:46 +05:30
end
def execute
2022-05-07 20:08:51 +05:30
update_hook_failure_state
2021-09-04 01:27:46 +05:30
log_execution
end
private
def log_execution
2022-05-07 20:08:51 +05:30
WebHookLog.create!(web_hook: hook, **log_data)
2021-09-04 01:27:46 +05:30
end
2022-05-07 20:08:51 +05:30
# 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
log_state_change
2021-09-04 01:27:46 +05:30
end
2022-05-07 20:08:51 +05:30
rescue Gitlab::ExclusiveLeaseHelpers::FailedToObtainLockError
raise if raise_lock_error?
end
def log_state_change
new_state = hook.active_state(ignore_flag: true)
return if @prev_state == new_state
Gitlab::AuthLogger.info(
message: 'WebHook change active_state',
# identification
hook_id: hook.id,
hook_type: hook.type,
project_id: hook.project_id,
group_id: hook.group_id,
# relevant data
prev_state: @prev_state,
new_state: new_state,
duration: log_data[:execution_duration],
response_status: log_data[:response_status],
recent_hook_failures: hook.recent_failures,
# context
**Gitlab::ApplicationContext.current
)
end
def lock_name
"web_hooks:update_hook_failure_state:#{hook.id}"
end
# Allow an error to be raised after failing to obtain a lease only if the hook
# is not already in the correct failure state.
def raise_lock_error?
hook.reset # Reload so properties are guaranteed to be current.
hook.executable? != (response_category == :ok)
2021-09-04 01:27:46 +05:30
end
end
end