strum-esb 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/.rspec +3 -0
- data/.rubocop.yml +25 -0
- data/.ruby-version +1 -0
- data/.travis.yml +6 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +80 -0
- data/README.md +36 -0
- data/Rakefile +6 -0
- data/bin/console +20 -0
- data/bin/setup +8 -0
- data/lib/strum/esb.rb +36 -0
- data/lib/strum/esb/action.rb +16 -0
- data/lib/strum/esb/event.rb +22 -0
- data/lib/strum/esb/handler.rb +70 -0
- data/lib/strum/esb/info.rb +16 -0
- data/lib/strum/esb/message.rb +29 -0
- data/lib/strum/esb/notice.rb +18 -0
- data/lib/strum/esb/version.rb +5 -0
- data/lib/strum/patch/sneakers/queue_patch.rb +145 -0
- data/strum-esb.gemspec +33 -0
- metadata +123 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 360c4b7cd86f34d2dd3628104474c6948cb926b852c493880b931d0f7a1730e6
|
4
|
+
data.tar.gz: 963fc92e530b52200a58e205247ded0728e1247aa8c865c69f3aa5d56fe729fa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9ffa2333d5dbbd263b32c7944acbfe2f82aa9233c8f7e6f2337f2444f134fb88b33ec260e377c1d4126609ad1194f62a24acce30a0cdc34f919f027da62db9a1
|
7
|
+
data.tar.gz: 740715b0c16c4c2f270f94fd2fca43aea88194f9c0352860b31fdb74758ffafa92cbaefb755f783b6de540473779d48a81a4b45ed50f8bb6270e7e350d3c8126
|
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/_yardoc/
|
4
|
+
/coverage/
|
5
|
+
/doc/
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/tmp/
|
9
|
+
|
10
|
+
# rspec failure tracking
|
11
|
+
.rspec_status
|
12
|
+
|
13
|
+
# Editor directories and files
|
14
|
+
.idea
|
15
|
+
.vscode
|
16
|
+
*.suo
|
17
|
+
*.ntvs*
|
18
|
+
*.njsproj
|
19
|
+
*.sln
|
20
|
+
*.sw*
|
21
|
+
|
22
|
+
# builds
|
23
|
+
*.gem
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.6
|
3
|
+
|
4
|
+
# Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
|
5
|
+
Style/HashSyntax:
|
6
|
+
Enabled: true
|
7
|
+
|
8
|
+
# Method definitions after `private` or `protected` isolated calls need one
|
9
|
+
# extra level of indentation.
|
10
|
+
Layout/IndentationConsistency:
|
11
|
+
Enabled: true
|
12
|
+
EnforcedStyle: indented_internal_methods
|
13
|
+
|
14
|
+
# Two spaces, no tabs (for indentation).
|
15
|
+
Layout/IndentationWidth:
|
16
|
+
Enabled: true
|
17
|
+
|
18
|
+
# Check quotes usage according to lint rule below.
|
19
|
+
Style/StringLiterals:
|
20
|
+
Enabled: true
|
21
|
+
EnforcedStyle: double_quotes
|
22
|
+
|
23
|
+
Metrics/LineLength:
|
24
|
+
Max: 120
|
25
|
+
IgnoredPatterns: ['\A#']
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.7.0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in strum-esb.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
gem "bundler", "~> 2.1.4"
|
7
|
+
gem "rake", "~> 12"
|
8
|
+
gem "rspec", "~> 3"
|
9
|
+
gem "rubocop", "~> 0.62.0"
|
10
|
+
|
11
|
+
gem "debase", "~> 0.2.4"
|
12
|
+
gem "ruby-debug-ide", "~> 0.7.0"
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
strum-esb (0.0.1)
|
5
|
+
bunny (~> 2.15)
|
6
|
+
connection_pool (~> 2.2.2)
|
7
|
+
json (~> 2.3)
|
8
|
+
sneakers (~> 2.12)
|
9
|
+
|
10
|
+
GEM
|
11
|
+
remote: https://rubygems.org/
|
12
|
+
specs:
|
13
|
+
amq-protocol (2.3.1)
|
14
|
+
ast (2.4.0)
|
15
|
+
bunny (2.15.0)
|
16
|
+
amq-protocol (~> 2.3, >= 2.3.1)
|
17
|
+
concurrent-ruby (1.1.6)
|
18
|
+
connection_pool (2.2.2)
|
19
|
+
debase (0.2.4.1)
|
20
|
+
debase-ruby_core_source (>= 0.10.2)
|
21
|
+
debase-ruby_core_source (0.10.9)
|
22
|
+
diff-lcs (1.3)
|
23
|
+
jaro_winkler (1.5.4)
|
24
|
+
json (2.3.0)
|
25
|
+
parallel (1.19.1)
|
26
|
+
parser (2.7.1.1)
|
27
|
+
ast (~> 2.4.0)
|
28
|
+
powerpack (0.1.2)
|
29
|
+
rainbow (3.0.0)
|
30
|
+
rake (12.3.3)
|
31
|
+
rspec (3.9.0)
|
32
|
+
rspec-core (~> 3.9.0)
|
33
|
+
rspec-expectations (~> 3.9.0)
|
34
|
+
rspec-mocks (~> 3.9.0)
|
35
|
+
rspec-core (3.9.1)
|
36
|
+
rspec-support (~> 3.9.1)
|
37
|
+
rspec-expectations (3.9.1)
|
38
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
39
|
+
rspec-support (~> 3.9.0)
|
40
|
+
rspec-mocks (3.9.1)
|
41
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
+
rspec-support (~> 3.9.0)
|
43
|
+
rspec-support (3.9.2)
|
44
|
+
rubocop (0.62.0)
|
45
|
+
jaro_winkler (~> 1.5.1)
|
46
|
+
parallel (~> 1.10)
|
47
|
+
parser (>= 2.5, != 2.5.1.1)
|
48
|
+
powerpack (~> 0.1)
|
49
|
+
rainbow (>= 2.2.2, < 4.0)
|
50
|
+
ruby-progressbar (~> 1.7)
|
51
|
+
unicode-display_width (~> 1.4.0)
|
52
|
+
ruby-debug-ide (0.7.0)
|
53
|
+
rake (>= 0.8.1)
|
54
|
+
ruby-progressbar (1.10.1)
|
55
|
+
serverengine (2.1.1)
|
56
|
+
sigdump (~> 0.2.2)
|
57
|
+
sigdump (0.2.4)
|
58
|
+
sneakers (2.12.0)
|
59
|
+
bunny (~> 2.14)
|
60
|
+
concurrent-ruby (~> 1.0)
|
61
|
+
rake (~> 12.3)
|
62
|
+
serverengine (~> 2.1.0)
|
63
|
+
thor
|
64
|
+
thor (1.0.1)
|
65
|
+
unicode-display_width (1.4.1)
|
66
|
+
|
67
|
+
PLATFORMS
|
68
|
+
ruby
|
69
|
+
|
70
|
+
DEPENDENCIES
|
71
|
+
bundler (~> 2.1.4)
|
72
|
+
debase (~> 0.2.4)
|
73
|
+
rake (~> 12)
|
74
|
+
rspec (~> 3)
|
75
|
+
rubocop (~> 0.62.0)
|
76
|
+
ruby-debug-ide (~> 0.7.0)
|
77
|
+
strum-esb!
|
78
|
+
|
79
|
+
BUNDLED WITH
|
80
|
+
2.1.4
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Strum::Esb
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/strum/esb`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'strum-esb'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install strum-esb
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
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.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/strum-esb.
|
36
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "strum/esb"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
Strum::Esb::Message.rabbit_connection ||= begin
|
14
|
+
rabbit_connection = Bunny.new
|
15
|
+
rabbit_connection.start
|
16
|
+
rabbit_connection
|
17
|
+
end
|
18
|
+
|
19
|
+
require "irb"
|
20
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/strum/esb.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require "strum/esb/version"
|
2
|
+
|
3
|
+
require "json"
|
4
|
+
require "bunny"
|
5
|
+
require "sneakers"
|
6
|
+
require_relative "patch/sneakers/queue_patch"
|
7
|
+
require_relative "esb/handler"
|
8
|
+
require_relative "esb/message"
|
9
|
+
require_relative "esb/action"
|
10
|
+
require_relative "esb/event"
|
11
|
+
require_relative "esb/notice"
|
12
|
+
require_relative "esb/info"
|
13
|
+
|
14
|
+
module Strum
|
15
|
+
module Esb
|
16
|
+
class Error < StandardError; end
|
17
|
+
|
18
|
+
Sneakers.configure(
|
19
|
+
log: STDOUT,
|
20
|
+
hooks: {
|
21
|
+
before_fork: lambda { DB.disconnect if defined?(DB) },
|
22
|
+
after_fork: lambda do
|
23
|
+
Strum::Esb::Message.rabbit_connection ||= begin
|
24
|
+
rabbit_connection = Bunny.new
|
25
|
+
rabbit_connection.start
|
26
|
+
rabbit_connection
|
27
|
+
end
|
28
|
+
end
|
29
|
+
},
|
30
|
+
exchange: "strum.general",
|
31
|
+
exchange_type: "headers"
|
32
|
+
)
|
33
|
+
Sneakers.logger.level = Logger::INFO
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "message"
|
4
|
+
|
5
|
+
module Strum
|
6
|
+
module Esb
|
7
|
+
# Action message
|
8
|
+
class Action < Message
|
9
|
+
class << self
|
10
|
+
def call(payload, action, resource, exchange: "strum.actions", chain: Thread.current[:chain])
|
11
|
+
publish(headers: { resource: resource, action: action }, payload: payload, exchange: exchange, chain: chain)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "message"
|
4
|
+
|
5
|
+
module Strum
|
6
|
+
module Esb
|
7
|
+
# Event message
|
8
|
+
class Event < Message
|
9
|
+
class << self
|
10
|
+
def call(payload, resource, event, state, exchange:, chain:)
|
11
|
+
publish(headers: { resource: resource, event: event, state: state }, payload: payload, exchange: exchange, chain: chain)
|
12
|
+
end
|
13
|
+
|
14
|
+
%i[success failure].each do |method_name|
|
15
|
+
define_method method_name do |payload, resource, *events, exchange: "strum.events", chain: Thread.current[:chain]|
|
16
|
+
events.each { |event| call(payload, resource, event, method_name, exchange: exchange, chain: chain) }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
module Strum
|
6
|
+
module Esb
|
7
|
+
# Sneakers helper for Strum
|
8
|
+
module Handler
|
9
|
+
def self.included(base)
|
10
|
+
base.class_eval do
|
11
|
+
include Sneakers::Worker
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def work_with_params(deserialized_msg, _delivery_info, metadata)
|
16
|
+
notice_data = JSON.parse(deserialized_msg)
|
17
|
+
|
18
|
+
snake_case_modify = ->(string) { string.nil? ? nil : string&.to_s.gsub(/[^a-zA-Z0-9]/, "_")&.downcase }
|
19
|
+
parse_header = ->(string) { metadata[:headers] && metadata[:headers][string] }
|
20
|
+
header = parse_header >> snake_case_modify
|
21
|
+
|
22
|
+
action = header.call("action")
|
23
|
+
resource = header.call("resource")
|
24
|
+
event = header.call("event")
|
25
|
+
state = header.call("state")
|
26
|
+
info = header.call("info")
|
27
|
+
chain = header.call("chain")
|
28
|
+
notice = header.call("notice")
|
29
|
+
Thread.current[:chain] = chain
|
30
|
+
|
31
|
+
methods_names = if action
|
32
|
+
%W[action_#{action}_#{resource} action_handler]
|
33
|
+
elsif event
|
34
|
+
result = %W[event_#{resource}_#{event}_#{state}]
|
35
|
+
result << "event_#{resource}_#{event}" if state.eql?("success")
|
36
|
+
result << "event_handler"
|
37
|
+
result
|
38
|
+
elsif info
|
39
|
+
%W[info_#{info} info_handler]
|
40
|
+
elsif notice
|
41
|
+
result = []
|
42
|
+
result << "notice_#{resource}_#{notice}" if resource
|
43
|
+
result << "notice_#{notice}"
|
44
|
+
result << "notice_handler"
|
45
|
+
result
|
46
|
+
end
|
47
|
+
|
48
|
+
method_name = ([*methods_names] << "handler").find { |n| respond_to?(n, include_all: true) }
|
49
|
+
|
50
|
+
unless method_name
|
51
|
+
logger.debug "Handler not found. Message rejected #{metadata[:headers]} with payload #{notice_data}"
|
52
|
+
return reject!
|
53
|
+
end
|
54
|
+
|
55
|
+
method_params = method(method_name)
|
56
|
+
.parameters
|
57
|
+
.map { |param| _, param_name = param; param_name }
|
58
|
+
.then { |m| m & %I[action resource event state chain info] }
|
59
|
+
handler_params = method_params.each_with_object({}) { |i, res| res[i] = eval(i.to_s); }
|
60
|
+
logger.debug("Handler #{method_name} found. Payload: #{notice_data}")
|
61
|
+
handler_params.count.positive? ? send(method_name, notice_data, handler_params) : send(method_name, notice_data)
|
62
|
+
logger.debug("Handler #{method_name} executed")
|
63
|
+
ack!
|
64
|
+
rescue StandardError => e
|
65
|
+
logger.error e
|
66
|
+
reject!
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "message"
|
4
|
+
|
5
|
+
module Strum
|
6
|
+
module Esb
|
7
|
+
# Action message
|
8
|
+
class Info < Message
|
9
|
+
class << self
|
10
|
+
def call(payload, resource, exchange: "strum.info", chain: Thread.current[:chain])
|
11
|
+
publish(headers: { info: resource }, payload: payload, exchange: exchange, chain: chain)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Strum
|
4
|
+
module Esb
|
5
|
+
# Strum Message
|
6
|
+
class Message
|
7
|
+
class << self
|
8
|
+
attr_accessor :rabbit_connection
|
9
|
+
|
10
|
+
def publish(exchange:, headers:, payload:, chain:)
|
11
|
+
rabbit_exchange ||= Strum::Esb::Message.rabbit_channel.headers(exchange, durable: true)
|
12
|
+
if chain
|
13
|
+
payload["chain"] ||= chain
|
14
|
+
headers["chain"] ||= chain
|
15
|
+
end
|
16
|
+
rabbit_exchange.publish(payload.to_json, headers: headers, content_type: "application/json")
|
17
|
+
end
|
18
|
+
|
19
|
+
def rabbit_channel
|
20
|
+
@rabbit_channel ||= begin
|
21
|
+
raise "Strum::Esb::Message.rabbit_connection must be setup" unless rabbit_connection
|
22
|
+
rabbit_connection.create_channel
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "message"
|
4
|
+
|
5
|
+
module Strum
|
6
|
+
module Esb
|
7
|
+
# Notice message
|
8
|
+
class Notice < Message
|
9
|
+
class << self
|
10
|
+
def call(payload, notice, resource = nil, exchange: "strum.notice", chain: Thread.current[:chain])
|
11
|
+
headers = { notice: notice }
|
12
|
+
headers[:resource] = resource if resource
|
13
|
+
publish(headers: headers, payload: payload, exchange: exchange, chain: chain)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sneakers/queue"
|
4
|
+
|
5
|
+
module QueuePatch
|
6
|
+
def subscribe(worker)
|
7
|
+
if @opts[:bindings]
|
8
|
+
qiwa_subscribe(worker)
|
9
|
+
elsif @opts[:header_bindings]
|
10
|
+
header_subscribe(worker)
|
11
|
+
else
|
12
|
+
super(worker)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# bindings: {
|
17
|
+
# actions: "get/account",
|
18
|
+
# events: %w[account/update account/delete],
|
19
|
+
# infos: "account"
|
20
|
+
# }
|
21
|
+
def qiwa_subscribe(worker)
|
22
|
+
@opts[:header_bindings] = []
|
23
|
+
actions_subscribes([*(@opts[:bindings][:actions] || @opts[:bindings][:action])])
|
24
|
+
events_subscribes([*(@opts[:bindings][:events] || @opts[:bindings][:event])])
|
25
|
+
infos_subscribes([*(@opts[:bindings][:infos] || @opts[:bindings][:info])])
|
26
|
+
notices_subscribes([*(@opts[:bindings][:notices] || @opts[:bindings][:notice])])
|
27
|
+
header_subscribe(worker)
|
28
|
+
end
|
29
|
+
|
30
|
+
def actions_subscribes(actions)
|
31
|
+
actions.each do |_action|
|
32
|
+
exchange, resource_action = _action.split(":")
|
33
|
+
if resource_action.nil?
|
34
|
+
resource_action = exchange
|
35
|
+
exchange = "strum.actions"
|
36
|
+
end
|
37
|
+
raise StandardError "binding format must be a `exchange:action/resource`" unless exchange && resource_action
|
38
|
+
|
39
|
+
action, resource = resource_action.split("/")
|
40
|
+
raise StandardError "binding format must be a `exchange:action/resource`" unless resource && action
|
41
|
+
|
42
|
+
@opts[:header_bindings] << {
|
43
|
+
exchange: exchange,
|
44
|
+
arguments: {
|
45
|
+
action: action,
|
46
|
+
resource: resource
|
47
|
+
}
|
48
|
+
}
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def events_subscribes(events)
|
53
|
+
events.each do |_event|
|
54
|
+
exchange, resource_event = _event.split(":")
|
55
|
+
if resource_event.nil?
|
56
|
+
resource_event = exchange
|
57
|
+
exchange = "strum.events"
|
58
|
+
end
|
59
|
+
raise StandardError "binding format must be `exchange:resource/event{/state}`" unless exchange && resource_event
|
60
|
+
|
61
|
+
resource, event, state = resource_event.split("/")
|
62
|
+
raise StandardError "binding format must be `exchange:resource/event{/state}`" unless resource && event
|
63
|
+
|
64
|
+
@opts[:header_bindings] << {
|
65
|
+
exchange: exchange,
|
66
|
+
arguments: {
|
67
|
+
resource: resource,
|
68
|
+
event: event,
|
69
|
+
state: state || "success"
|
70
|
+
}
|
71
|
+
}
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def infos_subscribes(infos)
|
76
|
+
infos.each do |_info|
|
77
|
+
exchange, resource = _info.split(":")
|
78
|
+
if resource.nil?
|
79
|
+
resource = exchange
|
80
|
+
exchange = "strum.info"
|
81
|
+
end
|
82
|
+
raise StandardError "binding format must be a `exchange:resource`" unless exchange && resource
|
83
|
+
|
84
|
+
@opts[:header_bindings] << {
|
85
|
+
exchange: exchange,
|
86
|
+
arguments: {
|
87
|
+
info: resource
|
88
|
+
}
|
89
|
+
}
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def notices_subscribes(notices)
|
94
|
+
notices.each do |_info|
|
95
|
+
exchange, resource = _info.split(":")
|
96
|
+
if resource.nil?
|
97
|
+
resource = exchange
|
98
|
+
exchange = "strum.info"
|
99
|
+
end
|
100
|
+
raise StandardError "binding format must be a `exchange:resource`" unless exchange && resource
|
101
|
+
|
102
|
+
@opts[:header_bindings] << {
|
103
|
+
exchange: exchange,
|
104
|
+
arguments: {
|
105
|
+
notice: resource
|
106
|
+
}
|
107
|
+
}
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def header_subscribe(worker)
|
112
|
+
@bunny = @opts[:connection]
|
113
|
+
@bunny ||= create_bunny_connection
|
114
|
+
@bunny.start
|
115
|
+
|
116
|
+
@channel = @bunny.create_channel
|
117
|
+
@channel.prefetch(@opts[:prefetch])
|
118
|
+
|
119
|
+
handler_klass = worker.opts[:handler] || Sneakers::CONFIG.fetch(:handler)
|
120
|
+
# Configure options if needed
|
121
|
+
if handler_klass.respond_to?(:configure_queue)
|
122
|
+
@opts[:queue_options] = handler_klass.configure_queue(@name, @opts[:queue_options])
|
123
|
+
end
|
124
|
+
|
125
|
+
queue = @channel.queue(@name, @opts[:queue_options])
|
126
|
+
|
127
|
+
header_bindings = [*@opts[:header_bindings]]
|
128
|
+
header_bindings.each do |header_binding|
|
129
|
+
exchange_name = header_binding[:exchange] || @opts[:exchange]
|
130
|
+
exchange = @channel.exchange(exchange_name, @opts[:exchange_options].merge(type: :headers))
|
131
|
+
queue.bind(exchange, arguments: { "x-match": :all }.merge(header_binding[:arguments]))
|
132
|
+
end
|
133
|
+
|
134
|
+
handler = handler_klass.new(@channel, queue, worker.opts)
|
135
|
+
|
136
|
+
@consumer = queue.subscribe(block: false, manual_ack: @opts[:ack]) do |delivery_info, metadata, msg|
|
137
|
+
worker.do_work(delivery_info, metadata, msg, handler)
|
138
|
+
end
|
139
|
+
nil
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
class Sneakers::Queue
|
144
|
+
prepend QueuePatch
|
145
|
+
end
|
data/strum-esb.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require_relative 'lib/strum/esb/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "strum-esb"
|
5
|
+
spec.version = Strum::Esb::VERSION
|
6
|
+
spec.authors = ["Serhiy Nazarov"]
|
7
|
+
spec.email = ["sn@nazarov.com.ua"]
|
8
|
+
|
9
|
+
spec.summary = "Publish and subscribe rabbitMQ messages"
|
10
|
+
spec.description = "Publish and subscribe rabbitMQ messages"
|
11
|
+
spec.homepage = "https://code.qpard.com/strum/strum-esb"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
13
|
+
|
14
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = "https://code.qpard.com/strum/strum-esb"
|
18
|
+
spec.metadata["changelog_uri"] = "https://code.qpard.com/strum/strum-esb/CHANGELOG.md"
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_dependency "bunny", "~> 2.15"
|
30
|
+
spec.add_dependency "connection_pool", "~> 2.2.2"
|
31
|
+
spec.add_dependency "json", "~> 2.3"
|
32
|
+
spec.add_dependency "sneakers", "~> 2.12"
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: strum-esb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Serhiy Nazarov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-04-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bunny
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.15'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: connection_pool
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.2.2
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.2.2
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: json
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.3'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sneakers
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.12'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.12'
|
69
|
+
description: Publish and subscribe rabbitMQ messages
|
70
|
+
email:
|
71
|
+
- sn@nazarov.com.ua
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".rubocop.yml"
|
79
|
+
- ".ruby-version"
|
80
|
+
- ".travis.yml"
|
81
|
+
- Gemfile
|
82
|
+
- Gemfile.lock
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/strum/esb.rb
|
88
|
+
- lib/strum/esb/action.rb
|
89
|
+
- lib/strum/esb/event.rb
|
90
|
+
- lib/strum/esb/handler.rb
|
91
|
+
- lib/strum/esb/info.rb
|
92
|
+
- lib/strum/esb/message.rb
|
93
|
+
- lib/strum/esb/notice.rb
|
94
|
+
- lib/strum/esb/version.rb
|
95
|
+
- lib/strum/patch/sneakers/queue_patch.rb
|
96
|
+
- strum-esb.gemspec
|
97
|
+
homepage: https://code.qpard.com/strum/strum-esb
|
98
|
+
licenses: []
|
99
|
+
metadata:
|
100
|
+
allowed_push_host: https://rubygems.org
|
101
|
+
homepage_uri: https://code.qpard.com/strum/strum-esb
|
102
|
+
source_code_uri: https://code.qpard.com/strum/strum-esb
|
103
|
+
changelog_uri: https://code.qpard.com/strum/strum-esb/CHANGELOG.md
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: 2.3.0
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubygems_version: 3.1.2
|
120
|
+
signing_key:
|
121
|
+
specification_version: 4
|
122
|
+
summary: Publish and subscribe rabbitMQ messages
|
123
|
+
test_files: []
|