NETRAVE/lib/utils/alert_queue_manager.rb
VetheonGames 7e17bd6e2d Replaced Ring Buffer with Array queue in AlertQueueManager class
In the AlertQueueManager class of the NETRAVE project, we made a
significant change to improve code simplicity, efficiency, and maintainability.
We replaced the previously used Ring Buffer with a standard Array queue for
 managing the queue of alerts. This change simplifies the code structure and
 improves the efficiency of queue management. As a result of this change, the
 Ring Buffer file, which is no longer needed, was also removed from the project.

 I would also like to thank Havenwood, Lapizistik, Sampersand, and Crimson
 from the Ruby discord in helping me determine the best data structure to use
 and helping me generally expand my deeper knowledge of Ruby
2023-07-06 09:08:39 -06:00

48 lines
1011 B
Ruby

# frozen_string_literal: true
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
class AlertQueueManager
SHUTDOWN_SIGNAL = 'shutdown'
def initialize(logger) # rubocop:disable Metrics/MethodLength
@loggman = logger
@alert_queue = []
@shutdown = false
@worker_thread = Thread.new do
loop do
if @alert_queue.empty?
sleep(0.1) # Sleep for 100 milliseconds
next
end
alert = pop_alert
break if alert.message == SHUTDOWN_SIGNAL
alert.display
sleep(4.5)
end
end
end
def enqueue_alert(alert)
@alert_queue << alert
end
def pop_alert
@alert_queue.shift
end
def shutdown
enqueue_alert(Alert.new(SHUTDOWN_SIGNAL, :info))
@shutdown = true
end
def join_worker
@worker_thread.join if @shutdown
end
end