Improves error handling

This commit is contained in:
Brusk Awat 2024-08-05 15:28:23 +03:00
parent edb1fffad1
commit 3950ff5047
Signed by: broosk1993
GPG Key ID: 5D20F7E02649F74E
7 changed files with 42 additions and 28 deletions

View File

@ -1,7 +1,7 @@
PATH
remote: .
specs:
rabbit_carrots (0.1.20)
rabbit_carrots (1.0.2)
bunny (>= 2.22)
connection_pool (~> 2.4)
@ -80,7 +80,7 @@ GEM
rubocop-ast (>= 1.30.0, < 2.0)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
set (1.0.3)
set (1.1.0)
sorted_set (1.0.3)
rbtree
set (~> 1.0)

View File

@ -1,6 +1,7 @@
# rabbit_carrots.rb
require 'puma/plugin'
require 'rabbit_carrots'
Puma::Plugin.create do
attr_reader :puma_pid, :rabbit_carrots_pid, :log_writer, :core_service

View File

@ -1,26 +1,11 @@
# frozen_string_literal: true
require_relative 'rabbit_carrots/version'
require 'rabbit_carrots/errors'
require 'rabbit_carrots/connection'
require 'rabbit_carrots/core'
require 'rabbit_carrots/configuration'
require 'rabbit_carrots/railtie' if defined?(Rails)
require 'rabbit_carrots/core'
module RabbitCarrots
class Error < StandardError; end
module EventHandlers
module Errors
class IrrelevantMessage < StandardError
end
class NackMessage < StandardError
end
class NackAndRequeueMessage < StandardError
end
class PlaceholderError < Error; end
end
end
end

View File

@ -18,6 +18,10 @@ module RabbitCarrots
:rabbitmq_exchange_name,
:automatically_recover,
:network_recovery_interval,
:recovery_attempts
:recovery_attempts,
:orm
def orm
@orm ||= :activerecord
end
end
end

View File

@ -2,9 +2,13 @@ module RabbitCarrots
class Core
attr_reader :logger
DatabaseAgonsticNotNullViolation = defined?(ActiveRecord) ? ActiveRecord::NotNullViolation : RabbitCarrots::EventHandlers::Errors::PlaceholderError
DatabaseAgonsticConnectionNotEstablished = defined?(ActiveRecord) ? ActiveRecord::ConnectionNotEstablished : Mongo::Error::SocketError
DatabaseAgnosticRecordInvalid = defined?(ActiveRecord) ? ActiveRecord::RecordInvalid : Mongoid::Errors::Validations
@database_agnostic_not_null_violation = nil
@database_agnostic_connection_not_established = nil
@database_agnostic_record_invalid = nil
class << self
attr_accessor :database_agnostic_not_null_violation, :database_agnostic_connection_not_established, :database_agnostic_record_invalid
end
def initialize(logger: nil)
@logger = logger || Logger.new(Rails.env.production? ? '/proc/self/fd/1' : $stdout)
@ -14,6 +18,10 @@ module RabbitCarrots
end
def start(kill_to_restart_on_standard_error: false)
self.class.database_agnostic_not_null_violation = RabbitCarrots.configuration.orm == :activerecord ? ActiveRecord::NotNullViolation : RabbitCarrots::EventHandlers::Errors::PlaceholderError
self.class.database_agnostic_connection_not_established = RabbitCarrots.configuration.orm == :activerecord ? ActiveRecord::ConnectionNotEstablished : ::Mongo::Error::SocketError
self.class.database_agnostic_record_invalid = RabbitCarrots.configuration.orm == :activerecord ? ActiveRecord::RecordInvalid : ::Mongoid::Errors::Validations
channels = RabbitCarrots.configuration.routing_key_mappings.map do |mapping|
{ **mapping, handler: mapping[:handler].constantize }
end
@ -89,10 +97,10 @@ module RabbitCarrots
rescue RabbitCarrots::EventHandlers::Errors::NackAndRequeueMessage => _e
logger.log "Nacked and Requeued message: #{payload}"
channel.nack(delivery_info.delivery_tag, false, true)
rescue DatabaseAgonsticNotNullViolation, DatabaseAgnosticRecordInvalid => e
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}"
channel.ack(delivery_info.delivery_tag, false)
rescue DatabaseAgonsticConnectionNotEstablished => e
rescue self.class.database_agnostic_connection_not_established => e
logger.log "Error connection not established to the database: #{payload}. Error: #{e.message}"
sleep 3
channel.nack(delivery_info.delivery_tag, false, true)
@ -102,8 +110,6 @@ module RabbitCarrots
channel.nack(delivery_info.delivery_tag, false, true)
Process.kill('SIGTERM', Process.pid) if kill_to_restart_on_standard_error
end
logger.log "Ending task for queue: #{queue_name}"
end
rescue StandardError => e
logger.error "Bunny session error: #{e.message}"

View File

@ -0,0 +1,18 @@
module RabbitCarrots
class Error < StandardError; end
module EventHandlers
module Errors
class IrrelevantMessage < StandardError
end
class NackMessage < StandardError
end
class NackAndRequeueMessage < StandardError
end
class PlaceholderError < Error; end
end
end
end

View File

@ -1,5 +1,5 @@
# frozen_string_literal: true
module RabbitCarrots
VERSION = '0.1.20'
VERSION = '1.0.2'
end