debian-mirror-gitlab/lib/gitlab/memory/watchdog/monitor_state.rb

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

79 lines
1.8 KiB
Ruby
Raw Normal View History

2022-11-25 23:54:43 +05:30
# frozen_string_literal: true
module Gitlab
module Memory
class Watchdog
class MonitorState
class Result
2023-03-04 22:38:38 +05:30
attr_reader :payload, :monitor_name
2022-11-25 23:54:43 +05:30
2023-03-04 22:38:38 +05:30
def initialize(strikes_exceeded:, threshold_violated:, monitor_name:, payload:)
2022-11-25 23:54:43 +05:30
@strikes_exceeded = strikes_exceeded
@threshold_violated = threshold_violated
2023-03-04 22:38:38 +05:30
@monitor_name = monitor_name.to_s.to_sym
2022-11-25 23:54:43 +05:30
@payload = payload
end
def strikes_exceeded?
@strikes_exceeded
end
def threshold_violated?
@threshold_violated
end
end
2023-03-04 22:38:38 +05:30
def initialize(monitor, max_strikes:, monitor_name:)
2022-11-25 23:54:43 +05:30
@monitor = monitor
@max_strikes = max_strikes
2023-03-04 22:38:38 +05:30
@monitor_name = monitor_name
2022-11-25 23:54:43 +05:30
@strikes = 0
end
def call
reset_strikes if strikes_exceeded?
monitor_result = @monitor.call
if monitor_result[:threshold_violated]
issue_strike
else
reset_strikes
end
build_result(monitor_result)
end
private
def build_result(monitor_result)
Result.new(
strikes_exceeded: strikes_exceeded?,
2023-03-04 22:38:38 +05:30
monitor_name: @monitor_name,
2022-11-25 23:54:43 +05:30
threshold_violated: monitor_result[:threshold_violated],
payload: payload.merge(monitor_result[:payload]))
end
def payload
{
memwd_max_strikes: @max_strikes,
memwd_cur_strikes: @strikes
}
end
def strikes_exceeded?
@strikes > @max_strikes
end
def issue_strike
@strikes += 1
end
def reset_strikes
@strikes = 0
end
end
end
end
end