Class: Legion::Chat::NotificationBridge

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/chat/notification_bridge.rb

Constant Summary collapse

DEFAULT_PATTERNS =
{
  'alert.fired'                  => :critical,
  'extinction.*'                 => :critical,
  'governance.consent_violation' => :critical,
  'runner.failure'               => :info,
  'worker.lifecycle'             => :info,
  'scheduler.mode_changed'       => :info
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue: NotificationQueue.new) ⇒ NotificationBridge

Returns a new instance of NotificationBridge.



19
20
21
22
# File 'lib/legion/chat/notification_bridge.rb', line 19

def initialize(queue: NotificationQueue.new)
  @queue = queue
  @patterns = load_patterns
end

Instance Attribute Details

#queueObject (readonly)

Returns the value of attribute queue.



17
18
19
# File 'lib/legion/chat/notification_bridge.rb', line 17

def queue
  @queue
end

Instance Method Details

#has_urgent?Boolean

rubocop:disable Naming/PredicatePrefix

Returns:

  • (Boolean)


40
41
42
# File 'lib/legion/chat/notification_bridge.rb', line 40

def has_urgent? # rubocop:disable Naming/PredicatePrefix
  @queue.has_critical?
end

#pending_notifications(max_priority: :info) ⇒ Object



36
37
38
# File 'lib/legion/chat/notification_bridge.rb', line 36

def pending_notifications(max_priority: :info)
  @queue.pop_all(max_priority: max_priority)
end

#startObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/legion/chat/notification_bridge.rb', line 24

def start
  return unless defined?(Legion::Events)

  Legion::Events.on('*') do |event_name, payload|
    priority = match_priority(event_name)
    next unless priority

    message = format_notification(event_name, payload)
    @queue.push(message: message, priority: priority, source: event_name)
  end
end