mirror of
https://github.com/ditkrg/rabbit_carrots.git
synced 2026-01-25 15:12:55 +00:00
Improves error handling
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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}"
|
||||
|
||||
18
lib/rabbit_carrots/errors.rb
Normal file
18
lib/rabbit_carrots/errors.rb
Normal 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
|
||||
@@ -1,5 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module RabbitCarrots
|
||||
VERSION = '0.1.20'
|
||||
VERSION = '1.0.2'
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user