Skip to content

Commit a0c5f80

Browse files
authored
Merge pull request #7 from arikarim/dev
Refines logging levels for shutdown, stop, and message handling in Rabbit Carrots service
2 parents 59ad012 + 7cfe2a5 commit a0c5f80

File tree

5 files changed

+40
-20
lines changed

5 files changed

+40
-20
lines changed

Gemfile.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
rabbit_carrots (1.0.2)
4+
rabbit_carrots (1.0.3)
55
bunny (>= 2.22)
66
connection_pool (~> 2.4)
77

@@ -18,12 +18,12 @@ GEM
1818
minitest (>= 5.1)
1919
mutex_m
2020
tzinfo (~> 2.0)
21-
amq-protocol (2.3.2)
21+
amq-protocol (2.3.4)
2222
ast (2.4.2)
2323
base64 (0.2.0)
2424
bigdecimal (3.1.4)
25-
bunny (2.22.0)
26-
amq-protocol (~> 2.3, >= 2.3.1)
25+
bunny (2.24.0)
26+
amq-protocol (~> 2.3)
2727
sorted_set (~> 1, >= 1.0.2)
2828
concurrent-ruby (1.2.2)
2929
connection_pool (2.4.1)
@@ -80,7 +80,7 @@ GEM
8080
rubocop-ast (>= 1.30.0, < 2.0)
8181
ruby-progressbar (1.13.0)
8282
ruby2_keywords (0.0.5)
83-
set (1.1.0)
83+
set (1.1.2)
8484
sorted_set (1.0.3)
8585
rbtree
8686
set (~> 1.0)

lib/puma/plugin/rabbit_carrots.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
def start(launcher)
1010
@log_writer = launcher.log_writer
11-
@puma_pid = $$
11+
@puma_pid = $PROCESS_ID
1212

1313
@core_service = RabbitCarrots::Core.new(logger: log_writer)
1414

@@ -43,6 +43,7 @@ def stop_rabbit_carrots
4343
Process.kill('TERM', rabbit_carrots_pid)
4444
Process.wait(rabbit_carrots_pid)
4545
rescue Errno::ECHILD, Errno::ESRCH
46+
log 'Rabbit Carrots already stopped'
4647
end
4748

4849
def monitor_puma
@@ -57,7 +58,7 @@ def monitor(process_dead, message)
5758
loop do
5859
if send(process_dead)
5960
log message
60-
Process.kill('TERM', $$)
61+
Process.kill('TERM', $PROCESS_ID)
6162
break
6263
end
6364
sleep 2

lib/rabbit_carrots/configuration.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class Configuration
1818
:rabbitmq_exchange_name,
1919
:automatically_recover,
2020
:network_recovery_interval,
21-
:recovery_attempts,
22-
:orm
21+
:recovery_attempts
22+
2323
def orm
2424
@orm ||= :activerecord
2525
end

lib/rabbit_carrots/core.rb

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class << self
1111
end
1212

