From 7081ca0704f889fffd4e2c9e3ddb8df8fc466022 Mon Sep 17 00:00:00 2001 From: ari Date: Sun, 29 Jan 2023 12:13:52 +0300 Subject: [PATCH 1/2] Adds rescue from database related errors then delay --- lib/rabbit_carrots/tasks/rmq.rake | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/rabbit_carrots/tasks/rmq.rake b/lib/rabbit_carrots/tasks/rmq.rake index 3fc3698..8d7a755 100644 --- a/lib/rabbit_carrots/tasks/rmq.rake +++ b/lib/rabbit_carrots/tasks/rmq.rake @@ -47,9 +47,18 @@ def run_task(queue_name:, handler_class:, routing_keys:) rescue RabbitCarrots::EventHandlers::Errors::NackAndRequeueMessage => _e Rails.logger.info "Nacked and Requeued message: #{payload}" channel.nack(delivery_info.delivery_tag, false, true) + # rescue all database errors + rescue ActiveRecord::Error => e + Rails.logger.error "Error handling message: #{payload}. Error: #{e.message}" + # delay for 3 seconds before requeuing + sleep 3 + channel.nack(delivery_info.delivery_tag, false, true) rescue StandardError => e Rails.logger.error "Error handling message: #{payload}. Error: #{e.message}" + # requeue the message then kill the container channel.nack(delivery_info.delivery_tag, false, true) + # kill the container with sigterm + Process.kill('SIGTERM', Process.pid) end Rails.logger.info 'RUN TASK ENDED' From fc944f59c923e9801d720c53e18b92c6bac9eb1a Mon Sep 17 00:00:00 2001 From: ari Date: Sun, 29 Jan 2023 14:33:34 +0300 Subject: [PATCH 2/2] Rescue from common ActiveRecord errors --- lib/rabbit_carrots/tasks/rmq.rake | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/rabbit_carrots/tasks/rmq.rake b/lib/rabbit_carrots/tasks/rmq.rake index 8d7a755..16caa7c 100644 --- a/lib/rabbit_carrots/tasks/rmq.rake +++ b/lib/rabbit_carrots/tasks/rmq.rake @@ -47,15 +47,20 @@ def run_task(queue_name:, handler_class:, routing_keys:) rescue RabbitCarrots::EventHandlers::Errors::NackAndRequeueMessage => _e Rails.logger.info "Nacked and Requeued message: #{payload}" channel.nack(delivery_info.delivery_tag, false, true) - # rescue all database errors - rescue ActiveRecord::Error => e - Rails.logger.error "Error handling message: #{payload}. Error: #{e.message}" + rescue ActiveRecord::NotNullViolation, ActiveRecord::RecordInvalid => e + # on null constraint violation, we want to ack the message + Rails.logger.error "Null constraint or Invalid violation: #{payload}. Error: #{e.message}" + channel.ack(delivery_info.delivery_tag, false) + rescue ActiveRecord::ConnectionNotEstablished => e + # on connection not established, we want to requeue the message and sleep for 3 seconds + Rails.logger.error "Error connection not established to the database: #{payload}. Error: #{e.message}" # delay for 3 seconds before requeuing sleep 3 channel.nack(delivery_info.delivery_tag, false, true) rescue StandardError => e Rails.logger.error "Error handling message: #{payload}. Error: #{e.message}" # requeue the message then kill the container + sleep 3 channel.nack(delivery_info.delivery_tag, false, true) # kill the container with sigterm Process.kill('SIGTERM', Process.pid)