class SystemdMon::NotificationCentre

Attributes

notifiers[RW]

Public Class Methods

new() click to toggle source
# File lib/systemd_mon/notification_centre.rb, line 8
def initialize
  self.notifiers = []
end

Public Instance Methods

<<(notifier)
Alias for: add_notifier
add_notifier(notifier) click to toggle source
# File lib/systemd_mon/notification_centre.rb, line 22
def add_notifier(notifier)
  unless notifier.respond_to?(:notify!)
    raise NotifierError, "Notifier #{notifier.class} must respond to 'notify!'"
  end
  self.notifiers << notifier
end
Also aliased as: <<
classes() click to toggle source
# File lib/systemd_mon/notification_centre.rb, line 12
def classes
  notifiers.map(&:class)
end
each() { |notifier| ... } click to toggle source
# File lib/systemd_mon/notification_centre.rb, line 16
def each
  notifiers.each do |notifier|
    yield notifier
  end
end
notify!(notification) click to toggle source
# File lib/systemd_mon/notification_centre.rb, line 51
def notify!(notification)
  each_notifier do |notifier|
    Logger.puts "Notifying state change of #{notification.unit.name} via #{notifier.class}"
    notifier.notify! notification
  end
end
notify_start!(hostname) click to toggle source
# File lib/systemd_mon/notification_centre.rb, line 29
def notify_start!(hostname)
  each_notifier do |notifier|
    if notifier.respond_to?(:notify_start!)
      Logger.puts "Notifying SystemdMon start via #{notifier.class}"
      notifier.notify_start! hostname
    else
      Logger.debug { "#{notifier.class} doesn't respond to 'notify_start!', not sending notification" }
    end
  end
end
notify_stop!(hostname) click to toggle source
# File lib/systemd_mon/notification_centre.rb, line 40
def notify_stop!(hostname)
  each_notifier do |notifier|
    if notifier.respond_to?(:notify_stop!)
      Logger.puts "Notifying SystemdMon stop via #{notifier.class}"
      notifier.notify_stop! hostname
    else
      Logger.debug { "#{notifier.class} doesn't respond to 'notify_start!', not sending notification" }
    end
  end
end

Protected Instance Methods

each_notifier() { |notifier| ... } click to toggle source
# File lib/systemd_mon/notification_centre.rb, line 63
def each_notifier
  notifiers.map { |notifier|
    Thread.new do
      begin
        yield notifier
      rescue => e
        Logger.error "Failed to send notification via #{notifier.class}:\n"
        Logger.error "  #{e.class}: #{e.message}\n"
        Logger.debug_error { "\n\t#{e.backtrace.join('\n\t')}\n" }
      end
    end
  }.each(&:join)
end