From 3950ff5047e7b35a3f06fa203d233ae9efec59dd Mon Sep 17 00:00:00 2001 From: Brusk Awat Date: Mon, 5 Aug 2024 15:28:23 +0300 Subject: [PATCH] Improves error handling --- Gemfile.lock | 4 ++-- lib/puma/plugin/rabbit_carrots.rb | 1 + lib/rabbit_carrots.rb | 19 ++----------------- lib/rabbit_carrots/configuration.rb | 6 +++++- lib/rabbit_carrots/core.rb | 20 +++++++++++++------- lib/rabbit_carrots/errors.rb | 18 ++++++++++++++++++ lib/rabbit_carrots/version.rb | 2 +- 7 files changed, 42 insertions(+), 28 deletions(-) create mode 100644 lib/rabbit_carrots/errors.rb diff --git a/Gemfile.lock b/Gemfile.lock index 5cfd915..b8d56bd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) diff --git a/lib/puma/plugin/rabbit_carrots.rb b/lib/puma/plugin/rabbit_carrots.rb index d49ce86..402d64c 100644 --- a/lib/puma/plugin/rabbit_carrots.rb +++ b/lib/puma/plugin/rabbit_carrots.rb @@ -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 diff --git a/lib/rabbit_carrots.rb b/lib/rabbit_carrots.rb index 2f0a2c4..2f89d56 100644 --- a/lib/rabbit_carrots.rb +++ b/lib/rabbit_carrots.rb @@ -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 diff --git a/lib/rabbit_carrots/configuration.rb b/lib/rabbit_carrots/configuration.rb index f1e4d92..e2308c0 100644 --- a/lib/rabbit_carrots/configuration.rb +++ b/lib/rabbit_carrots/configuration.rb @@ -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 diff --git a/lib/rabbit_carrots/core.rb b/lib/rabbit_carrots/core.rb index dba740d..f72931f 100644 --- a/lib/rabbit_carrots/core.rb +++ b/lib/rabbit_carrots/core.rb @@ -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}" diff --git a/lib/rabbit_carrots/errors.rb b/lib/rabbit_carrots/errors.rb new file mode 100644 index 0000000..8b55392 --- /dev/null +++ b/lib/rabbit_carrots/errors.rb @@ -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 diff --git a/lib/rabbit_carrots/version.rb b/lib/rabbit_carrots/version.rb index e036b5e..f0bf878 100644 --- a/lib/rabbit_carrots/version.rb +++ b/lib/rabbit_carrots/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module RabbitCarrots - VERSION = '0.1.20' + VERSION = '1.0.2' end