NETRAVE/lib/utils/alert_queue_manager.rb
VetheonGames 5b76cd117e Completion of Initial Major Setup System and Program Efficiency Enhancements
This commit marks a significant milestone in the NETRAVE project. The first major system has been successfully completed, laying a solid foundation for the subsequent development phases.

Key changes include:

1. Completion of the Initial Setup Process: The initial setup process has been successfully implemented. This process gathers necessary information from the user and sets up the basic environment for the application to run.

2. Removal of Redundant Thread in Alert System: An unnecessary thread in the alert system was identified and removed. This change simplifies the alert system's architecture and reduces the potential for concurrency-related issues.

3. Program Efficiency Improvements: Several optimizations have been made to enhance the efficiency of the program. These include changes to the way alerts are handled and displayed, as well as improvements to the handling of the alert queue.

These changes have been verified and tested to ensure they work as expected and contribute to the overall functionality and performance of the NETRAVE project.
2023-07-05 16:25:26 -06:00

46 lines
1.0 KiB
Ruby

# frozen_string_literal: true
require_relative 'ring_buffer'
# Class for managing the queues for alerts
class AlertQueueManager
SHUTDOWN_SIGNAL = 'shutdown'
def initialize(logger, size = 2 * 1024 * 1024) # rubocop:disable Metrics/MethodLength
@loggman = logger
@queue = RingBuffer.new(@loggman, size)
@shutdown = false
# Start a thread that continuously checks the queue and displays alerts
@worker_thread = Thread.new do
loop do
break if @shutdown && @queue.empty?
if @queue.empty?
sleep(0.1) # Sleep for 100 milliseconds
next
end
alert = @queue.pop # This will block until there's an alert in the queue
next if alert.nil?
break if alert.message == SHUTDOWN_SIGNAL
alert.display
sleep(4.5)
alert.clear
end
end
end
def enqueue_alert(alert)
return if @shutdown
@queue.push(alert)
end
def shutdown
@shutdown = true
enqueue_alert(Alert.new(SHUTDOWN_SIGNAL, :info))
end
end