4 Commits

Author SHA1 Message Date
Brusk Awat
a2dd059194 Update README.md 2024-08-16 00:48:01 +03:00
59ad012e04 Updates README.md 2024-08-05 17:42:22 +03:00
3950ff5047 Improves error handling 2024-08-05 15:28:23 +03:00
Brusk Awat
edb1fffad1 Merge pull request #6 from ditkrg/rewrite
Rewrite
2024-08-05 01:07:48 +03:00
8 changed files with 44 additions and 29 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,6 @@
# RabbitCarrots
RabbitCarrots is a simple background task based on rake to handle the consumption of RabbitMQ message in Rails applications. It is an opinionated library that solves the consumption of messages among microservices, given the following conditions:
RabbitCarrots is a simple background worker to handle the consumption of RabbitMQ message in Rails applications. It is an opinionated library that solves the consumption of messages among microservices, given the following conditions:
1. RabbitMQ is used as an event bus for communication.
2. Messages are routed using a single exchange, multiple routing keys.
@@ -36,6 +36,7 @@ RabbitCarrots.configure do |c|
c.automatically_recover = true
c.network_recovery_interval = 5
c.recovery_attempts = 5
c.orm = :activerecord || :mongoid
c.routing_key_mappings = [
{ routing_keys: ['RK1', 'RK2'], queue: 'QUEUE_NAME', handler: 'CLASS HANDLER IN STRING' },
{ routing_keys: ['RK1', 'RK2'], queue: 'QUEUE_NAME', handler: 'CLASS HANDLER IN STRING' }

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