Skip to content

Commit

Permalink
Feature/config for flexible handler resolve (#26)
Browse files Browse the repository at this point in the history
* add configuration option

* get in new config option

* cover by specs feature

* fix for old ruby versions

* fix linter

* fix audit

* continue fix audit

* remove 2.7 from suppport list and add 3.3 ruby version

* fix codereviwer remarkds

* more specs

* fix linter admonitions

* move specs depencies to spec helper
  • Loading branch information
mitya87 authored Jun 5, 2024
1 parent b684ca9 commit 8d6280d
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 113 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby: ["2.7", "3.0", "3.1", "3.2"]
ruby: ["3.0", "3.1", "3.2", "3.3"]

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ inherit_gem:

AllCops:
DisplayCopNames: true
TargetRubyVersion: 2.7
TargetRubyVersion: 3.0
SuggestExtensions: false
Include:
- lib/**/*.rb
Expand Down
234 changes: 135 additions & 99 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
rabbit_messaging (0.14.0)
rabbit_messaging (0.15.0)
bunny (~> 2.0)
lamian
rails (>= 5.2)
Expand All @@ -11,73 +11,84 @@ PATH
GEM
remote: https://rubygems.org/
specs:
actioncable (7.0.4.2)
actionpack (= 7.0.4.2)
activesupport (= 7.0.4.2)
actioncable (7.1.3.3)
actionpack (= 7.1.3.3)
activesupport (= 7.1.3.3)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (7.0.4.2)
actionpack (= 7.0.4.2)
activejob (= 7.0.4.2)
activerecord (= 7.0.4.2)
activestorage (= 7.0.4.2)
activesupport (= 7.0.4.2)
zeitwerk (~> 2.6)
actionmailbox (7.1.3.3)
actionpack (= 7.1.3.3)
activejob (= 7.1.3.3)
activerecord (= 7.1.3.3)
activestorage (= 7.1.3.3)
activesupport (= 7.1.3.3)
mail (>= 2.7.1)
net-imap
net-pop
net-smtp
actionmailer (7.0.4.2)
actionpack (= 7.0.4.2)
actionview (= 7.0.4.2)
activejob (= 7.0.4.2)
activesupport (= 7.0.4.2)
actionmailer (7.1.3.3)
actionpack (= 7.1.3.3)
actionview (= 7.1.3.3)
activejob (= 7.1.3.3)
activesupport (= 7.1.3.3)
mail (~> 2.5, >= 2.5.4)
net-imap
net-pop
net-smtp
rails-dom-testing (~> 2.0)
actionpack (7.0.4.2)
actionview (= 7.0.4.2)
activesupport (= 7.0.4.2)
rack (~> 2.0, >= 2.2.0)
rails-dom-testing (~> 2.2)
actionpack (7.1.3.3)
actionview (= 7.1.3.3)
activesupport (= 7.1.3.3)
nokogiri (>= 1.8.5)
racc
rack (>= 2.2.4)
rack-session (>= 1.0.1)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (7.0.4.2)
actionpack (= 7.0.4.2)
activerecord (= 7.0.4.2)
activestorage (= 7.0.4.2)
activesupport (= 7.0.4.2)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
actiontext (7.1.3.3)
actionpack (= 7.1.3.3)
activerecord (= 7.1.3.3)
activestorage (= 7.1.3.3)
activesupport (= 7.1.3.3)
globalid (>= 0.6.0)
nokogiri (>= 1.8.5)
actionview (7.0.4.2)
activesupport (= 7.0.4.2)
actionview (7.1.3.3)
activesupport (= 7.1.3.3)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activejob (7.0.4.2)
activesupport (= 7.0.4.2)
erubi (~> 1.11)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
activejob (7.1.3.3)
activesupport (= 7.1.3.3)
globalid (>= 0.3.6)
activemodel (7.0.4.2)
activesupport (= 7.0.4.2)
activerecord (7.0.4.2)
activemodel (= 7.0.4.2)
activesupport (= 7.0.4.2)
activestorage (7.0.4.2)
actionpack (= 7.0.4.2)
activejob (= 7.0.4.2)
activerecord (= 7.0.4.2)
activesupport (= 7.0.4.2)
activemodel (7.1.3.3)
activesupport (= 7.1.3.3)
activerecord (7.1.3.3)
activemodel (= 7.1.3.3)
activesupport (= 7.1.3.3)
timeout (>= 0.4.0)
activestorage (7.1.3.3)
actionpack (= 7.1.3.3)
activejob (= 7.1.3.3)
activerecord (= 7.1.3.3)
activesupport (= 7.1.3.3)
marcel (~> 1.0)
mini_mime (>= 1.1.0)
activesupport (7.0.4.2)
activesupport (7.1.3.3)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
amq-protocol (2.3.2)
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.8)
builder (3.2.4)
bundler-audit (0.9.1)
bundler (>= 1.2.0, < 3)
Expand All @@ -86,86 +97,108 @@ GEM
amq-protocol (~> 2.3, >= 2.3.1)
sorted_set (~> 1, >= 1.0.2)
coderay (1.1.3)
concurrent-ruby (1.2.0)
concurrent-ruby (1.2.3)
connection_pool (2.4.1)
crass (1.0.6)
date (3.3.3)
diff-lcs (1.5.0)
date (3.3.4)
diff-lcs (1.5.1)
docile (1.4.0)
drb (2.2.1)
erubi (1.12.0)
globalid (1.1.0)
activesupport (>= 5.0)
i18n (1.12.0)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
io-console (0.7.2)
irb (1.13.1)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.6.3)
lamian (1.7.0)
rails (>= 4.2)
loofah (2.19.1)
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
nokogiri (>= 1.12.0)
mail (2.8.1)
mini_mime (>= 0.1.1)
net-imap
net-pop
net-smtp
marcel (1.0.2)
method_source (1.0.0)
mini_mime (1.1.2)
mini_portile2 (2.8.1)
minitest (5.17.0)
net-imap (0.3.4)
marcel (1.0.4)
method_source (1.1.0)
mini_mime (1.1.5)
mini_portile2 (2.8.6)
minitest (5.23.1)
mutex_m (0.2.0)
net-imap (0.4.11)
date
net-protocol
net-pop (0.1.2)
net-protocol
net-protocol (0.2.1)
net-protocol (0.2.2)
timeout
net-smtp (0.3.3)
net-smtp (0.5.0)
net-protocol
nio4r (2.5.8)
nokogiri (1.14.2)
mini_portile2 (~> 2.8.0)
nio4r (2.7.3)
nokogiri (1.16.5)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
parallel (1.22.1)
parser (3.2.1.0)
ast (~> 2.4.1)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
racc (1.6.2)
rack (2.2.6.2)
rack-test (2.0.2)
psych (5.1.2)
stringio
racc (1.8.0)
rack (3.0.11)
rack-session (2.0.0)
rack (>= 3.0.0)
rack-test (2.1.0)
rack (>= 1.3)
rails (7.0.4.2)
actioncable (= 7.0.4.2)
actionmailbox (= 7.0.4.2)
actionmailer (= 7.0.4.2)
actionpack (= 7.0.4.2)
actiontext (= 7.0.4.2)
actionview (= 7.0.4.2)
activejob (= 7.0.4.2)
activemodel (= 7.0.4.2)
activerecord (= 7.0.4.2)
activestorage (= 7.0.4.2)
activesupport (= 7.0.4.2)
rackup (2.1.0)
rack (>= 3)
webrick (~> 1.8)
rails (7.1.3.3)
actioncable (= 7.1.3.3)
actionmailbox (= 7.1.3.3)
actionmailer (= 7.1.3.3)
actionpack (= 7.1.3.3)
actiontext (= 7.1.3.3)
actionview (= 7.1.3.3)
activejob (= 7.1.3.3)
activemodel (= 7.1.3.3)
activerecord (= 7.1.3.3)
activestorage (= 7.1.3.3)
activesupport (= 7.1.3.3)
bundler (>= 1.15.0)
railties (= 7.0.4.2)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
railties (= 7.1.3.3)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
minitest
nokogiri (>= 1.6)
rails-html-sanitizer (1.5.0)
loofah (~> 2.19, >= 2.19.1)
railties (7.0.4.2)
actionpack (= 7.0.4.2)
activesupport (= 7.0.4.2)
method_source
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
railties (7.1.3.3)
actionpack (= 7.1.3.3)
activesupport (= 7.1.3.3)
irb
rackup (>= 1.0.0)
rake (>= 12.2)
thor (~> 1.0)
zeitwerk (~> 2.5)
thor (~> 1.0, >= 1.2.2)
zeitwerk (~> 2.6)
rainbow (3.1.1)
rake (13.0.6)
rake (13.2.1)
rbtree (0.4.6)
rdoc (6.7.0)
psych (>= 4.0.0)
regexp_parser (2.7.0)
rexml (3.2.5)
reline (0.5.7)
io-console (~> 0.5)
rexml (3.2.8)
strscan (>= 3.0.9)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
Expand All @@ -178,10 +211,10 @@ GEM
rspec-its (1.3.0)
rspec-core (>= 3.0.0)
rspec-expectations (>= 3.0.0)
rspec-mocks (3.12.3)
rspec-mocks (3.12.7)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.0)
rspec-support (3.12.2)
rubocop (1.43.0)
json (~> 2.3)
parallel (~> 1.10)
Expand Down Expand Up @@ -235,17 +268,20 @@ GEM
sorted_set (1.0.3)
rbtree
set (~> 1.0)
stringio (3.1.0)
strscan (3.1.0)
tainbox (2.1.2)
activesupport
thor (1.2.1)
timeout (0.3.2)
thor (1.3.1)
timeout (0.4.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.4.2)
websocket-driver (0.7.5)
webrick (1.8.1)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.6.7)
zeitwerk (2.6.15)

