mirror of
https://github.com/ditkrg/rabbit_carrots.git
synced 2026-01-22 22:06:40 +00:00
Adds support for puma
This commit is contained in:
parent
28b620b7b4
commit
a51eed8101
84
lib/puma/plugin/rabbit_carrots.rb
Normal file
84
lib/puma/plugin/rabbit_carrots.rb
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
# rabbit_carrots.rb
|
||||||
|
|
||||||
|
require 'puma/plugin'
|
||||||
|
|
||||||
|
Puma::Plugin.create do
|
||||||
|
attr_reader :puma_pid, :rabbit_carrots_pid, :log_writer, :core_service
|
||||||
|
|
||||||
|
def start(launcher)
|
||||||
|
@log_writer = launcher.log_writer
|
||||||
|
@puma_pid = $$
|
||||||
|
|
||||||
|
@core_service = RabbitCarrots::Core.new(logger: log_writer)
|
||||||
|
|
||||||
|
in_background do
|
||||||
|
monitor_rabbit_carrots
|
||||||
|
end
|
||||||
|
|
||||||
|
launcher.events.on_booted do
|
||||||
|
@rabbit_carrots_pid = fork do
|
||||||
|
Thread.new { monitor_puma }
|
||||||
|
start_rabbit_carrots_consumer
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
launcher.events.on_stopped { stop_rabbit_carrots }
|
||||||
|
launcher.events.on_restart { stop_rabbit_carrots }
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def start_rabbit_carrots_consumer
|
||||||
|
core_service.start(kill_to_restart_on_standard_error: true)
|
||||||
|
rescue StandardError => e
|
||||||
|
Rails.logger.error "Error starting Rabbit Carrots: #{e.message}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def stop_rabbit_carrots
|
||||||
|
return unless rabbit_carrots_pid
|
||||||
|
|
||||||
|
log 'Stopping Rabbit Carrots...'
|
||||||
|
core_service.request_shutdown
|
||||||
|
Process.kill('TERM', rabbit_carrots_pid)
|
||||||
|
Process.wait(rabbit_carrots_pid)
|
||||||
|
rescue Errno::ECHILD, Errno::ESRCH
|
||||||
|
end
|
||||||
|
|
||||||
|
def monitor_puma
|
||||||
|
monitor(:puma_dead?, 'Detected Puma has gone away, stopping Rabbit Carrots...')
|
||||||
|
end
|
||||||
|
|
||||||
|
def monitor_rabbit_carrots
|
||||||
|
monitor(:rabbit_carrots_dead?, 'Rabbits Carrot is dead, stopping Puma...')
|
||||||
|
end
|
||||||
|
|
||||||
|
def monitor(process_dead, message)
|
||||||
|
loop do
|
||||||
|
if send(process_dead)
|
||||||
|
log message
|
||||||
|
Process.kill('TERM', $$)
|
||||||
|
break
|
||||||
|
end
|
||||||
|
sleep 2
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def rabbit_carrots_dead?
|
||||||
|
Process.waitpid(rabbit_carrots_pid, Process::WNOHANG) if rabbit_carrots_started?
|
||||||
|
false
|
||||||
|
rescue Errno::ECHILD, Errno::ESRCH
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def rabbit_carrots_started?
|
||||||
|
rabbit_carrots_pid.present?
|
||||||
|
end
|
||||||
|
|
||||||
|
def puma_dead?
|
||||||
|
Process.ppid != puma_pid
|
||||||
|
end
|
||||||
|
|
||||||
|
def log(...)
|
||||||
|
log_writer.log(...)
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -53,7 +53,7 @@ module RabbitCarrots
|
|||||||
def request_shutdown
|
def request_shutdown
|
||||||
# Workaround to a known issue with Signal Traps and logs
|
# Workaround to a known issue with Signal Traps and logs
|
||||||
Thread.start do
|
Thread.start do
|
||||||
logger.info 'Shutting down Rabbit Carrots service...'
|
logger.log 'Shutting down Rabbit Carrots service...'
|
||||||
end
|
end
|
||||||
@shutdown_requested = true
|
@shutdown_requested = true
|
||||||
@threads.each(&:kill)
|
@threads.each(&:kill)
|
||||||
@ -63,7 +63,7 @@ module RabbitCarrots
|
|||||||
def stop
|
def stop
|
||||||
# Workaround to a known issue with Signal Traps and logs
|
# Workaround to a known issue with Signal Traps and logs
|
||||||
Thread.start do
|
Thread.start do
|
||||||
logger.info 'Stoppig the Rabbit Carrots service...'
|
logger.log 'Stoppig the Rabbit Carrots service...'
|
||||||
end
|
end
|
||||||
@running = false
|
@running = false
|
||||||
end
|
end
|
||||||
@ -72,7 +72,7 @@ module RabbitCarrots
|
|||||||
RabbitCarrots::Connection.instance.channel.with do |channel|
|
RabbitCarrots::Connection.instance.channel.with do |channel|
|
||||||
exchange = channel.topic(RabbitCarrots.configuration.rabbitmq_exchange_name, durable: true)
|
exchange = channel.topic(RabbitCarrots.configuration.rabbitmq_exchange_name, durable: true)
|
||||||
|
|
||||||
logger.info "Listening on QUEUE: #{queue_name} for ROUTING KEYS: #{routing_keys}"
|
logger.log "Listening on QUEUE: #{queue_name} for ROUTING KEYS: #{routing_keys}"
|
||||||
queue = channel.queue(queue_name, durable: true, arguments: queue_arguments)
|
queue = channel.queue(queue_name, durable: true, arguments: queue_arguments)
|
||||||
|
|
||||||
routing_keys.map(&:strip).each { |k| queue.bind(exchange, routing_key: k) }
|
routing_keys.map(&:strip).each { |k| queue.bind(exchange, routing_key: k) }
|
||||||
@ -80,30 +80,30 @@ module RabbitCarrots
|
|||||||
queue.subscribe(block: false, manual_ack: true, prefetch: 10) do |delivery_info, properties, payload|
|
queue.subscribe(block: false, manual_ack: true, prefetch: 10) do |delivery_info, properties, payload|
|
||||||
break if @shutdown_requested
|
break if @shutdown_requested
|
||||||
|
|
||||||
logger.info "Received from queue: #{queue_name}, Routing Keys: #{routing_keys}"
|
logger.log "Received from queue: #{queue_name}, Routing Keys: #{routing_keys}"
|
||||||
handler_class.handle!(channel, delivery_info, properties, payload)
|
handler_class.handle!(channel, delivery_info, properties, payload)
|
||||||
channel.ack(delivery_info.delivery_tag, false)
|
channel.ack(delivery_info.delivery_tag, false)
|
||||||
rescue RabbitCarrots::EventHandlers::Errors::NackMessage, JSON::ParserError => _e
|
rescue RabbitCarrots::EventHandlers::Errors::NackMessage, JSON::ParserError => _e
|
||||||
logger.info "Nacked message: #{payload}"
|
logger.log "Nacked message: #{payload}"
|
||||||
channel.nack(delivery_info.delivery_tag, false, false)
|
channel.nack(delivery_info.delivery_tag, false, false)
|
||||||
rescue RabbitCarrots::EventHandlers::Errors::NackAndRequeueMessage => _e
|
rescue RabbitCarrots::EventHandlers::Errors::NackAndRequeueMessage => _e
|
||||||
logger.info "Nacked and Requeued message: #{payload}"
|
logger.log "Nacked and Requeued message: #{payload}"
|
||||||
channel.nack(delivery_info.delivery_tag, false, true)
|
channel.nack(delivery_info.delivery_tag, false, true)
|
||||||
rescue DatabaseAgonsticNotNullViolation, DatabaseAgnosticRecordInvalid => e
|
rescue DatabaseAgonsticNotNullViolation, DatabaseAgnosticRecordInvalid => e
|
||||||
logger.info "Null constraint or Invalid violation: #{payload}. Error: #{e.message}"
|
logger.log "Null constraint or Invalid violation: #{payload}. Error: #{e.message}"
|
||||||
channel.ack(delivery_info.delivery_tag, false)
|
channel.ack(delivery_info.delivery_tag, false)
|
||||||
rescue DatabaseAgonsticConnectionNotEstablished => e
|
rescue DatabaseAgonsticConnectionNotEstablished => e
|
||||||
logger.info "Error connection not established to the database: #{payload}. Error: #{e.message}"
|
logger.log "Error connection not established to the database: #{payload}. Error: #{e.message}"
|
||||||
sleep 3
|
sleep 3
|
||||||
channel.nack(delivery_info.delivery_tag, false, true)
|
channel.nack(delivery_info.delivery_tag, false, true)
|
||||||
rescue StandardError => e
|
rescue StandardError => e
|
||||||
logger.info "Error handling message: #{payload}. Error: #{e.message}"
|
logger.log "Error handling message: #{payload}. Error: #{e.message}"
|
||||||
sleep 3
|
sleep 3
|
||||||
channel.nack(delivery_info.delivery_tag, false, true)
|
channel.nack(delivery_info.delivery_tag, false, true)
|
||||||
Process.kill('SIGTERM', Process.pid) if kill_to_restart_on_standard_error
|
Process.kill('SIGTERM', Process.pid) if kill_to_restart_on_standard_error
|
||||||
end
|
end
|
||||||
|
|
||||||
logger.info "Ending task for queue: #{queue_name}"
|
logger.log "Ending task for queue: #{queue_name}"
|
||||||
end
|
end
|
||||||
rescue StandardError => e
|
rescue StandardError => e
|
||||||
logger.error "Bunny session error: #{e.message}"
|
logger.error "Bunny session error: #{e.message}"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user