rabbit_carrots 1.1.1 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +52 -0
- data/Gemfile +3 -3
- data/Gemfile.lock +44 -39
- data/README.md +51 -1
- data/lib/puma/plugin/rabbit_carrots.rb +6 -1
- data/lib/rabbit_carrots/configuration.rb +15 -1
- data/lib/rabbit_carrots/connection.rb +12 -0
- data/lib/rabbit_carrots/core.rb +202 -32
- data/lib/rabbit_carrots/tasks/rmq.rake +4 -1
- data/lib/rabbit_carrots/version.rb +1 -1
- data/lib/rabbit_carrots.rb +3 -0
- metadata +4 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6d158e9a122e5fe4e003a3b4c132aee2560940fe219ab12b029815130f7f55c6
|
|
4
|
+
data.tar.gz: ac719d756751b3583716136e7014d89348df7a7ca94ccf4e03e68d65aab6e2cb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f83a2c8a8d45e95a081804ebd406adb3655f9372608bc51548f7ee7684c3996b9e7ec6ad2936a71827e74f9a4db69af78cb5bb570d86dc7c06e125ba04dd6fbc
|
|
7
|
+
data.tar.gz: 81197e0c9027daa4f07b5180eae94f0bdded351d208d9bc20759dcf4ce3e026b597432aea8749df4dc0aa2fcf3d2baa17cfe4e165d14a88c78d3322ca087ffa1
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,57 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
### Added
|
|
4
|
+
|
|
5
|
+
- `passive_exchange: true` on a routing key mapping binds to an existing
|
|
6
|
+
exchange without declaring it, so the user needs no `configure` permission on
|
|
7
|
+
an exchange owned by another service.
|
|
8
|
+
|
|
9
|
+
### Changed
|
|
10
|
+
|
|
11
|
+
- Requires `bunny >= 3.3`, the first version that leaves passive declarations
|
|
12
|
+
out of topology recovery.
|
|
13
|
+
|
|
14
|
+
## [1.2.0] - 2026-09-22
|
|
15
|
+
|
|
16
|
+
### Fixed
|
|
17
|
+
|
|
18
|
+
- **Consumers stopped consuming, permanently, after any connection recovery.**
|
|
19
|
+
The exchange was declared passively, which records `durable: false` for
|
|
20
|
+
topology recovery. On the first reconnect Bunny replayed that against a
|
|
21
|
+
durable exchange, the broker answered `PRECONDITION_FAILED - inequivalent arg
|
|
22
|
+
'durable'`, and the channel closed, taking every consumer with it. The process
|
|
23
|
+
stayed alive and exited 0, so nothing restarted it.
|
|
24
|
+
- Acking or nacking a closed channel no longer raises into Bunny's work pool.
|
|
25
|
+
The rescue for a failed ack used to nack on the same closed channel, and that
|
|
26
|
+
second raise killed the consumer.
|
|
27
|
+
- `start` no longer raises `NameError` when the configured ORM is not loaded.
|
|
28
|
+
- `bunny` and `connection_pool` are required by the gem.
|
|
29
|
+
- Each consumer gets its own channel. Channels came from a pool that was
|
|
30
|
+
handed back as soon as `subscribe` returned, so once there were more
|
|
31
|
+
mappings than the pool size, consumers silently shared channels — and
|
|
32
|
+
therefore shared a prefetch, a work pool, and the fate of that channel.
|
|
33
|
+
|
|
34
|
+
### Added
|
|
35
|
+
|
|
36
|
+
- A supervisor that checks the connection, consumers and channels every
|
|
37
|
+
`supervision_interval`. After `unhealthy_grace` without consuming it shuts
|
|
38
|
+
down and exits non-zero so the process is restarted. Transient reconnects are
|
|
39
|
+
tolerated.
|
|
40
|
+
- `Core#start` returns false when the service died; `rabbit_carrots:eat` and the
|
|
41
|
+
Puma plugin exit non-zero on it. `Core#healthy?` added.
|
|
42
|
+
- Config: `rabbitmq_exchange_durable` (default `true`), `prefetch` (default 10,
|
|
43
|
+
was hardcoded), `supervision_interval` (5s), `startup_grace` (60s),
|
|
44
|
+
`unhealthy_grace` (60s).
|
|
45
|
+
- Specs that run against a real broker (`docker compose up -d`), including a
|
|
46
|
+
regression test that severs the connection and asserts messages are still
|
|
47
|
+
consumed.
|
|
48
|
+
|
|
49
|
+
### Changed
|
|
50
|
+
|
|
51
|
+
- `bunny` floor raised from `>= 2.22` to `>= 3.1`; 2.x and 3.x record topology
|
|
52
|
+
differently and only 3.x is tested.
|
|
53
|
+
- The rake task and Puma plugin now exit non-zero when the consumer dies.
|
|
54
|
+
|
|
3
55
|
## [0.1.0] - 2022-12-01
|
|
4
56
|
|
|
5
57
|
- Initial release
|
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.4.2'
|
|
9
9
|
|
|
10
10
|
gem 'rspec', '~> 3.13.2'
|
|
11
11
|
|
|
12
|
-
gem 'rubocop', '~> 1.
|
|
12
|
+
gem 'rubocop', '~> 1.91.0'
|
|
13
13
|
|
|
14
|
-
gem 'rubocop-rails', '~> 2.
|
|
14
|
+
gem 'rubocop-rails', '~> 2.38.0'
|
data/Gemfile.lock
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
rabbit_carrots (1.
|
|
5
|
-
bunny (>=
|
|
4
|
+
rabbit_carrots (1.2.1)
|
|
5
|
+
bunny (>= 3.3)
|
|
6
6
|
connection_pool (>= 2.4)
|
|
7
7
|
|
|
8
8
|
GEM
|
|
9
9
|
remote: https://rubygems.org/
|
|
10
10
|
specs:
|
|
11
|
-
activesupport (8.1.
|
|
11
|
+
activesupport (8.1.4.1)
|
|
12
12
|
base64
|
|
13
13
|
bigdecimal
|
|
14
14
|
concurrent-ruby (~> 1.0, >= 1.3.1)
|
|
@@ -21,36 +21,40 @@ GEM
|
|
|
21
21
|
securerandom (>= 0.3)
|
|
22
22
|
tzinfo (~> 2.0, >= 2.0.5)
|
|
23
23
|
uri (>= 0.13.1)
|
|
24
|
-
amq-protocol (2.
|
|
24
|
+
amq-protocol (2.9.0)
|
|
25
25
|
ast (2.4.3)
|
|
26
26
|
base64 (0.3.0)
|
|
27
|
-
bigdecimal (4.
|
|
28
|
-
bunny (
|
|
29
|
-
amq-protocol (~> 2.
|
|
27
|
+
bigdecimal (4.1.3)
|
|
28
|
+
bunny (3.4.0)
|
|
29
|
+
amq-protocol (~> 2.9)
|
|
30
|
+
logger (~> 1, >= 1.7)
|
|
31
|
+
openssl (>= 3.3)
|
|
30
32
|
sorted_set (~> 1, >= 1.0.2)
|
|
31
|
-
concurrent-ruby (1.3.
|
|
33
|
+
concurrent-ruby (1.3.8)
|
|
32
34
|
connection_pool (3.0.2)
|
|
33
35
|
diff-lcs (1.6.2)
|
|
34
36
|
drb (2.2.3)
|
|
35
|
-
i18n (1.
|
|
37
|
+
i18n (1.15.2)
|
|
36
38
|
concurrent-ruby (~> 1.0)
|
|
37
|
-
json (
|
|
38
|
-
language_server-protocol (3.17.0.
|
|
39
|
+
json (3.0.2)
|
|
40
|
+
language_server-protocol (3.17.0.6)
|
|
39
41
|
lint_roller (1.1.0)
|
|
40
42
|
logger (1.7.0)
|
|
41
|
-
minitest (6.0.
|
|
43
|
+
minitest (6.0.6)
|
|
44
|
+
drb (~> 2.0)
|
|
42
45
|
prism (~> 1.5)
|
|
43
|
-
|
|
44
|
-
|
|
46
|
+
openssl (4.0.2)
|
|
47
|
+
parallel (2.2.0)
|
|
48
|
+
parser (3.3.12.0)
|
|
45
49
|
ast (~> 2.4.1)
|
|
46
50
|
racc
|
|
47
|
-
prism (1.
|
|
51
|
+
prism (1.9.0)
|
|
48
52
|
racc (1.8.1)
|
|
49
|
-
rack (3.2.
|
|
53
|
+
rack (3.2.8)
|
|
50
54
|
rainbow (3.1.1)
|
|
51
|
-
rake (13.
|
|
52
|
-
rbtree (0.4.
|
|
53
|
-
regexp_parser (2.
|
|
55
|
+
rake (13.4.2)
|
|
56
|
+
rbtree (0.4.7)
|
|
57
|
+
regexp_parser (2.13.0)
|
|
54
58
|
rspec (3.13.2)
|
|
55
59
|
rspec-core (~> 3.13.0)
|
|
56
60
|
rspec-expectations (~> 3.13.0)
|
|
@@ -60,53 +64,54 @@ GEM
|
|
|
60
64
|
rspec-expectations (3.13.5)
|
|
61
65
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
62
66
|
rspec-support (~> 3.13.0)
|
|
63
|
-
rspec-mocks (3.13.
|
|
67
|
+
rspec-mocks (3.13.8)
|
|
64
68
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
65
69
|
rspec-support (~> 3.13.0)
|
|
66
|
-
rspec-support (3.13.
|
|
67
|
-
rubocop (1.
|
|
68
|
-
json (
|
|
70
|
+
rspec-support (3.13.7)
|
|
71
|
+
rubocop (1.91.0)
|
|
72
|
+
json (>= 2.3)
|
|
69
73
|
language_server-protocol (~> 3.17.0.2)
|
|
70
74
|
lint_roller (~> 1.1.0)
|
|
71
|
-
parallel (
|
|
75
|
+
parallel (>= 1.10)
|
|
72
76
|
parser (>= 3.3.0.2)
|
|
73
77
|
rainbow (>= 2.2.2, < 4.0)
|
|
74
78
|
regexp_parser (>= 2.9.3, < 3.0)
|
|
75
|
-
rubocop-ast (>= 1.
|
|
79
|
+
rubocop-ast (>= 1.49.0, < 2.0)
|
|
76
80
|
ruby-progressbar (~> 1.7)
|
|
77
81
|
unicode-display_width (>= 2.4.0, < 4.0)
|
|
78
|
-
rubocop-ast (1.
|
|
82
|
+
rubocop-ast (1.50.0)
|
|
79
83
|
parser (>= 3.3.7.2)
|
|
80
84
|
prism (~> 1.7)
|
|
81
|
-
rubocop-rails (2.
|
|
85
|
+
rubocop-rails (2.38.0)
|
|
82
86
|
activesupport (>= 4.2.0)
|
|
83
87
|
lint_roller (~> 1.1)
|
|
84
88
|
rack (>= 1.1)
|
|
85
|
-
rubocop (>= 1.
|
|
89
|
+
rubocop (>= 1.89.0, < 2.0)
|
|
86
90
|
rubocop-ast (>= 1.44.0, < 2.0)
|
|
87
91
|
ruby-progressbar (1.13.0)
|
|
88
92
|
securerandom (0.4.1)
|
|
89
|
-
|
|
90
|
-
sorted_set (1.0.3)
|
|
93
|
+
sorted_set (1.1.0)
|
|
91
94
|
rbtree
|
|
92
|
-
set (~> 1.0)
|
|
93
95
|
tzinfo (2.0.6)
|
|
94
96
|
concurrent-ruby (~> 1.0)
|
|
95
|
-
unicode-display_width (3.
|
|
96
|
-
unicode-emoji (~> 4.
|
|
97
|
-
unicode-emoji (4.
|
|
97
|
+
unicode-display_width (3.3.0)
|
|
98
|
+
unicode-emoji (~> 4.3)
|
|
99
|
+
unicode-emoji (4.3.0)
|
|
98
100
|
uri (1.1.1)
|
|
99
101
|
|
|
100
102
|
PLATFORMS
|
|
103
|
+
aarch64-linux-musl
|
|
101
104
|
arm64-darwin-25
|
|
102
|
-
|
|
105
|
+
x86-linux-musl
|
|
106
|
+
x86_64-linux-gnu
|
|
107
|
+
x86_64-linux-musl
|
|
103
108
|
|
|
104
109
|
DEPENDENCIES
|
|
105
110
|
rabbit_carrots!
|
|
106
|
-
rake (~> 13.
|
|
111
|
+
rake (~> 13.4.2)
|
|
107
112
|
rspec (~> 3.13.2)
|
|
108
|
-
rubocop (~> 1.
|
|
109
|
-
rubocop-rails (~> 2.
|
|
113
|
+
rubocop (~> 1.91.0)
|
|
114
|
+
rubocop-rails (~> 2.38.0)
|
|
110
115
|
|
|
111
116
|
BUNDLED WITH
|
|
112
|
-
|
|
117
|
+
4.0.19
|
data/README.md
CHANGED
|
@@ -33,13 +33,20 @@ RabbitCarrots.configure do |c|
|
|
|
33
33
|
c.rabbitmq_password = ENV.fetch('RABBITMQ__PASSWORD', nil)
|
|
34
34
|
c.rabbitmq_vhost = ENV.fetch('RABBITMQ__VHOST', nil)
|
|
35
35
|
c.rabbitmq_exchange_name = ENV.fetch('RABBITMQ__EXCHANGE_NAME', nil)
|
|
36
|
+
c.rabbitmq_exchange_durable = true # must match the exchange on the broker
|
|
36
37
|
c.automatically_recover = true
|
|
37
38
|
c.network_recovery_interval = 5
|
|
38
39
|
c.recovery_attempts = 5
|
|
40
|
+
c.prefetch = 10
|
|
41
|
+
c.supervision_interval = 5
|
|
42
|
+
c.startup_grace = 60
|
|
43
|
+
c.unhealthy_grace = 60
|
|
39
44
|
c.orm = :activerecord || :mongoid
|
|
40
45
|
c.routing_key_mappings = [
|
|
41
46
|
{ routing_keys: ['RK1', 'RK2'], queue: 'QUEUE_NAME', handler: 'CLASS HANDLER IN STRING' },
|
|
42
|
-
{ routing_keys: ['RK1', 'RK2'], queue: 'QUEUE_NAME', handler: 'CLASS HANDLER IN STRING' }
|
|
47
|
+
{ routing_keys: ['RK1', 'RK2'], queue: 'QUEUE_NAME', handler: 'CLASS HANDLER IN STRING' },
|
|
48
|
+
{ routing_keys: ['RK1'], queue: 'QUEUE_NAME', handler: 'CLASS HANDLER IN STRING',
|
|
49
|
+
exchange_name: 'ANOTHER_SERVICES_EXCHANGE', passive_exchange: true }
|
|
43
50
|
]
|
|
44
51
|
end
|
|
45
52
|
|
|
@@ -63,6 +70,49 @@ If no errors are thrown, the message will be acknowledged soon after the ```hand
|
|
|
63
70
|
|
|
64
71
|
Note: Any other unrescued exception raised inside ```handle!``` the that is a subclass of ```StandardError``` will trigger a NACK and re-queue.
|
|
65
72
|
|
|
73
|
+
### Exchange durability
|
|
74
|
+
|
|
75
|
+
`rabbitmq_exchange_durable` has to match the exchange as it exists on the
|
|
76
|
+
broker. It defaults to `true`, which is what a shared event bus exchange
|
|
77
|
+
normally is.
|
|
78
|
+
|
|
79
|
+
This is not cosmetic. Bunny records how an entity was declared and replays that
|
|
80
|
+
declaration during automatic topology recovery, and topology recovery is not
|
|
81
|
+
configurable. Declare a durable exchange as non-durable and every reconnect —
|
|
82
|
+
a node put into maintenance, a failover, a brief network blip — is answered with
|
|
83
|
+
`PRECONDITION_FAILED - inequivalent arg 'durable'`, which closes the channel and
|
|
84
|
+
takes every consumer on it with it. The process stays up and stops consuming.
|
|
85
|
+
|
|
86
|
+
If the durability is wrong, the service now says so and refuses to start rather
|
|
87
|
+
than consuming from a channel that will not survive its first reconnect.
|
|
88
|
+
|
|
89
|
+
### Consuming from an exchange you don't own
|
|
90
|
+
|
|
91
|
+
By default each exchange is declared, which needs `configure` permission on it.
|
|
92
|
+
To bind to an exchange another service owns without that permission, set
|
|
93
|
+
`passive_exchange: true` on the mapping. The exchange is then only checked for
|
|
94
|
+
existence; the user needs `read` on it to bind, and nothing else.
|
|
95
|
+
|
|
96
|
+
If the exchange does not exist, the consumer fails to start and says so. Passive
|
|
97
|
+
declarations are left out of Bunny's topology recovery (Bunny >= 3.3), so a
|
|
98
|
+
reconnect does not try to declare the exchange either.
|
|
99
|
+
|
|
100
|
+
### Health and restarts
|
|
101
|
+
|
|
102
|
+
A consumer subscribes and then does its work on Bunny's own threads, so the
|
|
103
|
+
process staying alive says nothing about whether it is still consuming. The
|
|
104
|
+
supervisor checks every `supervision_interval` seconds that the connection is
|
|
105
|
+
open, every consumer is still registered, and every channel is still alive.
|
|
106
|
+
|
|
107
|
+
A reconnect in progress is indistinguishable from a dead consumer, so the
|
|
108
|
+
service is only declared dead after `unhealthy_grace` seconds without
|
|
109
|
+
consuming. It then shuts down and **exits non-zero**, so that Kubernetes, Puma
|
|
110
|
+
or systemd restarts it. Make sure whatever supervises the process actually
|
|
111
|
+
restarts it on failure.
|
|
112
|
+
|
|
113
|
+
`Core#start` returns `true` when it shut down because it was asked to, and
|
|
114
|
+
`false` when it died.
|
|
115
|
+
|
|
66
116
|
### Running
|
|
67
117
|
|
|
68
118
|
For better scalability and improved performance, you can run rabbit_carrots in standalone mode by invoking the following command:
|
|
@@ -30,9 +30,14 @@ Puma::Plugin.create do
|
|
|
30
30
|
private
|
|
31
31
|
|
|
32
32
|
def start_rabbit_carrots_consumer
|
|
33
|
-
core_service.start(kill_to_restart_on_standard_error: true)
|
|
33
|
+
healthy = core_service.start(kill_to_restart_on_standard_error: true)
|
|
34
|
+
|
|
35
|
+
# Exits the forked child, not the Rails process, so that
|
|
36
|
+
# monitor_rabbit_carrots notices and brings Puma down with it.
|
|
37
|
+
exit(1) unless healthy # rubocop:disable Rails/Exit
|
|
34
38
|
rescue StandardError => e
|
|
35
39
|
Rails.logger.error "Error starting Rabbit Carrots: #{e.message}"
|
|
40
|
+
exit(1) # rubocop:disable Rails/Exit
|
|
36
41
|
end
|
|
37
42
|
|
|
38
43
|
def stop_rabbit_carrots
|
|
@@ -16,9 +16,23 @@ module RabbitCarrots
|
|
|
16
16
|
:rabbitmq_vhost,
|
|
17
17
|
:routing_key_mappings,
|
|
18
18
|
:rabbitmq_exchange_name,
|
|
19
|
+
# Must match the exchange on the broker, or topology recovery
|
|
20
|
+
# is rejected and every consumer is lost. Defaults to true.
|
|
21
|
+
:rabbitmq_exchange_durable,
|
|
19
22
|
:automatically_recover,
|
|
20
23
|
:network_recovery_interval,
|
|
21
|
-
:recovery_attempts
|
|
24
|
+
:recovery_attempts,
|
|
25
|
+
# Unacked messages allowed per consumer. Defaults to 10.
|
|
26
|
+
:prefetch,
|
|
27
|
+
# Seconds between supervisor health checks. Defaults to 5.
|
|
28
|
+
:supervision_interval,
|
|
29
|
+
# Seconds to wait for every consumer to subscribe at boot.
|
|
30
|
+
# Defaults to 60.
|
|
31
|
+
:startup_grace,
|
|
32
|
+
# Seconds the service may go without consuming before the
|
|
33
|
+
# supervisor gives up. Must outlast Bunny's recovery.
|
|
34
|
+
# Defaults to 60.
|
|
35
|
+
:unhealthy_grace
|
|
22
36
|
|
|
23
37
|
def orm
|
|
24
38
|
@orm ||= :activerecord
|
|
@@ -27,5 +27,17 @@ module RabbitCarrots
|
|
|
27
27
|
connection.create_channel
|
|
28
28
|
end
|
|
29
29
|
end
|
|
30
|
+
|
|
31
|
+
# Consumers get a channel of their own rather than one from the pool. A
|
|
32
|
+
# pooled channel is returned as soon as `subscribe` returns, so several
|
|
33
|
+
# consumers end up sharing one channel, which means they share a prefetch,
|
|
34
|
+
# a work pool, and the fate of that channel when it errors.
|
|
35
|
+
def create_channel(...)
|
|
36
|
+
connection.create_channel(...)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def open?
|
|
40
|
+
!connection.nil? && connection.open?
|
|
41
|
+
end
|
|
30
42
|
end
|
|
31
43
|
end
|
data/lib/rabbit_carrots/core.rb
CHANGED
|
@@ -13,19 +13,29 @@ module RabbitCarrots
|
|
|
13
13
|
def initialize(logger: nil)
|
|
14
14
|
@logger = create_logger_adapter(logger || Logger.new(Rails.env.production? ? '/proc/self/fd/1' : $stdout))
|
|
15
15
|
@threads = []
|
|
16
|
+
@consumers = []
|
|
17
|
+
@consumers_mutex = Mutex.new
|
|
16
18
|
@running = true
|
|
17
19
|
@shutdown_requested = false
|
|
20
|
+
@healthy = true
|
|
18
21
|
end
|
|
19
22
|
|
|
23
|
+
# Returns true when the service shut down because it was asked to, false
|
|
24
|
+
# when it shut down because it could no longer consume.
|
|
20
25
|
def start(kill_to_restart_on_standard_error: false)
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
self.class.
|
|
26
|
+
activerecord = RabbitCarrots.configuration.orm == :activerecord
|
|
27
|
+
|
|
28
|
+
self.class.database_agnostic_not_null_violation = activerecord ? error_class('ActiveRecord::NotNullViolation') : RabbitCarrots::EventHandlers::Errors::PlaceholderError
|
|
29
|
+
self.class.database_agnostic_connection_not_established = error_class(activerecord ? 'ActiveRecord::ConnectionNotEstablished' : 'Mongo::Error::SocketError')
|
|
30
|
+
self.class.database_agnostic_record_invalid = error_class(activerecord ? 'ActiveRecord::RecordInvalid' : 'Mongoid::Errors::Validations')
|
|
24
31
|
|
|
25
32
|
channels = RabbitCarrots.configuration.routing_key_mappings.map do |mapping|
|
|
26
33
|
{ **mapping, handler: mapping[:handler].constantize }
|
|
27
34
|
end
|
|
28
35
|
|
|
36
|
+
@expected_consumers = channels.size
|
|
37
|
+
@startup_deadline = monotonic_now + startup_grace
|
|
38
|
+
|
|
29
39
|
channels.each do |channel|
|
|
30
40
|
handler_class = channel[:handler]
|
|
31
41
|
raise "#{handler_class.name} must respond to `handle!`" unless handler_class.respond_to?(:handle!)
|
|
@@ -37,6 +47,7 @@ module RabbitCarrots
|
|
|
37
47
|
routing_keys: channel[:routing_keys],
|
|
38
48
|
queue_arguments: channel[:arguments],
|
|
39
49
|
exchange_name: channel[:exchange_name],
|
|
50
|
+
passive_exchange: channel[:passive_exchange],
|
|
40
51
|
kill_to_restart_on_standard_error:
|
|
41
52
|
)
|
|
42
53
|
end
|
|
@@ -45,18 +56,14 @@ module RabbitCarrots
|
|
|
45
56
|
Signal.trap('INT') { request_shutdown }
|
|
46
57
|
Signal.trap('TERM') { request_shutdown }
|
|
47
58
|
|
|
48
|
-
|
|
49
|
-
if @shutdown_requested
|
|
50
|
-
request_shutdown
|
|
51
|
-
sleep 1
|
|
52
|
-
break
|
|
53
|
-
end
|
|
54
|
-
sleep 1
|
|
55
|
-
end
|
|
59
|
+
supervise
|
|
56
60
|
|
|
57
61
|
@threads.each(&:join)
|
|
62
|
+
|
|
63
|
+
@healthy
|
|
58
64
|
rescue StandardError => e
|
|
59
65
|
logger.error "Error starting Rabbit Carrots: #{e.message}"
|
|
66
|
+
false
|
|
60
67
|
end
|
|
61
68
|
|
|
62
69
|
def request_shutdown
|
|
@@ -77,61 +84,224 @@ module RabbitCarrots
|
|
|
77
84
|
@running = false
|
|
78
85
|
end
|
|
79
86
|
|
|
80
|
-
def
|
|
81
|
-
|
|
82
|
-
|
|
87
|
+
def healthy?
|
|
88
|
+
@healthy
|
|
89
|
+
end
|
|
83
90
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
# If the exchange does not exist, declare it
|
|
89
|
-
exchange = channel.topic(exchange_name, durable: true)
|
|
90
|
-
end
|
|
91
|
+
def run_task(queue_name:, handler_class:, routing_keys:, queue_arguments: {}, exchange_name: nil, passive_exchange: false, kill_to_restart_on_standard_error: false) # rubocop:disable Metrics/ParameterLists
|
|
92
|
+
channel = RabbitCarrots::Connection.instance.create_channel
|
|
93
|
+
exchange_name ||= RabbitCarrots.configuration.rabbitmq_exchange_name
|
|
94
|
+
exchange = declare_exchange(channel, exchange_name, passive: passive_exchange)
|
|
91
95
|
|
|
92
|
-
|
|
93
|
-
|
|
96
|
+
logger.info "Listening on QUEUE: #{queue_name} for ROUTING KEYS: #{routing_keys}"
|
|
97
|
+
queue = channel.queue(queue_name, durable: true, arguments: queue_arguments)
|
|
94
98
|
|
|
95
|
-
|
|
99
|
+
routing_keys.map(&:strip).each { |k| queue.bind(exchange, routing_key: k) }
|
|
96
100
|
|
|
97
|
-
|
|
98
|
-
|
|
101
|
+
consumer = queue.subscribe(block: false, manual_ack: true, prefetch:) do |delivery_info, properties, payload|
|
|
102
|
+
next if @shutdown_requested
|
|
99
103
|
|
|
104
|
+
begin
|
|
100
105
|
logger.info "Received from queue: #{queue_name}, Routing Keys: #{routing_keys}"
|
|
101
106
|
handler_class.handle!(channel, delivery_info, properties, payload)
|
|
102
|
-
channel
|
|
107
|
+
acknowledge(channel, delivery_info)
|
|
103
108
|
rescue RabbitCarrots::EventHandlers::Errors::NackMessage, JSON::ParserError => _e
|
|
104
109
|
payload = encode_payload(payload)
|
|
105
110
|
logger.warn "Nacked message: #{payload}"
|
|
106
|
-
channel
|
|
111
|
+
reject(channel, delivery_info, requeue: false)
|
|
107
112
|
rescue RabbitCarrots::EventHandlers::Errors::NackAndRequeueMessage => _e
|
|
108
113
|
payload = encode_payload(payload)
|
|
109
114
|
logger.warn "Nacked and Requeued message: #{payload}"
|
|
110
|
-
channel
|
|
115
|
+
reject(channel, delivery_info, requeue: true)
|
|
111
116
|
rescue self.class.database_agnostic_not_null_violation, self.class.database_agnostic_record_invalid => e
|
|
112
117
|
payload = encode_payload(payload)
|
|
113
118
|
logger.warn "Null constraint or Invalid violation: #{payload}. Error: #{e.message}"
|
|
114
|
-
channel
|
|
119
|
+
acknowledge(channel, delivery_info)
|
|
115
120
|
rescue self.class.database_agnostic_connection_not_established => e
|
|
116
121
|
payload = encode_payload(payload)
|
|
117
122
|
logger.warn "Error connection not established to the database: #{payload}. Error: #{e.message}"
|
|
118
123
|
sleep 3
|
|
119
|
-
channel
|
|
124
|
+
reject(channel, delivery_info, requeue: true)
|
|
120
125
|
rescue StandardError => e
|
|
121
126
|
payload = encode_payload(payload)
|
|
122
127
|
logger.error "Error handling message: #{payload}. Error: #{e.message}"
|
|
123
128
|
sleep 3
|
|
124
|
-
channel
|
|
129
|
+
reject(channel, delivery_info, requeue: true)
|
|
125
130
|
Process.kill('SIGTERM', Process.pid) if kill_to_restart_on_standard_error
|
|
126
131
|
end
|
|
127
132
|
end
|
|
133
|
+
|
|
134
|
+
register_consumer(queue_name:, channel:, consumer:)
|
|
128
135
|
rescue StandardError => e
|
|
129
136
|
logger.error "Bunny session error: #{e.message}"
|
|
137
|
+
@healthy = false
|
|
130
138
|
request_shutdown
|
|
131
139
|
end
|
|
132
140
|
|
|
133
141
|
private
|
|
134
142
|
|
|
143
|
+
# Falls back to a class that is never raised when the ORM is not loaded.
|
|
144
|
+
def error_class(name)
|
|
145
|
+
Object.const_get(name)
|
|
146
|
+
rescue NameError
|
|
147
|
+
RabbitCarrots::EventHandlers::Errors::PlaceholderError
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# A passive declaration only checks that an exchange owned by another
|
|
151
|
+
# service exists, so it needs no `configure` permission on it. Bunny >= 3.3
|
|
152
|
+
# leaves passive declarations out of topology recovery; older versions
|
|
153
|
+
# recorded them and replayed a durability mismatch on every reconnect.
|
|
154
|
+
def declare_exchange(channel, exchange_name, passive: false)
|
|
155
|
+
return channel.exchange(exchange_name, type: :topic, durable: exchange_durable?, passive: true) if passive
|
|
156
|
+
|
|
157
|
+
channel.topic(exchange_name, durable: exchange_durable?)
|
|
158
|
+
rescue Bunny::NotFound => e
|
|
159
|
+
logger.error(
|
|
160
|
+
"Exchange '#{exchange_name}' does not exist. With `passive_exchange: true` it is not declared " \
|
|
161
|
+
"here and must be created by the service that owns it. Error: #{e.message}"
|
|
162
|
+
)
|
|
163
|
+
raise
|
|
164
|
+
rescue Bunny::PreconditionFailed => e
|
|
165
|
+
logger.error(
|
|
166
|
+
"Exchange '#{exchange_name}' exists on the broker with a different durability than the one " \
|
|
167
|
+
"declared here (durable: #{exchange_durable?}). Set `config.rabbitmq_exchange_durable` to " \
|
|
168
|
+
"match the exchange. Error: #{e.message}"
|
|
169
|
+
)
|
|
170
|
+
raise
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def exchange_durable?
|
|
174
|
+
durable = RabbitCarrots.configuration.rabbitmq_exchange_durable
|
|
175
|
+
durable.nil? ? true : durable # rubocop:disable Style/RedundantCondition
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def prefetch
|
|
179
|
+
RabbitCarrots.configuration.prefetch || 10
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def supervision_interval
|
|
183
|
+
RabbitCarrots.configuration.supervision_interval || 5
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def startup_grace
|
|
187
|
+
RabbitCarrots.configuration.startup_grace || 60
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def unhealthy_grace
|
|
191
|
+
RabbitCarrots.configuration.unhealthy_grace || 60
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def register_consumer(queue_name:, channel:, consumer:)
|
|
195
|
+
@consumers_mutex.synchronize do
|
|
196
|
+
@consumers << { queue_name:, channel:, consumer: }
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# `subscribe` is non-blocking, so the consumer threads finish at boot and
|
|
201
|
+
# every delivery happens on Bunny's threads, which this class does not own.
|
|
202
|
+
# Without this loop, a channel closed by a failed recovery leaves the
|
|
203
|
+
# process alive and idle forever.
|
|
204
|
+
def supervise
|
|
205
|
+
while @running
|
|
206
|
+
if @shutdown_requested
|
|
207
|
+
request_shutdown
|
|
208
|
+
sleep 1
|
|
209
|
+
break
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
if consumers_healthy?
|
|
213
|
+
logger.info 'Rabbit Carrots recovered and is consuming again' if @degraded_since
|
|
214
|
+
@degraded_since = nil
|
|
215
|
+
else
|
|
216
|
+
@degraded_since ||= monotonic_now
|
|
217
|
+
degraded_for = monotonic_now - @degraded_since
|
|
218
|
+
|
|
219
|
+
if degraded_for >= unhealthy_grace
|
|
220
|
+
@healthy = false
|
|
221
|
+
logger.error "Rabbit Carrots has not been consuming for #{degraded_for.round}s; shutting down so the process can be restarted"
|
|
222
|
+
request_shutdown
|
|
223
|
+
break
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
# A reconnect in progress is indistinguishable from a dead consumer.
|
|
227
|
+
logger.warn "Rabbit Carrots is not consuming (#{degraded_for.round}s); waiting for recovery"
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
sleep supervision_interval
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
def consumers_healthy?
|
|
235
|
+
return false unless connection_open?
|
|
236
|
+
|
|
237
|
+
registered = @consumers_mutex.synchronize { @consumers.dup }
|
|
238
|
+
|
|
239
|
+
if registered.size < @expected_consumers.to_i
|
|
240
|
+
return true if monotonic_now < @startup_deadline
|
|
241
|
+
|
|
242
|
+
logger.warn "Only #{registered.size} of #{@expected_consumers} consumers registered within #{startup_grace}s"
|
|
243
|
+
return false
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
registered.all? { |entry| consumer_alive?(entry) }
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
def consumer_alive?(entry)
|
|
250
|
+
channel = entry[:channel]
|
|
251
|
+
consumer = entry[:consumer]
|
|
252
|
+
|
|
253
|
+
unless channel.open?
|
|
254
|
+
logger.warn "Channel for queue #{entry[:queue_name]} is closed"
|
|
255
|
+
return false
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# Bunny can regenerate the tag when it recovers a consumer.
|
|
259
|
+
return true if channel.consumers.key?(consumer.consumer_tag) || channel.consumers.value?(consumer)
|
|
260
|
+
|
|
261
|
+
logger.warn "Consumer for queue #{entry[:queue_name]} is no longer registered on its channel"
|
|
262
|
+
false
|
|
263
|
+
rescue StandardError => e
|
|
264
|
+
logger.warn "Could not determine consumer health for queue #{entry[:queue_name]}: #{e.message}"
|
|
265
|
+
false
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
def connection_open?
|
|
269
|
+
return true if RabbitCarrots::Connection.instance.open?
|
|
270
|
+
|
|
271
|
+
logger.warn 'RabbitMQ connection is closed'
|
|
272
|
+
false
|
|
273
|
+
rescue StandardError => e
|
|
274
|
+
logger.warn "Could not determine RabbitMQ connection health: #{e.message}"
|
|
275
|
+
false
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
# Acking a closed channel raises, and so does the nack in the rescue that
|
|
279
|
+
# follows; that second raise escapes into Bunny's work pool and takes the
|
|
280
|
+
# consumer with it. The broker redelivers whatever was left unacked.
|
|
281
|
+
def acknowledge(channel, delivery_info)
|
|
282
|
+
return false unless channel.open?
|
|
283
|
+
|
|
284
|
+
channel.ack(delivery_info.delivery_tag, false)
|
|
285
|
+
true
|
|
286
|
+
rescue StandardError => e
|
|
287
|
+
logger.error "Failed to ack message on a closed or closing channel: #{e.message}"
|
|
288
|
+
false
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
def reject(channel, delivery_info, requeue:)
|
|
292
|
+
return false unless channel.open?
|
|
293
|
+
|
|
294
|
+
channel.nack(delivery_info.delivery_tag, false, requeue)
|
|
295
|
+
true
|
|
296
|
+
rescue StandardError => e
|
|
297
|
+
logger.error "Failed to nack message on a closed or closing channel: #{e.message}"
|
|
298
|
+
false
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
def monotonic_now
|
|
302
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
303
|
+
end
|
|
304
|
+
|
|
135
305
|
def create_logger_adapter(logger)
|
|
136
306
|
return logger if logger.respond_to?(:info) && logger.respond_to?(:error) && logger.respond_to?(:warn)
|
|
137
307
|
|
|
@@ -8,6 +8,9 @@ namespace :rabbit_carrots do
|
|
|
8
8
|
|
|
9
9
|
core_service = RabbitCarrots::Core.new(logger:)
|
|
10
10
|
|
|
11
|
-
core_service.start(kill_to_restart_on_standard_error: true)
|
|
11
|
+
healthy = core_service.start(kill_to_restart_on_standard_error: true)
|
|
12
|
+
|
|
13
|
+
# Exit non-zero so that whatever supervises the process restarts it.
|
|
14
|
+
exit(1) unless healthy
|
|
12
15
|
end
|
|
13
16
|
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: 1.
|
|
4
|
+
version: 1.2.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Brusk Awat
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-11-09 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bunny
|
|
@@ -16,14 +16,14 @@ dependencies:
|
|
|
16
16
|
requirements:
|
|
17
17
|
- - ">="
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '
|
|
19
|
+
version: '3.3'
|
|
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: '
|
|
26
|
+
version: '3.3'
|
|
27
27
|
- !ruby/object:Gem::Dependency
|
|
28
28
|
name: connection_pool
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|