Skip to content

Commit

Permalink
Improves error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
broosk1993 committed Aug 5, 2024
1 parent edb1fff commit 3950ff5
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
rabbit_carrots (0.1.20)
rabbit_carrots (1.0.2)
bunny (>= 2.22)
connection_pool (~> 2.4)

Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/puma/plugin/rabbit_carrots.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 2 additions & 17 deletions lib/rabbit_carrots.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion lib/rabbit_carrots/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class Configuration
:rabbitmq_exchange_name,
:automatically_recover,
:network_recovery_interval,
:recovery_attempts
:recovery_attempts,
:orm
def orm
@orm ||= :activerecord
end
end
end
20 changes: 13 additions & 7 deletions lib/rabbit_carrots/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -14,6 +18,10 @@ def initialize(logger: nil)
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
Expand Down Expand Up @@ -89,10 +97,10 @@ def run_task(queue_name:, handler_class:, routing_keys:, queue_arguments: {}, ki
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)
Expand All @@ -102,8 +110,6 @@ def run_task(queue_name:, handler_class:, routing_keys:, queue_arguments: {}, ki
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}"
Expand Down
18 changes: 18 additions & 0 deletions lib/rabbit_carrots/errors.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/rabbit_carrots/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module RabbitCarrots
VERSION = '0.1.20'
VERSION = '1.0.2'
end

0 comments on commit 3950ff5

Please sign in to comment.