rabbit_carrots 0.1.19 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +4 -4
- data/Gemfile.lock +46 -30
- data/README.md +13 -6
- data/lib/puma/plugin/rabbit_carrots.rb +84 -0
- data/lib/rabbit_carrots/configuration.rb +10 -1
- data/lib/rabbit_carrots/connection.rb +5 -1
- data/lib/rabbit_carrots/core.rb +113 -0
- data/lib/rabbit_carrots/tasks/rmq.rake +5 -67
- data/lib/rabbit_carrots/version.rb +1 -1
- data/lib/rabbit_carrots.rb +1 -0
- metadata +9 -8
- data/rabbit_carrots.gemspec +0 -41
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5041ccacc59130b82ec1059d5b1f2803e3ef6f7a0ac3e09cbe8120a52c6071ee
|
4
|
+
data.tar.gz: 25fa5dbb545b6217a6c9a74c1ca6aaa75e86e2ba189cded3eeb1bcd93cfd1418
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 75eb397c461cc2acf4a0d8f1ba9673f49d84dd9344681218cc2a5e800eaa979e46f24ec0ecb3fbfe6dc73d25a5c3ad72e79e4256a15fe356a2fa04366402db69
|
7
|
+
data.tar.gz: a2a900c8d6663e5ed2d3a022e36fd1baa4ee7fbf7c7b531bde2ae1d0efa4c023cc7d5b1b007e8bbb9c82c20edaff9dd7f1c08f499a4e03d342d1915adf26836e
|
data/Gemfile
CHANGED
@@ -5,10 +5,10 @@ source 'https://rubygems.org'
|
|
5
5
|
# Specify your gem's dependencies in rabbit_carrots.gemspec
|
6
6
|
gemspec
|
7
7
|
|
8
|
-
gem 'rake', '~> 13.
|
8
|
+
gem 'rake', '~> 13.1'
|
9
9
|
|
10
|
-
gem 'rspec', '~> 3.
|
10
|
+
gem 'rspec', '~> 3.12'
|
11
11
|
|
12
|
-
gem 'rubocop', '~> 1.
|
12
|
+
gem 'rubocop', '~> 1.58'
|
13
13
|
|
14
|
-
gem 'rubocop-rails', '~> 2.
|
14
|
+
gem 'rubocop-rails', '~> 2.22'
|
data/Gemfile.lock
CHANGED
@@ -1,39 +1,52 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
rabbit_carrots (0.
|
5
|
-
bunny (>= 2.
|
6
|
-
connection_pool (~> 2.
|
4
|
+
rabbit_carrots (1.0.0)
|
5
|
+
bunny (>= 2.22)
|
6
|
+
connection_pool (~> 2.4)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
activesupport (7.
|
11
|
+
activesupport (7.1.2)
|
12
|
+
base64
|
13
|
+
bigdecimal
|
12
14
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
15
|
+
connection_pool (>= 2.2.5)
|
16
|
+
drb
|
13
17
|
i18n (>= 1.6, < 2)
|
14
18
|
minitest (>= 5.1)
|
19
|
+
mutex_m
|
15
20
|
tzinfo (~> 2.0)
|
16
21
|
amq-protocol (2.3.2)
|
17
22
|
ast (2.4.2)
|
23
|
+
base64 (0.2.0)
|
24
|
+
bigdecimal (3.1.4)
|
18
25
|
bunny (2.22.0)
|
19
26
|
amq-protocol (~> 2.3, >= 2.3.1)
|
20
27
|
sorted_set (~> 1, >= 1.0.2)
|
21
|
-
concurrent-ruby (1.
|
22
|
-
connection_pool (2.
|
28
|
+
concurrent-ruby (1.2.2)
|
29
|
+
connection_pool (2.4.1)
|
23
30
|
diff-lcs (1.5.0)
|
24
|
-
|
31
|
+
drb (2.2.0)
|
32
|
+
ruby2_keywords
|
33
|
+
i18n (1.14.1)
|
25
34
|
concurrent-ruby (~> 1.0)
|
26
|
-
json (2.
|
27
|
-
|
28
|
-
|
29
|
-
|
35
|
+
json (2.7.1)
|
36
|
+
language_server-protocol (3.17.0.3)
|
37
|
+
minitest (5.20.0)
|
38
|
+
mutex_m (0.2.0)
|
39
|
+
parallel (1.23.0)
|
40
|
+
parser (3.2.2.4)
|
30
41
|
ast (~> 2.4.1)
|
31
|
-
|
42
|
+
racc
|
43
|
+
racc (1.7.3)
|
44
|
+
rack (3.0.8)
|
32
45
|
rainbow (3.1.1)
|
33
|
-
rake (13.0
|
46
|
+
rake (13.1.0)
|
34
47
|
rbtree (0.4.6)
|
35
|
-
regexp_parser (2.
|
36
|
-
rexml (3.2.
|
48
|
+
regexp_parser (2.8.3)
|
49
|
+
rexml (3.2.6)
|
37
50
|
rspec (3.12.0)
|
38
51
|
rspec-core (~> 3.12.0)
|
39
52
|
rspec-expectations (~> 3.12.0)
|
@@ -47,40 +60,43 @@ GEM
|
|
47
60
|
diff-lcs (>= 1.2.0, < 2.0)
|
48
61
|
rspec-support (~> 3.12.0)
|
49
62
|
rspec-support (3.12.0)
|
50
|
-
rubocop (1.
|
63
|
+
rubocop (1.58.0)
|
51
64
|
json (~> 2.3)
|
65
|
+
language_server-protocol (>= 3.17.0)
|
52
66
|
parallel (~> 1.10)
|
53
|
-
parser (>= 3.
|
67
|
+
parser (>= 3.2.2.4)
|
54
68
|
rainbow (>= 2.2.2, < 4.0)
|
55
69
|
regexp_parser (>= 1.8, < 3.0)
|
56
70
|
rexml (>= 3.2.5, < 4.0)
|
57
|
-
rubocop-ast (>= 1.
|
71
|
+
rubocop-ast (>= 1.30.0, < 2.0)
|
58
72
|
ruby-progressbar (~> 1.7)
|
59
|
-
unicode-display_width (>=
|
60
|
-
rubocop-ast (1.
|
61
|
-
parser (>= 3.
|
62
|
-
rubocop-rails (2.
|
73
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
74
|
+
rubocop-ast (1.30.0)
|
75
|
+
parser (>= 3.2.1.0)
|
76
|
+
rubocop-rails (2.22.2)
|
63
77
|
activesupport (>= 4.2.0)
|
64
78
|
rack (>= 1.1)
|
65
79
|
rubocop (>= 1.33.0, < 2.0)
|
66
|
-
|
67
|
-
|
80
|
+
rubocop-ast (>= 1.30.0, < 2.0)
|
81
|
+
ruby-progressbar (1.13.0)
|
82
|
+
ruby2_keywords (0.0.5)
|
83
|
+
set (1.1.0)
|
68
84
|
sorted_set (1.0.3)
|
69
85
|
rbtree
|
70
86
|
set (~> 1.0)
|
71
|
-
tzinfo (2.0.
|
87
|
+
tzinfo (2.0.6)
|
72
88
|
concurrent-ruby (~> 1.0)
|
73
|
-
unicode-display_width (2.
|
89
|
+
unicode-display_width (2.5.0)
|
74
90
|
|
75
91
|
PLATFORMS
|
76
92
|
x86_64-linux
|
77
93
|
|
78
94
|
DEPENDENCIES
|
79
95
|
rabbit_carrots!
|
80
|
-
rake (~> 13.
|
81
|
-
rspec (~> 3.
|
82
|
-
rubocop (~> 1.
|
83
|
-
rubocop-rails (~> 2.
|
96
|
+
rake (~> 13.1)
|
97
|
+
rspec (~> 3.12)
|
98
|
+
rubocop (~> 1.58)
|
99
|
+
rubocop-rails (~> 2.22)
|
84
100
|
|
85
101
|
BUNDLED WITH
|
86
102
|
2.3.26
|
data/README.md
CHANGED
@@ -32,7 +32,10 @@ RabbitCarrots.configure do |c|
|
|
32
32
|
c.rabbitmq_user = ENV.fetch('RABBITMQ__USER', nil)
|
33
33
|
c.rabbitmq_password = ENV.fetch('RABBITMQ__PASSWORD', nil)
|
34
34
|
c.rabbitmq_vhost = ENV.fetch('RABBITMQ__VHOST', nil)
|
35
|
-
c.
|
35
|
+
c.rabbitmq_exchange_name = ENV.fetch('RABBITMQ__EXCHANGE_NAME', nil)
|
36
|
+
c.automatically_recover = true
|
37
|
+
c.network_recovery_interval = 5
|
38
|
+
c.recovery_attempts = 5
|
36
39
|
c.routing_key_mappings = [
|
37
40
|
{ routing_keys: ['RK1', 'RK2'], queue: 'QUEUE_NAME', handler: 'CLASS HANDLER IN STRING' },
|
38
41
|
{ routing_keys: ['RK1', 'RK2'], queue: 'QUEUE_NAME', handler: 'CLASS HANDLER IN STRING' }
|
@@ -41,8 +44,6 @@ end
|
|
41
44
|
|
42
45
|
```
|
43
46
|
|
44
|
-
|
45
|
-
|
46
47
|
Note that handler is a class that must implement a method named ```handle!``` that takes 4 parameters as follow:
|
47
48
|
|
48
49
|
```ruby
|
@@ -53,8 +54,6 @@ class DummyEventHandler
|
|
53
54
|
end
|
54
55
|
```
|
55
56
|
|
56
|
-
|
57
|
-
|
58
57
|
Inside the handle message, you can NACK the message without re-queuing by raising ```RabbitCarrots::EventHandlers::Errors::NackMessage``` exception.
|
59
58
|
|
60
59
|
To NACK and re-queue, raise ```RabbitCarrots::EventHandlers::Errors::NackAndRequeueMessage``` exception.
|
@@ -65,8 +64,16 @@ Note: Any other unrescued exception raised inside ```handle!``` the that is a su
|
|
65
64
|
|
66
65
|
### Running
|
67
66
|
|
68
|
-
|
67
|
+
For better scalability and improved performance, you can run rabbit_carrots in standalone mode by invoking the following command:
|
68
|
+
```bundle exec rake rabbit_carrots:eat```.
|
69
|
+
|
70
|
+
#### Puma
|
71
|
+
|
72
|
+
For small and medium sized projects, you can delegate the management of the rabbit_carrots to the Puma web server. To achieve that, add the following line to your puma.rb
|
73
|
+
|
74
|
+
```plugin :rabbit_carrots```
|
69
75
|
|
76
|
+
This will make sure that Puma will manage rabbit carrots as a background service and will gracefully terminate if rabbit_carrots eventually loses connection after multiple automatic recovery.
|
70
77
|
## Development
|
71
78
|
|
72
79
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# rabbit_carrots.rb
|
2
|
+
|
3
|
+
require 'puma/plugin'
|
4
|
+
|
5
|
+
Puma::Plugin.create do
|
6
|
+
attr_reader :puma_pid, :rabbit_carrots_pid, :log_writer, :core_service
|
7
|
+
|
8
|
+
def start(launcher)
|
9
|
+
@log_writer = launcher.log_writer
|
10
|
+
@puma_pid = $$
|
11
|
+
|
12
|
+
@core_service = RabbitCarrots::Core.new(logger: log_writer)
|
13
|
+
|
14
|
+
in_background do
|
15
|
+
monitor_rabbit_carrots
|
16
|
+
end
|
17
|
+
|
18
|
+
launcher.events.on_booted do
|
19
|
+
@rabbit_carrots_pid = fork do
|
20
|
+
Thread.new { monitor_puma }
|
21
|
+
start_rabbit_carrots_consumer
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
launcher.events.on_stopped { stop_rabbit_carrots }
|
26
|
+
launcher.events.on_restart { stop_rabbit_carrots }
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def start_rabbit_carrots_consumer
|
32
|
+
core_service.start(kill_to_restart_on_standard_error: true)
|
33
|
+
rescue StandardError => e
|
34
|
+
Rails.logger.error "Error starting Rabbit Carrots: #{e.message}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def stop_rabbit_carrots
|
38
|
+
return unless rabbit_carrots_pid
|
39
|
+
|
40
|
+
log 'Stopping Rabbit Carrots...'
|
41
|
+
core_service.request_shutdown
|
42
|
+
Process.kill('TERM', rabbit_carrots_pid)
|
43
|
+
Process.wait(rabbit_carrots_pid)
|
44
|
+
rescue Errno::ECHILD, Errno::ESRCH
|
45
|
+
end
|
46
|
+
|
47
|
+
def monitor_puma
|
48
|
+
monitor(:puma_dead?, 'Detected Puma has gone away, stopping Rabbit Carrots...')
|
49
|
+
end
|
50
|
+
|
51
|
+
def monitor_rabbit_carrots
|
52
|
+
monitor(:rabbit_carrots_dead?, 'Rabbits Carrot is dead, stopping Puma...')
|
53
|
+
end
|
54
|
+
|
55
|
+
def monitor(process_dead, message)
|
56
|
+
loop do
|
57
|
+
if send(process_dead)
|
58
|
+
log message
|
59
|
+
Process.kill('TERM', $$)
|
60
|
+
break
|
61
|
+
end
|
62
|
+
sleep 2
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def rabbit_carrots_dead?
|
67
|
+
Process.waitpid(rabbit_carrots_pid, Process::WNOHANG) if rabbit_carrots_started?
|
68
|
+
false
|
69
|
+
rescue Errno::ECHILD, Errno::ESRCH
|
70
|
+
true
|
71
|
+
end
|
72
|
+
|
73
|
+
def rabbit_carrots_started?
|
74
|
+
rabbit_carrots_pid.present?
|
75
|
+
end
|
76
|
+
|
77
|
+
def puma_dead?
|
78
|
+
Process.ppid != puma_pid
|
79
|
+
end
|
80
|
+
|
81
|
+
def log(...)
|
82
|
+
log_writer.log(...)
|
83
|
+
end
|
84
|
+
end
|
@@ -9,6 +9,15 @@ module RabbitCarrots
|
|
9
9
|
end
|
10
10
|
|
11
11
|
class Configuration
|
12
|
-
attr_accessor :rabbitmq_host,
|
12
|
+
attr_accessor :rabbitmq_host,
|
13
|
+
:rabbitmq_port,
|
14
|
+
:rabbitmq_user,
|
15
|
+
:rabbitmq_password,
|
16
|
+
:rabbitmq_vhost,
|
17
|
+
:routing_key_mappings,
|
18
|
+
:rabbitmq_exchange_name,
|
19
|
+
:automatically_recover,
|
20
|
+
:network_recovery_interval,
|
21
|
+
:recovery_attempts
|
13
22
|
end
|
14
23
|
end
|
@@ -11,7 +11,11 @@ module RabbitCarrots
|
|
11
11
|
port: RabbitCarrots.configuration.rabbitmq_port,
|
12
12
|
user: RabbitCarrots.configuration.rabbitmq_user,
|
13
13
|
password: RabbitCarrots.configuration.rabbitmq_password,
|
14
|
-
vhost: RabbitCarrots.configuration.rabbitmq_vhost
|
14
|
+
vhost: RabbitCarrots.configuration.rabbitmq_vhost,
|
15
|
+
automatically_recover: RabbitCarrots.configuration.automatically_recover || true,
|
16
|
+
network_recovery_interval: RabbitCarrots.configuration.network_recovery_interval || 5,
|
17
|
+
recovery_attempts: RabbitCarrots.configuration.recovery_attempts || 5,
|
18
|
+
recovery_attempts_exhausted: -> { Process.kill('TERM', Process.pid) }
|
15
19
|
)
|
16
20
|
|
17
21
|
@connection.start
|
@@ -0,0 +1,113 @@
|
|
1
|
+
module RabbitCarrots
|
2
|
+
class Core
|
3
|
+
attr_reader :logger
|
4
|
+
|
5
|
+
DatabaseAgonsticNotNullViolation = defined?(ActiveRecord) ? ActiveRecord::NotNullViolation : RabbitCarrots::EventHandlers::Errors::PlaceholderError
|
6
|
+
DatabaseAgonsticConnectionNotEstablished = defined?(ActiveRecord) ? ActiveRecord::ConnectionNotEstablished : Mongo::Error::SocketError
|
7
|
+
DatabaseAgnosticRecordInvalid = defined?(ActiveRecord) ? ActiveRecord::RecordInvalid : Mongoid::Errors::Validations
|
8
|
+
|
9
|
+
def initialize(logger: nil)
|
10
|
+
@logger = logger || Logger.new(Rails.env.production? ? '/proc/self/fd/1' : $stdout)
|
11
|
+
@threads = []
|
12
|
+
@running = true
|
13
|
+
@shutdown_requested = false
|
14
|
+
end
|
15
|
+
|
16
|
+
def start(kill_to_restart_on_standard_error: false)
|
17
|
+
channels = RabbitCarrots.configuration.routing_key_mappings.map do |mapping|
|
18
|
+
{ **mapping, handler: mapping[:handler].constantize }
|
19
|
+
end
|
20
|
+
|
21
|
+
channels.each do |channel|
|
22
|
+
handler_class = channel[:handler]
|
23
|
+
raise "#{handler_class.name} must respond to `handle!`" unless handler_class.respond_to?(:handle!)
|
24
|
+
|
25
|
+
@threads << Thread.new do
|
26
|
+
run_task(
|
27
|
+
queue_name: channel[:queue],
|
28
|
+
handler_class:,
|
29
|
+
routing_keys: channel[:routing_keys],
|
30
|
+
queue_arguments: channel[:arguments],
|
31
|
+
kill_to_restart_on_standard_error:
|
32
|
+
)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
Signal.trap('INT') { request_shutdown }
|
37
|
+
Signal.trap('TERM') { request_shutdown }
|
38
|
+
|
39
|
+
while @running
|
40
|
+
if @shutdown_requested
|
41
|
+
request_shutdown
|
42
|
+
sleep 1
|
43
|
+
break
|
44
|
+
end
|
45
|
+
sleep 1
|
46
|
+
end
|
47
|
+
|
48
|
+
@threads.each(&:join)
|
49
|
+
rescue StandardError => e
|
50
|
+
logger.error "Error starting Rabbit Carrots: #{e.message}"
|
51
|
+
end
|
52
|
+
|
53
|
+
def request_shutdown
|
54
|
+
# Workaround to a known issue with Signal Traps and logs
|
55
|
+
Thread.start do
|
56
|
+
logger.log 'Shutting down Rabbit Carrots service...'
|
57
|
+
end
|
58
|
+
@shutdown_requested = true
|
59
|
+
@threads.each(&:kill)
|
60
|
+
stop
|
61
|
+
end
|
62
|
+
|
63
|
+
def stop
|
64
|
+
# Workaround to a known issue with Signal Traps and logs
|
65
|
+
Thread.start do
|
66
|
+
logger.log 'Stoppig the Rabbit Carrots service...'
|
67
|
+
end
|
68
|
+
@running = false
|
69
|
+
end
|
70
|
+
|
71
|
+
def run_task(queue_name:, handler_class:, routing_keys:, queue_arguments: {}, kill_to_restart_on_standard_error: false)
|
72
|
+
RabbitCarrots::Connection.instance.channel.with do |channel|
|
73
|
+
exchange = channel.topic(RabbitCarrots.configuration.rabbitmq_exchange_name, durable: true)
|
74
|
+
|
75
|
+
logger.log "Listening on QUEUE: #{queue_name} for ROUTING KEYS: #{routing_keys}"
|
76
|
+
queue = channel.queue(queue_name, durable: true, arguments: queue_arguments)
|
77
|
+
|
78
|
+
routing_keys.map(&:strip).each { |k| queue.bind(exchange, routing_key: k) }
|
79
|
+
|
80
|
+
queue.subscribe(block: false, manual_ack: true, prefetch: 10) do |delivery_info, properties, payload|
|
81
|
+
break if @shutdown_requested
|
82
|
+
|
83
|
+
logger.log "Received from queue: #{queue_name}, Routing Keys: #{routing_keys}"
|
84
|
+
handler_class.handle!(channel, delivery_info, properties, payload)
|
85
|
+
channel.ack(delivery_info.delivery_tag, false)
|
86
|
+
rescue RabbitCarrots::EventHandlers::Errors::NackMessage, JSON::ParserError => _e
|
87
|
+
logger.log "Nacked message: #{payload}"
|
88
|
+
channel.nack(delivery_info.delivery_tag, false, false)
|
89
|
+
rescue RabbitCarrots::EventHandlers::Errors::NackAndRequeueMessage => _e
|
90
|
+
logger.log "Nacked and Requeued message: #{payload}"
|
91
|
+
channel.nack(delivery_info.delivery_tag, false, true)
|
92
|
+
rescue DatabaseAgonsticNotNullViolation, DatabaseAgnosticRecordInvalid => e
|
93
|
+
logger.log "Null constraint or Invalid violation: #{payload}. Error: #{e.message}"
|
94
|
+
channel.ack(delivery_info.delivery_tag, false)
|
95
|
+
rescue DatabaseAgonsticConnectionNotEstablished => e
|
96
|
+
logger.log "Error connection not established to the database: #{payload}. Error: #{e.message}"
|
97
|
+
sleep 3
|
98
|
+
channel.nack(delivery_info.delivery_tag, false, true)
|
99
|
+
rescue StandardError => e
|
100
|
+
logger.log "Error handling message: #{payload}. Error: #{e.message}"
|
101
|
+
sleep 3
|
102
|
+
channel.nack(delivery_info.delivery_tag, false, true)
|
103
|
+
Process.kill('SIGTERM', Process.pid) if kill_to_restart_on_standard_error
|
104
|
+
end
|
105
|
+
|
106
|
+
logger.log "Ending task for queue: #{queue_name}"
|
107
|
+
end
|
108
|
+
rescue StandardError => e
|
109
|
+
logger.error "Bunny session error: #{e.message}"
|
110
|
+
request_shutdown
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -1,75 +1,13 @@
|
|
1
|
-
require 'bunny'
|
2
|
-
|
3
1
|
namespace :rabbit_carrots do
|
4
|
-
desc '
|
2
|
+
desc 'Rake task for standalone RabbitCarrots mode'
|
5
3
|
task eat: :environment do
|
6
4
|
Rails.application.eager_load!
|
7
5
|
|
8
|
-
|
9
|
-
|
10
|
-
DatabaseAgnosticRecordInvalid = defined?(ActiveRecord) ? ActiveRecord::RecordInvalid : Mongoid::Errors::Validations
|
11
|
-
|
12
|
-
channels = RabbitCarrots.configuration.routing_key_mappings.map do |mapping|
|
13
|
-
# This will be supplied in initializer. At that time, the Handler will not be available to be loaded and will throw Uninitialized Constant
|
14
|
-
{ **mapping, handler: mapping[:handler].constantize }
|
15
|
-
end
|
16
|
-
|
17
|
-
Rails.logger = Logger.new(Rails.env.production? ? '/proc/self/fd/1' : $stdout)
|
18
|
-
|
19
|
-
# Run RMQ Subscriber for each channel
|
20
|
-
channels.each do |channel|
|
21
|
-
handler_class = channel[:handler]
|
22
|
-
|
23
|
-
raise "#{handler_class.name} must respond to `handle!`" unless handler_class.respond_to?(:handle!)
|
24
|
-
|
25
|
-
run_task(queue_name: channel[:queue], handler_class:, routing_keys: channel[:routing_keys], queue_arguments: channel[:arguments])
|
26
|
-
end
|
27
|
-
|
28
|
-
# Infinite loop to keep the process running
|
29
|
-
loop do
|
30
|
-
sleep 1
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def run_task(queue_name:, queue_arguments: {}, handler_class:, routing_keys:)
|
36
|
-
RabbitCarrots::Connection.instance.channel.with do |channel|
|
37
|
-
exchange = channel.topic(RabbitCarrots.configuration.event_bus_exchange_name, durable: true)
|
38
|
-
|
39
|
-
Rails.logger.info "Listening on QUEUE: #{queue_name} for ROUTING KEYS: #{routing_keys}"
|
40
|
-
queue = channel.queue(queue_name, durable: true, arguments: queue_arguments)
|
41
|
-
|
42
|
-
routing_keys.map(&:strip).each { |k| queue.bind(exchange, routing_key: k) }
|
6
|
+
logger = Logger.new(Rails.env.production? ? '/proc/self/fd/1' : $stdout)
|
7
|
+
logger.level = Logger::INFO
|
43
8
|
|
44
|
-
|
45
|
-
Rails.logger.info "Received from queue: #{queue_name}, Routing Keys: #{routing_keys}"
|
46
|
-
handler_class.handle!(channel, delivery_info, properties, payload)
|
47
|
-
channel.ack(delivery_info.delivery_tag, false)
|
48
|
-
rescue RabbitCarrots::EventHandlers::Errors::NackMessage, JSON::ParserError => _e
|
49
|
-
Rails.logger.info "Nacked message: #{payload}"
|
50
|
-
channel.nack(delivery_info.delivery_tag, false, false)
|
51
|
-
rescue RabbitCarrots::EventHandlers::Errors::NackAndRequeueMessage => _e
|
52
|
-
Rails.logger.info "Nacked and Requeued message: #{payload}"
|
53
|
-
channel.nack(delivery_info.delivery_tag, false, true)
|
54
|
-
rescue DatabaseAgonsticNotNullViolation, DatabaseAgnosticRecordInvalid => e
|
55
|
-
# on null constraint violation, we want to ack the message
|
56
|
-
Rails.logger.error "Null constraint or Invalid violation: #{payload}. Error: #{e.message}"
|
57
|
-
channel.ack(delivery_info.delivery_tag, false)
|
58
|
-
rescue DatabaseAgonsticConnectionNotEstablished => e
|
59
|
-
# on connection not established, we want to requeue the message and sleep for 3 seconds
|
60
|
-
Rails.logger.error "Error connection not established to the database: #{payload}. Error: #{e.message}"
|
61
|
-
# delay for 3 seconds before requeuing
|
62
|
-
sleep 3
|
63
|
-
channel.nack(delivery_info.delivery_tag, false, true)
|
64
|
-
rescue StandardError => e
|
65
|
-
Rails.logger.error "Error handling message: #{payload}. Error: #{e.message}"
|
66
|
-
# requeue the message then kill the container
|
67
|
-
sleep 3
|
68
|
-
channel.nack(delivery_info.delivery_tag, false, true)
|
69
|
-
# kill the container with sigterm
|
70
|
-
Process.kill('SIGTERM', Process.pid)
|
71
|
-
end
|
9
|
+
core_service = RabbitCarrots::Core.new(logger:)
|
72
10
|
|
73
|
-
|
11
|
+
core_service.start(kill_to_restart_on_standard_error: true)
|
74
12
|
end
|
75
13
|
end
|
data/lib/rabbit_carrots.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rabbit_carrots
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brusk Awat
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-08-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bunny
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 2.
|
19
|
+
version: '2.22'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 2.
|
26
|
+
version: '2.22'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: connection_pool
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 2.
|
33
|
+
version: '2.4'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 2.
|
40
|
+
version: '2.4'
|
41
41
|
description: A background task based on rake to consume RabbitMQ messages
|
42
42
|
email:
|
43
43
|
- broosk.edogawa@gmail.com
|
@@ -54,13 +54,14 @@ files:
|
|
54
54
|
- LICENSE.txt
|
55
55
|
- README.md
|
56
56
|
- Rakefile
|
57
|
+
- lib/puma/plugin/rabbit_carrots.rb
|
57
58
|
- lib/rabbit_carrots.rb
|
58
59
|
- lib/rabbit_carrots/configuration.rb
|
59
60
|
- lib/rabbit_carrots/connection.rb
|
61
|
+
- lib/rabbit_carrots/core.rb
|
60
62
|
- lib/rabbit_carrots/railtie.rb
|
61
63
|
- lib/rabbit_carrots/tasks/rmq.rake
|
62
64
|
- lib/rabbit_carrots/version.rb
|
63
|
-
- rabbit_carrots.gemspec
|
64
65
|
- sig/rabbit_carrots.rbs
|
65
66
|
homepage: https://github.com/ditkrg
|
66
67
|
licenses:
|
@@ -86,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
86
87
|
- !ruby/object:Gem::Version
|
87
88
|
version: '0'
|
88
89
|
requirements: []
|
89
|
-
rubygems_version: 3.
|
90
|
+
rubygems_version: 3.5.9
|
90
91
|
signing_key:
|
91
92
|
specification_version: 4
|
92
93
|
summary: A simple RabbitMQ consumer task
|
data/rabbit_carrots.gemspec
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'lib/rabbit_carrots/version'
|
4
|
-
|
5
|
-
Gem::Specification.new do |spec|
|
6
|
-
spec.name = 'rabbit_carrots'
|
7
|
-
spec.version = RabbitCarrots::VERSION
|
8
|
-
spec.authors = ['Brusk Awat']
|
9
|
-
spec.email = ['broosk.edogawa@gmail.com']
|
10
|
-
|
11
|
-
spec.summary = 'A simple RabbitMQ consumer task'
|
12
|
-
spec.description = 'A background task based on rake to consume RabbitMQ messages'
|
13
|
-
spec.homepage = 'https://github.com/ditkrg'
|
14
|
-
spec.license = 'MIT'
|
15
|
-
spec.required_ruby_version = '>= 3.1.0'
|
16
|
-
|
17
|
-
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
18
|
-
|
19
|
-
spec.metadata['homepage_uri'] = spec.homepage
|
20
|
-
spec.metadata['source_code_uri'] = 'https://github.com/ditkrg/rabbit_carrots'
|
21
|
-
spec.metadata['changelog_uri'] = 'https://github.com/ditkrg/rabbit_carrots'
|
22
|
-
|
23
|
-
# Specify which files should be added to the gem when it is released.
|
24
|
-
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
-
spec.files = Dir.chdir(__dir__) do
|
26
|
-
`git ls-files -z`.split("\x0").reject do |f|
|
27
|
-
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
28
|
-
end
|
29
|
-
end
|
30
|
-
spec.bindir = 'exe'
|
31
|
-
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
32
|
-
spec.require_paths = ['lib']
|
33
|
-
|
34
|
-
# Uncomment to register a new dependency of your gem
|
35
|
-
spec.add_dependency 'bunny', '>= 2.19.0'
|
36
|
-
spec.add_dependency 'connection_pool', '~> 2.3.0'
|
37
|
-
|
38
|
-
# For more information and examples about making a new gem, check out our
|
39
|
-
# guide at: https://bundler.io/guides/creating_gem.html
|
40
|
-
spec.metadata['rubygems_mfa_required'] = 'true'
|
41
|
-
end
|