Class: Takwimu::Periodic

Inherits:
Object
  • Object
show all
Defined in:
lib/takwimu/periodic.rb

Instance Method Summary collapse

Constructor Details

#initialize(reporter:, sample_rate: 1, panels: []) ⇒ Periodic

Returns a new instance of Periodic.



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
# File 'lib/takwimu/periodic.rb', line 34

def initialize(reporter:, sample_rate: 1, panels: [])
  @reporter = reporter
  @reporter.sample_rate = sample_rate

  @default_interval = 5.0

  # compute interval based on a 60s reporting phase.
  @interval = sample_rate * @default_interval
  @panels = panels

  @thread = Thread.new {
    Thread.current[:takwimu_state] = {}

    @panels.each do |panel|
      panel.start! Thread.current[:takwimu_state]
    end

    loop do
      begin
        sleep @interval

        # read the current values
        env = {
          STATE    => Thread.current[:takwimu_state],
          COUNTERS => {},
          GAUGES   => {},
          TIMERS   => {}
        }

        @panels.each do |panel|
          panel.instrument! env[STATE], env[COUNTERS], env[GAUGES], env[TIMERS]
        end
        @reporter.report env
      end
    end
  }
  @thread.abort_on_exception = true
end

Instance Method Details

#stopObject



73
74
75
# File 'lib/takwimu/periodic.rb', line 73

def stop
  @thread.exit
end