summaryrefslogtreecommitdiff
path: root/lib/gitlab/memory/watchdog/configuration.rb
blob: 6ab199bf816da326151aa474a5c4b1849095d403 (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
# frozen_string_literal: true

module Gitlab
  module Memory
    class Watchdog
      class Configuration
        class MonitorStack
          def initialize
            @monitors = []
          end

          def push(monitor_class, *args, **kwargs, &block)
            @monitors.push(build_monitor_state(monitor_class, *args, **kwargs, &block))
          end

          def call_each
            @monitors.each do |monitor|
              yield monitor.call
            end
          end

          def empty?
            @monitors.empty?
          end

          private

          def build_monitor_state(monitor_class, *args, max_strikes:, monitor_name: nil, **kwargs, &block)
            monitor = build_monitor(monitor_class, *args, **kwargs, &block)
            monitor_name ||= monitor_class.name.demodulize.underscore

            Gitlab::Memory::Watchdog::MonitorState.new(monitor, max_strikes: max_strikes, monitor_name: monitor_name)
          end

          def build_monitor(monitor_class, *args, **kwargs, &block)
            monitor_class.new(*args, **kwargs, &block)
          end
        end

        DEFAULT_SLEEP_TIME_SECONDS = 60

        attr_writer :event_reporter, :handler, :sleep_time_seconds

        def monitors
          @monitor_stack ||= MonitorStack.new
          yield @monitor_stack if block_given?
          @monitor_stack
        end

        def handler
          @handler ||= Handlers::NullHandler.instance
        end

        def event_reporter
          @event_reporter ||= EventReporter.new
        end

        # Used to control the frequency with which the watchdog will wake up and poll the GC.
        def sleep_time_seconds
          @sleep_time_seconds ||= DEFAULT_SLEEP_TIME_SECONDS
        end
      end
    end
  end
end