Class: SystemdMon::CallbackManager

Inherits:
Object
  • Object
show all
Defined in:
lib/systemd_mon/callback_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(queue) ⇒ CallbackManager

Returns a new instance of CallbackManager.



5
6
7
8
# File 'lib/systemd_mon/callback_manager.rb', line 5

def initialize(queue)
  self.queue  = queue
  self.states = Hash.new { |h, u| h[u] = UnitWithState.new(u) }
end

Instance Method Details

#start(change_callback, each_state_change_callback) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/systemd_mon/callback_manager.rb', line 10

def start(change_callback, each_state_change_callback)
  loop do
    unit, state = queue.deq
    Logger.debug { "#{unit} new state: #{state}" }
    unit_state = states[unit]
    unit_state << state

    if each_state_change_callback
      with_error_handling { each_state_change_callback.call(unit_state) }
    end

    if change_callback && unit_state.state_change.important?
      with_error_handling { change_callback.call(unit_state) }
    end

    unit_state.reset! if unit_state.state_change.important?
  end
end

#with_error_handlingObject



29
30
31
32
33
34
# File 'lib/systemd_mon/callback_manager.rb', line 29

def with_error_handling
  yield
rescue => e
  Logger.error "Uncaught exception (#{e.class}) in callback: #{e.message}"
  Logger.debug_error { "\n\t#{e.backtrace.join("\n\t")}\n" }
end