2023-07-05 12:35:20 -06:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-07-06 09:08:39 -06:00
|
|
|
require_relative 'alert'
|
|
|
|
require_relative 'logg_man'
|
|
|
|
|
|
|
|
# Class for managing the queue of alerts. This class also manages a little bit of concurrency
|
|
|
|
# We use mutex for sync so we don't break Curses, as Curses isn't thread safe
|
2023-07-05 12:35:20 -06:00
|
|
|
class AlertQueueManager
|
2023-07-05 16:25:26 -06:00
|
|
|
SHUTDOWN_SIGNAL = 'shutdown'
|
|
|
|
|
2023-07-06 09:08:39 -06:00
|
|
|
def initialize(logger) # rubocop:disable Metrics/MethodLength
|
2023-07-05 12:35:20 -06:00
|
|
|
@loggman = logger
|
2023-07-06 09:08:39 -06:00
|
|
|
@alert_queue = []
|
2023-07-05 16:25:26 -06:00
|
|
|
@shutdown = false
|
2023-07-05 12:35:20 -06:00
|
|
|
@worker_thread = Thread.new do
|
|
|
|
loop do
|
2023-07-06 09:08:39 -06:00
|
|
|
if @alert_queue.empty?
|
2023-07-05 16:25:26 -06:00
|
|
|
sleep(0.1) # Sleep for 100 milliseconds
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2023-07-06 09:08:39 -06:00
|
|
|
alert = pop_alert
|
2023-07-05 16:25:26 -06:00
|
|
|
break if alert.message == SHUTDOWN_SIGNAL
|
|
|
|
|
|
|
|
alert.display
|
|
|
|
sleep(4.5)
|
2023-07-05 12:35:20 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def enqueue_alert(alert)
|
2023-07-06 09:08:39 -06:00
|
|
|
@alert_queue << alert
|
|
|
|
end
|
2023-07-05 16:25:26 -06:00
|
|
|
|
2023-07-06 09:08:39 -06:00
|
|
|
def pop_alert
|
|
|
|
@alert_queue.shift
|
2023-07-05 12:35:20 -06:00
|
|
|
end
|
|
|
|
|
2023-07-05 16:25:26 -06:00
|
|
|
def shutdown
|
|
|
|
enqueue_alert(Alert.new(SHUTDOWN_SIGNAL, :info))
|
2023-07-06 09:08:39 -06:00
|
|
|
@shutdown = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def join_worker
|
|
|
|
@worker_thread.join if @shutdown
|
2023-07-05 12:35:20 -06:00
|
|
|
end
|
|
|
|
end
|