PLATFORMS
ruby
Expand Down
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,15 @@ Rabbit.publish(
`Rabbit::Daemon.run`. `before_fork` and `after_fork` procs in `Rabbit.config` are used
to teardown and setup external connections between daemon restarts, for example ORM connections
- After the server runs, received messages are handled by `Rabbit::EventHandler` subclasses.
Subclasses are selected by following code:
```ruby
"rabbit/handler/#{group_id}/#{event}".camelize.constantize
```
- After the server runs, received messages are handled by `Rabbit::EventHandler` subclasses in two possible ways:
a) Subclasses are selected by following code(by default):
```ruby
"rabbit/handler/#{group_id}/#{event}".camelize.constantize
```
b) you can change default behaviour to your own logic by setting the `handler_resolver_callable` config option with a `Proc` that should return the handler class:
```ruby
Rabbit.config.handler_resolver_callable = -> (group_id, event) { "recivers/#{group_id}/#{event}".camelize.constantize }
```
They use powerful `Tainbox` api to handle message data. Project_id also passed to them.
Expand Down
1 change: 1 addition & 0 deletions lib/rabbit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Config
attribute :environment, Symbol, default: :production
attribute :queue_name_conversion
attribute :receiving_job_class_callable
attribute :handler_resolver_callable
attribute :exception_notifier
attribute :before_receiving_hooks, default: []
attribute :after_receiving_hooks, default: []
Expand Down
9 changes: 7 additions & 2 deletions lib/rabbit/receiving/handler_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ def handler_for(message)
private

def unmemoized_handler_for(group_id, event)
name = "rabbit/handler/#{group_id}/#{event}".camelize
handler = name.safe_constantize
handler = if Rabbit.config.handler_resolver_callable.is_a?(Proc)
Rabbit.config.handler_resolver_callable.call(group_id, event)
else
name = "rabbit/handler/#{group_id}/#{event}".camelize
name.safe_constantize
end

if handler && handler < Rabbit::EventHandler
handler
else
Expand Down
Loading

0 comments on commit 8d6280d

Please sign in to comment.