blob: ae567cb7d0e1385f0dbde246659d7ec87685d76f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# frozen_string_literal: true
module Gitlab
module Memory
# A background thread that monitors Ruby memory and calls
# into a handler when the Ruby process violates defined limits
# for an extended period of time.
class Watchdog
def initialize
@configuration = Configuration.new
@alive = true
end
##
# Configuration for Watchdog, see Gitlab::Memory::Watchdog::Configurator
# for examples.
def configure
yield configuration
end
def call
event_reporter.started(log_labels)
while @alive
sleep(sleep_time_seconds)
monitor
end
event_reporter.stopped(log_labels(memwd_reason: @stop_reason).compact)
end
def stop
stop_working(reason: 'background task stopped')
handler.stop if handler.respond_to?(:stop)
end
private
attr_reader :configuration
delegate :event_reporter, :monitors, :sleep_time_seconds, to: :configuration
def monitor
if monitors.empty?
stop_working(reason: 'monitors are not configured')
return
end
monitors.call_each do |result|
break unless @alive
next unless result.threshold_violated?
event_reporter.threshold_violated(result.monitor_name)
next unless result.strikes_exceeded?
strike_exceeded_callback(result.monitor_name, result.payload)
end
end
def strike_exceeded_callback(monitor_name, monitor_payload)
event_reporter.strikes_exceeded(monitor_name, log_labels(monitor_payload))
Gitlab::Memory::Reports::HeapDump.enqueue!
stop_working(reason: 'successfully handled') if handler.call
end
def handler
configuration.handler
end
def log_labels(extra = {})
extra.merge(
memwd_handler_class: handler.class.name,
memwd_sleep_time_s: sleep_time_seconds
)
end
def stop_working(reason:)
return unless @alive
@stop_reason = reason
@alive = false
end
end
end
end
|