Rescue from common ActiveRecord errors

This commit is contained in:
ari 2023-01-29 14:33:34 +03:00
parent 7081ca0704
commit fc944f59c9
No known key found for this signature in database
GPG Key ID: 1A5559E2E32F1805

View File

@ -47,15 +47,20 @@ def run_task(queue_name:, handler_class:, routing_keys:)
rescue RabbitCarrots::EventHandlers::Errors::NackAndRequeueMessage => _e rescue RabbitCarrots::EventHandlers::Errors::NackAndRequeueMessage => _e
Rails.logger.info "Nacked and Requeued message: #{payload}" Rails.logger.info "Nacked and Requeued message: #{payload}"
channel.nack(delivery_info.delivery_tag, false, true) channel.nack(delivery_info.delivery_tag, false, true)
# rescue all database errors rescue ActiveRecord::NotNullViolation, ActiveRecord::RecordInvalid => e
rescue ActiveRecord::Error => e # on null constraint violation, we want to ack the message
Rails.logger.error "Error handling message: #{payload}. Error: #{e.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 # delay for 3 seconds before requeuing
sleep 3 sleep 3
channel.nack(delivery_info.delivery_tag, false, true) channel.nack(delivery_info.delivery_tag, false, true)
rescue StandardError => e rescue StandardError => e
Rails.logger.error "Error handling message: #{payload}. Error: #{e.message}" Rails.logger.error "Error handling message: #{payload}. Error: #{e.message}"
# requeue the message then kill the container # requeue the message then kill the container
sleep 3
channel.nack(delivery_info.delivery_tag, false, true) channel.nack(delivery_info.delivery_tag, false, true)
# kill the container with sigterm # kill the container with sigterm
Process.kill('SIGTERM', Process.pid) Process.kill('SIGTERM', Process.pid)