From f68ab83afc62ba9e24e3a61f9f133e6ff5ef13c3 Mon Sep 17 00:00:00 2001 From: Ari Karim Date: Sun, 25 May 2025 10:37:25 +0300 Subject: [PATCH 1/4] Refines logging levels for shutdown, stop, and message handling in Rabbit Carrots service --- lib/rabbit_carrots/core.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/rabbit_carrots/core.rb b/lib/rabbit_carrots/core.rb index f72931f..78d7ed8 100644 --- a/lib/rabbit_carrots/core.rb +++ b/lib/rabbit_carrots/core.rb @@ -61,7 +61,7 @@ module RabbitCarrots def request_shutdown # Workaround to a known issue with Signal Traps and logs Thread.start do - logger.log 'Shutting down Rabbit Carrots service...' + logger.error 'Shutting down Rabbit Carrots service...' end @shutdown_requested = true @threads.each(&:kill) @@ -71,7 +71,7 @@ module RabbitCarrots def stop # Workaround to a known issue with Signal Traps and logs Thread.start do - logger.log 'Stoppig the Rabbit Carrots service...' + logger.error 'Stoppig the Rabbit Carrots service...' end @running = false end @@ -80,7 +80,7 @@ module RabbitCarrots RabbitCarrots::Connection.instance.channel.with do |channel| exchange = channel.topic(RabbitCarrots.configuration.rabbitmq_exchange_name, durable: true) - logger.log "Listening on QUEUE: #{queue_name} for ROUTING KEYS: #{routing_keys}" + logger.info "Listening on QUEUE: #{queue_name} for ROUTING KEYS: #{routing_keys}" queue = channel.queue(queue_name, durable: true, arguments: queue_arguments) routing_keys.map(&:strip).each { |k| queue.bind(exchange, routing_key: k) } @@ -88,24 +88,24 @@ module RabbitCarrots queue.subscribe(block: false, manual_ack: true, prefetch: 10) do |delivery_info, properties, payload| break if @shutdown_requested - logger.log "Received from queue: #{queue_name}, Routing Keys: #{routing_keys}" + logger.info "Received from queue: #{queue_name}, Routing Keys: #{routing_keys}" handler_class.handle!(channel, delivery_info, properties, payload) channel.ack(delivery_info.delivery_tag, false) rescue RabbitCarrots::EventHandlers::Errors::NackMessage, JSON::ParserError => _e - logger.log "Nacked message: #{payload}" + logger.warn "Nacked message: #{payload}" channel.nack(delivery_info.delivery_tag, false, false) rescue RabbitCarrots::EventHandlers::Errors::NackAndRequeueMessage => _e - logger.log "Nacked and Requeued message: #{payload}" + logger.warn "Nacked and Requeued message: #{payload}" channel.nack(delivery_info.delivery_tag, false, true) rescue self.class.database_agnostic_not_null_violation, self.class.database_agnostic_record_invalid => e - logger.log "Null constraint or Invalid violation: #{payload}. Error: #{e.message}" + logger.warn "Null constraint or Invalid violation: #{payload}. Error: #{e.message}" channel.ack(delivery_info.delivery_tag, false) rescue self.class.database_agnostic_connection_not_established => e - logger.log "Error connection not established to the database: #{payload}. Error: #{e.message}" + logger.warn "Error connection not established to the database: #{payload}. Error: #{e.message}" sleep 3 channel.nack(delivery_info.delivery_tag, false, true) rescue StandardError => e - logger.log "Error handling message: #{payload}. Error: #{e.message}" + logger.error "Error handling message: #{payload}. Error: #{e.message}" sleep 3 channel.nack(delivery_info.delivery_tag, false, true) Process.kill('SIGTERM', Process.pid) if kill_to_restart_on_standard_error From aee5a1e3cb86aa2f080ccf99c1ae42a494beadab Mon Sep 17 00:00:00 2001 From: Ari Karim Date: Sun, 25 May 2025 10:56:35 +0300 Subject: [PATCH 2/4] Refactors process ID handling in Rabbit Carrots plugin and enhances logging for shutdown scenarios --- lib/puma/plugin/rabbit_carrots.rb | 5 +++-- lib/rabbit_carrots/configuration.rb | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/puma/plugin/rabbit_carrots.rb b/lib/puma/plugin/rabbit_carrots.rb index 402d64c..e4301ea 100644 --- a/lib/puma/plugin/rabbit_carrots.rb +++ b/lib/puma/plugin/rabbit_carrots.rb @@ -8,7 +8,7 @@ Puma::Plugin.create do def start(launcher) @log_writer = launcher.log_writer - @puma_pid = $$ + @puma_pid = $PROCESS_ID @core_service = RabbitCarrots::Core.new(logger: log_writer) @@ -43,6 +43,7 @@ Puma::Plugin.create do Process.kill('TERM', rabbit_carrots_pid) Process.wait(rabbit_carrots_pid) rescue Errno::ECHILD, Errno::ESRCH + log 'Rabbit Carrots already stopped' end def monitor_puma @@ -57,7 +58,7 @@ Puma::Plugin.create do loop do if send(process_dead) log message - Process.kill('TERM', $$) + Process.kill('TERM', $PROCESS_ID) break end sleep 2 diff --git a/lib/rabbit_carrots/configuration.rb b/lib/rabbit_carrots/configuration.rb index e2308c0..acc7a9b 100644 --- a/lib/rabbit_carrots/configuration.rb +++ b/lib/rabbit_carrots/configuration.rb @@ -18,8 +18,8 @@ module RabbitCarrots :rabbitmq_exchange_name, :automatically_recover, :network_recovery_interval, - :recovery_attempts, - :orm + :recovery_attempts + def orm @orm ||= :activerecord end From 076e1f52ce1c97a94b3888de2bbc5613d4b02c3e Mon Sep 17 00:00:00 2001 From: Ari Karim Date: Sun, 25 May 2025 11:43:41 +0300 Subject: [PATCH 3/4] Enhances logger initialization by introducing a logger adapter for improved compatibility and flexibility --- lib/rabbit_carrots/core.rb | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/rabbit_carrots/core.rb b/lib/rabbit_carrots/core.rb index 78d7ed8..4297092 100644 --- a/lib/rabbit_carrots/core.rb +++ b/lib/rabbit_carrots/core.rb @@ -11,7 +11,7 @@ module RabbitCarrots end def initialize(logger: nil) - @logger = logger || Logger.new(Rails.env.production? ? '/proc/self/fd/1' : $stdout) + @logger = create_logger_adapter(logger || Logger.new(Rails.env.production? ? '/proc/self/fd/1' : $stdout)) @threads = [] @running = true @shutdown_requested = false @@ -115,5 +115,24 @@ module RabbitCarrots logger.error "Bunny session error: #{e.message}" request_shutdown end + + private + + def create_logger_adapter(logger) + return logger if logger.respond_to?(:info) && logger.respond_to?(:error) && logger.respond_to?(:warn) + + adapter = Object.new + def adapter.info(msg) + @logger.write("[INFO] #{msg}\n") + end + def adapter.error(msg) + @logger.write("[ERROR] #{msg}\n") + end + def adapter.warn(msg) + @logger.write("[WARN] #{msg}\n") + end + adapter.instance_variable_set(:@logger, logger) + adapter + end end end From 7cfe2a5a45a35f649884eb0a64ca43d49f7520d2 Mon Sep 17 00:00:00 2001 From: Ari Karim Date: Sun, 25 May 2025 11:44:21 +0300 Subject: [PATCH 4/4] Updates dependencies: bumps rabbit_carrots to 1.0.3, amq-protocol to 2.3.4, bunny to 2.24.0, and set to 1.1.2 --- Gemfile.lock | 10 +++++----- lib/rabbit_carrots/version.rb | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index b8d56bd..5492aa4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - rabbit_carrots (1.0.2) + rabbit_carrots (1.0.3) bunny (>= 2.22) connection_pool (~> 2.4) @@ -18,12 +18,12 @@ GEM minitest (>= 5.1) mutex_m tzinfo (~> 2.0) - amq-protocol (2.3.2) + amq-protocol (2.3.4) ast (2.4.2) base64 (0.2.0) bigdecimal (3.1.4) - bunny (2.22.0) - amq-protocol (~> 2.3, >= 2.3.1) + bunny (2.24.0) + amq-protocol (~> 2.3) sorted_set (~> 1, >= 1.0.2) concurrent-ruby (1.2.2) connection_pool (2.4.1) @@ -80,7 +80,7 @@ GEM rubocop-ast (>= 1.30.0, < 2.0) ruby-progressbar (1.13.0) ruby2_keywords (0.0.5) - set (1.1.0) + set (1.1.2) sorted_set (1.0.3) rbtree set (~> 1.0) diff --git a/lib/rabbit_carrots/version.rb b/lib/rabbit_carrots/version.rb index f0bf878..55c2024 100644 --- a/lib/rabbit_carrots/version.rb +++ b/lib/rabbit_carrots/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module RabbitCarrots - VERSION = '1.0.2' + VERSION = '1.0.3' end