1313
def initialize(logger: nil)
14-
@logger = logger || Logger.new(Rails.env.production? ? '/proc/self/fd/1' : $stdout)
14+
@logger = create_logger_adapter(logger || Logger.new(Rails.env.production? ? '/proc/self/fd/1' : $stdout))
1515
@threads = []
1616
@running = true
1717
@shutdown_requested = false
@@ -61,7 +61,7 @@ def start(kill_to_restart_on_standard_error: false)
6161
def request_shutdown
6262
# Workaround to a known issue with Signal Traps and logs
6363
Thread.start do
64-
logger.log 'Shutting down Rabbit Carrots service...'
64+
logger.error 'Shutting down Rabbit Carrots service...'
6565
end
6666
@shutdown_requested = true
6767
@threads.each(&:kill)
@@ -71,7 +71,7 @@ def request_shutdown
7171
def stop
7272
# Workaround to a known issue with Signal Traps and logs
7373
Thread.start do
74-
logger.log 'Stoppig the Rabbit Carrots service...'
74+
logger.error 'Stoppig the Rabbit Carrots service...'
7575
end
7676
@running = false
7777
end
@@ -80,32 +80,32 @@ def run_task(queue_name:, handler_class:, routing_keys:, queue_arguments: {}, ki
8080
RabbitCarrots::Connection.instance.channel.with do |channel|
8181
exchange = channel.topic(RabbitCarrots.configuration.rabbitmq_exchange_name, durable: true)
8282

83-
logger.log "Listening on QUEUE: #{queue_name} for ROUTING KEYS: #{routing_keys}"
83+
logger.info "Listening on QUEUE: #{queue_name} for ROUTING KEYS: #{routing_keys}"
8484
queue = channel.queue(queue_name, durable: true, arguments: queue_arguments)
8585

8686
routing_keys.map(&:strip).each { |k| queue.bind(exchange, routing_key: k) }
8787

8888
queue.subscribe(block: false, manual_ack: true, prefetch: 10) do |delivery_info, properties, payload|
8989
break if @shutdown_requested
9090

91-
logger.log "Received from queue: #{queue_name}, Routing Keys: #{routing_keys}"
91+
logger.info "Received from queue: #{queue_name}, Routing Keys: #{routing_keys}"
9292
handler_class.handle!(channel, delivery_info, properties, payload)
9393
channel.ack(delivery_info.delivery_tag, false)
9494
rescue RabbitCarrots::EventHandlers::Errors::NackMessage, JSON::ParserError => _e
95-
logger.log "Nacked message: #{payload}"
95+
logger.warn "Nacked message: #{payload}"
9696
channel.nack(delivery_info.delivery_tag, false, false)
9797
rescue RabbitCarrots::EventHandlers::Errors::NackAndRequeueMessage => _e
98-
logger.log "Nacked and Requeued message: #{payload}"
98+
logger.warn "Nacked and Requeued message: #{payload}"
9999
channel.nack(delivery_info.delivery_tag, false, true)
100100
rescue self.class.database_agnostic_not_null_violation, self.class.database_agnostic_record_invalid => e
101-
logger.log "Null constraint or Invalid violation: #{payload}. Error: #{e.message}"
101+
logger.warn "Null constraint or Invalid violation: #{payload}. Error: #{e.message}"
102102
channel.ack(delivery_info.delivery_tag, false)
103103
rescue self.class.database_agnostic_connection_not_established => e
104-
logger.log "Error connection not established to the database: #{payload}. Error: #{e.message}"
104+
logger.warn "Error connection not established to the database: #{payload}. Error: #{e.message}"
105105
sleep 3
106106
channel.nack(delivery_info.delivery_tag, false, true)
107107
rescue StandardError => e
108-
logger.log "Error handling message: #{payload}. Error: #{e.message}"
108+
logger.error "Error handling message: #{payload}. Error: #{e.message}"
109109
sleep 3
110110
channel.nack(delivery_info.delivery_tag, false, true)
111111
Process.kill('SIGTERM', Process.pid) if kill_to_restart_on_standard_error
@@ -115,5 +115,24 @@ def run_task(queue_name:, handler_class:, routing_keys:, queue_arguments: {}, ki
115115
logger.error "Bunny session error: #{e.message}"
116116
request_shutdown
117117
end
118+
119+
private
120+
121+
def create_logger_adapter(logger)
122+
return logger if logger.respond_to?(:info) && logger.respond_to?(:error) && logger.respond_to?(:warn)
123+
124+
adapter = Object.new
125+
def adapter.info(msg)
126+
@logger.write("[INFO] #{msg}\n")
127+
end
128+
def adapter.error(msg)
129+
@logger.write("[ERROR] #{msg}\n")
130+
end
131+
def adapter.warn(msg)
132+
@logger.write("[WARN] #{msg}\n")
133+
end
134+
adapter.instance_variable_set(:@logger, logger)
135+
adapter
136+
end
118137
end
119138
end

lib/rabbit_carrots/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module RabbitCarrots
4-
VERSION = '1.0.2'
4+
VERSION = '1.0.3'
55
end

0 commit comments

Comments
 (0)