event_bus_rb 1.0.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 +7 -0
- data/.env.example +4 -0
- data/.gitignore +11 -0
- data/.pronto.yml +10 -0
- data/.rspec +2 -0
- data/.rubocop.yml +0 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +62 -0
- data/LICENSE.txt +21 -0
- data/README.md +146 -0
- data/Rakefile +6 -0
- data/bin/cisetup +5 -0
- data/bin/console +14 -0
- data/bin/rspec +16 -0
- data/bin/setup +8 -0
- data/circle.yml +8 -0
- data/event_bus_rb.gemspec +37 -0
- data/examples/daemon.rb +32 -0
- data/examples/emitter.rb +42 -0
- data/examples/listener.rb +30 -0
- data/lib/event_bus/broker/base.rb +37 -0
- data/lib/event_bus/broker/rabbit.rb +39 -0
- data/lib/event_bus/broker/rabbit/queue.rb +47 -0
- data/lib/event_bus/broker/rabbit/topic.rb +33 -0
- data/lib/event_bus/config.rb +13 -0
- data/lib/event_bus/daemon.rb +34 -0
- data/lib/event_bus/emitter.rb +23 -0
- data/lib/event_bus/event.rb +47 -0
- data/lib/event_bus/listener.rb +15 -0
- data/lib/event_bus/listeners/base.rb +13 -0
- data/lib/event_bus/listeners/manager.rb +26 -0
- data/lib/event_bus/version.rb +3 -0
- data/lib/event_bus_rb.rb +21 -0
- metadata +162 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e5dfa47070d19c605963f2baa34132dea814a1a9
|
4
|
+
data.tar.gz: ebab64c22accb7c2656ef4921846896384e9a138
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0a7882a67f07fe66e900a395554d55dfc5ad8f767cd597cd6e92dbc7b47a607b675459e6986d655c1aadfd6ea877d9fb9cd5fa8c8d75cd5b589e7280a8ae1d65
|
7
|
+
data.tar.gz: 25d628ece3309de4f17cb504df1fe6dfd01eee2d5fa9c8bf530e039ef135de1f11c990001307dd434782a6f434845157ad68b1aaba102a98c1102166ce9435b9
|
data/.env.example
ADDED
data/.gitignore
ADDED
data/.pronto.yml
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
File without changes
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.1
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
event_bus (1.0.0)
|
5
|
+
bunny (~> 2.7.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
amq-protocol (2.2.0)
|
11
|
+
awesome_print (1.7.0)
|
12
|
+
bunny (2.7.0)
|
13
|
+
amq-protocol (>= 2.2.0)
|
14
|
+
byebug (9.0.6)
|
15
|
+
coderay (1.1.1)
|
16
|
+
diff-lcs (1.3)
|
17
|
+
dotenv (2.2.1)
|
18
|
+
method_source (0.8.2)
|
19
|
+
pry (0.10.4)
|
20
|
+
coderay (~> 1.1.0)
|
21
|
+
method_source (~> 0.8.1)
|
22
|
+
slop (~> 3.4)
|
23
|
+
pry-byebug (3.4.2)
|
24
|
+
byebug (~> 9.0)
|
25
|
+
pry (~> 0.10)
|
26
|
+
pry-meta (0.0.10)
|
27
|
+
awesome_print
|
28
|
+
pry
|
29
|
+
pry-byebug
|
30
|
+
pry-remote
|
31
|
+
pry-remote (0.1.8)
|
32
|
+
pry (~> 0.9)
|
33
|
+
slop (~> 3.0)
|
34
|
+
rake (12.0.0)
|
35
|
+
rspec (3.6.0)
|
36
|
+
rspec-core (~> 3.6.0)
|
37
|
+
rspec-expectations (~> 3.6.0)
|
38
|
+
rspec-mocks (~> 3.6.0)
|
39
|
+
rspec-core (3.6.0)
|
40
|
+
rspec-support (~> 3.6.0)
|
41
|
+
rspec-expectations (3.6.0)
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
+
rspec-support (~> 3.6.0)
|
44
|
+
rspec-mocks (3.6.0)
|
45
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
46
|
+
rspec-support (~> 3.6.0)
|
47
|
+
rspec-support (3.6.0)
|
48
|
+
slop (3.6.0)
|
49
|
+
|
50
|
+
PLATFORMS
|
51
|
+
ruby
|
52
|
+
|
53
|
+
DEPENDENCIES
|
54
|
+
bundler
|
55
|
+
dotenv
|
56
|
+
event_bus!
|
57
|
+
pry-meta
|
58
|
+
rake
|
59
|
+
rspec
|
60
|
+
|
61
|
+
BUNDLED WITH
|
62
|
+
1.14.6
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Rafael Jesus
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
# EventBus
|
2
|
+
|
3
|
+
[](https://circleci.com/gh/EmpregoLigado/event_bus_rb)
|
4
|
+
|
5
|
+

|
6
|
+
|
7
|
+
EventBus provides additional messaging patterns for EmpregoLigado applications.
|
8
|
+
It exposes some methods for messaging-related features:
|
9
|
+
- EventBus::Emitter.trigger(event)
|
10
|
+
- EventBus::Listener.on(event_name)
|
11
|
+
- Extending EventBus::Listeners::Base
|
12
|
+
|
13
|
+
Let's look at a diagram for EventBus:
|
14
|
+
|
15
|
+
<p align='center'>
|
16
|
+
<img src='https://cloud.githubusercontent.com/assets/10248067/11762943/5a927e54-a0bd-11e5-8aa5-e0fafae0e559.png' alt='EventsBus diagram'>
|
17
|
+
</p>
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
Add this line to your application's Gemfile:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
gem 'event_bus_rb'
|
25
|
+
```
|
26
|
+
|
27
|
+
And then execute:
|
28
|
+
|
29
|
+
$ bundle
|
30
|
+
|
31
|
+
Or install it yourself as:
|
32
|
+
|
33
|
+
$ gem install event_bus_rb
|
34
|
+
|
35
|
+
And set env vars:
|
36
|
+
```ruby
|
37
|
+
ENV['RABBIT_URL'] = 'amqp://guest:guest@localhost:5672'
|
38
|
+
ENV['RABBIT_EVENT_BUS_APP_NAME'] = 'EventBusExampleApp'
|
39
|
+
ENV['RABBIT_EVENT_BUS_VHOST'] = 'event_bus'
|
40
|
+
ENV['RABBIT_EVENT_BUS_TOPIC_NAME'] = 'event_bus'
|
41
|
+
````
|
42
|
+
|
43
|
+
## Usage
|
44
|
+
|
45
|
+
### Emitter
|
46
|
+
A application can trigger events based passing an EventBus::Event instance,
|
47
|
+
so others systems that listens to these events based matching the routing key (used as event_name below) will receive the event payload content.
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
require 'event_bus_rb'
|
51
|
+
|
52
|
+
event_name = 'resource.origin.action'
|
53
|
+
body = { resource: 'full' }
|
54
|
+
event = EventBus::Event.new(event_name, body)
|
55
|
+
|
56
|
+
EventBus::Emitter.trigger(event)
|
57
|
+
|
58
|
+
EventBus::Config.broker.close_connection
|
59
|
+
```
|
60
|
+
[See more details](https://github.com/EmpregoLigado/event_bus_rb/blob/master/examples/emitter.rb)
|
61
|
+
|
62
|
+
### Listeners
|
63
|
+
|
64
|
+
An application that uses `EventBus` to handle _events_ should be treated as a
|
65
|
+
daemon like any other. The `Listener` api handles the blocking looping system,
|
66
|
+
so the client doesn`t need to think about those implementation details.
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
require 'event_bus_rb'
|
70
|
+
|
71
|
+
event_name = 'resource.origin.action'
|
72
|
+
|
73
|
+
puts 'Start receiving messages'
|
74
|
+
|
75
|
+
EventBus::Listener.on(event_name) do |event|
|
76
|
+
puts ""
|
77
|
+
puts " - Received a message from #{event.name}:"
|
78
|
+
puts " Message: #{event.body}"
|
79
|
+
puts ""
|
80
|
+
end
|
81
|
+
|
82
|
+
puts 'Stop receiving messages'
|
83
|
+
|
84
|
+
EventBus::Config.broker.close_connection
|
85
|
+
```
|
86
|
+
|
87
|
+
[See more details](https://github.com/EmpregoLigado/event_bus_rb/blob/master/examples/listener.rb)
|
88
|
+
|
89
|
+
#### Multiple events routing
|
90
|
+
|
91
|
+
If your application needs to handle with loads of events you can extends ```EventBus::Listeners::Base``` and bind events to methods.
|
92
|
+
|
93
|
+
A simplistic example can be written like so:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
require 'event_bus_rb'
|
97
|
+
|
98
|
+
class CustomEventListener < EventBus::Listeners::Base
|
99
|
+
bind :pay, 'resource.custom.pay'
|
100
|
+
bind :receive, 'resource.custom.receive'
|
101
|
+
|
102
|
+
def pay(event)
|
103
|
+
puts "Paid #{event.body['amount']} for #{event.body['name']} ~> #{event.name}"
|
104
|
+
end
|
105
|
+
|
106
|
+
def receive(event)
|
107
|
+
puts "Received #{event.body['amount']} from #{event.body['name']} ~> #{event.name}"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
```
|
111
|
+
[See more details](https://github.com/EmpregoLigado/event_bus_rb/blob/master/examples/daemon.rb)
|
112
|
+
|
113
|
+
You can also bind more than one routing keys to methods using `*` or `#`.
|
114
|
+
|
115
|
+
Using `resource.*.pay`, for example, you can bind routing keys prefixed by **resource.** and suffixed by **.pay** to a method:
|
116
|
+
|
117
|
+
```
|
118
|
+
# bind resource.everything.pay to pay method
|
119
|
+
bind :pay, 'resource.*.pay'
|
120
|
+
```
|
121
|
+
|
122
|
+
|
123
|
+
Using `#`, you can bind all routing keys to a method:
|
124
|
+
|
125
|
+
```
|
126
|
+
# bind everything to receive method
|
127
|
+
bind :receive, '#'
|
128
|
+
```
|
129
|
+
|
130
|
+
## Development
|
131
|
+
|
132
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
133
|
+
|
134
|
+
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).
|
135
|
+
|
136
|
+
## Contributing
|
137
|
+
|
138
|
+
- Fork it
|
139
|
+
- Create your feature branch (`git checkout -b my-new-feature`)
|
140
|
+
- Commit your changes (`git commit -am 'Add some feature'`)
|
141
|
+
- Push to the branch (`git push origin my-new-feature`)
|
142
|
+
- Create new Pull Request
|
143
|
+
|
144
|
+
## License
|
145
|
+
|
146
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/cisetup
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "event_bus"
|
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
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/rspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'rspec' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require "pathname"
|
10
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require "rubygems"
|
14
|
+
require "bundler/setup"
|
15
|
+
|
16
|
+
load Gem.bin_path("rspec-core", "rspec")
|
data/bin/setup
ADDED
data/circle.yml
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'event_bus/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'event_bus_rb'
|
8
|
+
spec.version = EventBus::VERSION
|
9
|
+
spec.authors = ['Emprego Ligado']
|
10
|
+
spec.email = ['vagas@empregoligado.com.br']
|
11
|
+
|
12
|
+
spec.summary = %q{Expose an api to produce and consume events.}
|
13
|
+
spec.description = %q{Tool to produce and consume events to a distributed and async architecture}
|
14
|
+
spec.homepage = 'http://gems.empregoligado.com.br'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
21
|
+
else
|
22
|
+
raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = 'exe'
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ['lib']
|
29
|
+
|
30
|
+
spec.add_development_dependency 'bundler', '~> 0'
|
31
|
+
spec.add_development_dependency 'rake', '~> 0'
|
32
|
+
spec.add_development_dependency 'rspec', '~> 0'
|
33
|
+
spec.add_development_dependency 'pry-meta', '~> 0'
|
34
|
+
spec.add_development_dependency 'dotenv', '~> 0'
|
35
|
+
|
36
|
+
spec.add_dependency 'bunny', '~> 2.7'
|
37
|
+
end
|
data/examples/daemon.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
ENV['RABBIT_URL'] = 'amqp://guest:guest@localhost:5672'
|
4
|
+
ENV['RABBIT_EVENT_BUS_APP_NAME'] = 'EventBusExampleApp'
|
5
|
+
ENV['RABBIT_EVENT_BUS_VHOST'] = 'event_bus'
|
6
|
+
ENV['RABBIT_EVENT_BUS_TOPIC_NAME'] = 'event_bus'
|
7
|
+
|
8
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
9
|
+
|
10
|
+
require 'rubygems'
|
11
|
+
require 'bundler'
|
12
|
+
Bundler.require(:default, 'development')
|
13
|
+
|
14
|
+
require 'event_bus'
|
15
|
+
|
16
|
+
|
17
|
+
class CustomEventListener < EventBus::Listeners::Base
|
18
|
+
bind :pay, 'resource.custom.pay'
|
19
|
+
bind :receive, 'resource.custom.receive'
|
20
|
+
|
21
|
+
def pay(event)
|
22
|
+
puts "Paid #{event.body['amount']} for #{event.body['name']} ~> #{event.name}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def receive(event)
|
26
|
+
puts "Received #{event.body['amount']} from #{event.body['name']} ~> #{event.name}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
puts "****************** Daemon Ready ******************"
|
31
|
+
|
32
|
+
EventBus::Daemon.start
|
data/examples/emitter.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
ENV['RABBIT_URL'] = 'amqp://guest:guest@localhost:5672'
|
4
|
+
ENV['RABBIT_EVENT_BUS_APP_NAME'] = 'EventBusExampleApp'
|
5
|
+
ENV['RABBIT_EVENT_BUS_VHOST'] = 'event_bus'
|
6
|
+
ENV['RABBIT_EVENT_BUS_TOPIC_NAME'] = 'event_bus'
|
7
|
+
|
8
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
9
|
+
|
10
|
+
require 'rubygems'
|
11
|
+
require 'bundler'
|
12
|
+
Bundler.require(:default, 'development')
|
13
|
+
|
14
|
+
require 'event_bus'
|
15
|
+
|
16
|
+
|
17
|
+
events = []
|
18
|
+
|
19
|
+
event_name = 'resource.custom.pay'
|
20
|
+
body = { amount: 1500, name: 'John' }
|
21
|
+
|
22
|
+
events.push(EventBus::Event.new(event_name, body))
|
23
|
+
|
24
|
+
event_name = 'resource.custom.receive'
|
25
|
+
body = { amount: 300, name: 'George' }
|
26
|
+
|
27
|
+
events.push(EventBus::Event.new(event_name, body))
|
28
|
+
|
29
|
+
event_name = 'resource.origin.action'
|
30
|
+
body = { bo: 'dy' }
|
31
|
+
schemaVersion = 4.2
|
32
|
+
|
33
|
+
event = EventBus::Event.new(event_name, body, schemaVersion)
|
34
|
+
|
35
|
+
p 'Sending messsages.'
|
36
|
+
|
37
|
+
EventBus::Emitter.trigger(event)
|
38
|
+
EventBus::Emitter.trigger(events)
|
39
|
+
|
40
|
+
p 'Mesages sent!'
|
41
|
+
|
42
|
+
EventBus::Config.broker.close_connection
|
@@ -0,0 +1,30 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
ENV['RABBIT_URL'] = 'amqp://guest:guest@localhost:5672'
|
4
|
+
ENV['RABBIT_EVENT_BUS_APP_NAME'] = 'EventBusExampleApp'
|
5
|
+
ENV['RABBIT_EVENT_BUS_VHOST'] = 'event_bus'
|
6
|
+
ENV['RABBIT_EVENT_BUS_TOPIC_NAME'] = 'event_bus'
|
7
|
+
|
8
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
9
|
+
|
10
|
+
require 'rubygems'
|
11
|
+
require 'bundler'
|
12
|
+
Bundler.require(:default, 'development')
|
13
|
+
|
14
|
+
require 'event_bus'
|
15
|
+
|
16
|
+
|
17
|
+
event_name = 'resource.origin.action'
|
18
|
+
|
19
|
+
puts 'Start receiving messages'
|
20
|
+
|
21
|
+
EventBus::Listener.on(event_name) do |event|
|
22
|
+
puts ""
|
23
|
+
puts " - Received a message from #{event.name}:"
|
24
|
+
puts " Message: #{event.body}"
|
25
|
+
puts ""
|
26
|
+
end
|
27
|
+
|
28
|
+
puts 'Stop receiving messages'
|
29
|
+
|
30
|
+
EventBus::Config.broker.close_connection
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module EventBus
|
2
|
+
module Broker
|
3
|
+
class Base
|
4
|
+
@@connection = nil
|
5
|
+
|
6
|
+
def self.consumers
|
7
|
+
@consumers ||= {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def connection
|
11
|
+
raise NotImplementedError.new('Must be implemented')
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.close_connection
|
15
|
+
raise NotImplementedError.new('Must be implemented')
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.consume(event_name, &block)
|
19
|
+
consumers[event_name] ||= begin
|
20
|
+
new.consume(event_name, &block)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def consume(event_name, &block)
|
25
|
+
raise NotImplementedError.new('Must be implemented')
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.produce(events)
|
29
|
+
new.produce(events)
|
30
|
+
end
|
31
|
+
|
32
|
+
def produce(events)
|
33
|
+
raise NotImplementedError.new('Must be implemented')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module EventBus
|
2
|
+
module Broker
|
3
|
+
class Rabbit < Base
|
4
|
+
def connection
|
5
|
+
@@connection ||= session
|
6
|
+
end
|
7
|
+
|
8
|
+
def consume(event_name, &block)
|
9
|
+
Queue.subscribe(channel, event_name, &block)
|
10
|
+
end
|
11
|
+
|
12
|
+
def produce(events)
|
13
|
+
events.each do |event|
|
14
|
+
Topic.produce(channel, event)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.close_connection
|
19
|
+
@@connection.close
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def channel
|
25
|
+
connection.create_channel
|
26
|
+
end
|
27
|
+
|
28
|
+
def session
|
29
|
+
Bunny.new(url).tap do |session|
|
30
|
+
session.start
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def url
|
35
|
+
EventBus::Config::FULL_URL
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module EventBus
|
2
|
+
module Broker
|
3
|
+
class Rabbit::Queue
|
4
|
+
def initialize(connection)
|
5
|
+
@channel = connection
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.subscribe(connection, routing_key, &block)
|
9
|
+
new(connection).subscribe(routing_key, &block)
|
10
|
+
end
|
11
|
+
|
12
|
+
def subscribe(routing_key, &block)
|
13
|
+
name = queue_name(routing_key)
|
14
|
+
|
15
|
+
channel.queue(name, queue_options)
|
16
|
+
.bind(topic, routing_key: routing_key)
|
17
|
+
.subscribe do |delivery_info, properties, payload|
|
18
|
+
callback(delivery_info, properties, payload, &block)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
attr_reader :channel
|
25
|
+
|
26
|
+
def callback(delivery_info, properties, payload, &block)
|
27
|
+
event_name = delivery_info.routing_key
|
28
|
+
|
29
|
+
event = EventBus::Event.new(event_name, payload)
|
30
|
+
|
31
|
+
block.call(event)
|
32
|
+
end
|
33
|
+
|
34
|
+
def topic
|
35
|
+
Rabbit::Topic.topic(channel)
|
36
|
+
end
|
37
|
+
|
38
|
+
def queue_options
|
39
|
+
{ durable: true }
|
40
|
+
end
|
41
|
+
|
42
|
+
def queue_name(routing_key)
|
43
|
+
"#{EventBus::Config::APP_NAME.downcase}-#{routing_key.downcase}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module EventBus
|
2
|
+
module Broker
|
3
|
+
class Rabbit::Topic
|
4
|
+
def initialize(connection)
|
5
|
+
@channel = connection
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.topic(connection)
|
9
|
+
new(connection).topic
|
10
|
+
end
|
11
|
+
|
12
|
+
def topic
|
13
|
+
@topic ||= channel.topic(EventBus::Config::TOPIC, topic_options)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.produce(connection, event)
|
17
|
+
new(connection).produce(event)
|
18
|
+
end
|
19
|
+
|
20
|
+
def produce(event)
|
21
|
+
topic.publish(event.payload, routing_key: event.name)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
attr_reader :channel
|
27
|
+
|
28
|
+
def topic_options
|
29
|
+
{ durable: true, auto_delete: false }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module EventBus
|
2
|
+
class Config
|
3
|
+
APP_NAME = ENV['RABBIT_EVENT_BUS_APP_NAME']
|
4
|
+
TOPIC = ENV['RABBIT_EVENT_BUS_TOPIC_NAME']
|
5
|
+
VHOST = ENV['RABBIT_EVENT_BUS_VHOST']
|
6
|
+
URL = ENV['RABBIT_URL']
|
7
|
+
FULL_URL = "#{ENV['RABBIT_URL']}/#{ENV['RABBIT_EVENT_BUS_VHOST']}"
|
8
|
+
|
9
|
+
def self.broker
|
10
|
+
EventBus::Broker::Rabbit
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module EventBus
|
2
|
+
class Daemon
|
3
|
+
INTERRUPTION_SIGNALS = %w(TERM INT)
|
4
|
+
|
5
|
+
def self.start
|
6
|
+
Listeners::Manager.bind_all_listeners
|
7
|
+
|
8
|
+
bind_signals
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.stop
|
12
|
+
EventBus::Config.broker.close_connection
|
13
|
+
|
14
|
+
exit
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.bind_signals
|
18
|
+
read, write = IO.pipe
|
19
|
+
|
20
|
+
INTERRUPTION_SIGNALS.each do |signal|
|
21
|
+
Signal.trap(signal) { write.puts(signal) }
|
22
|
+
end
|
23
|
+
|
24
|
+
begin
|
25
|
+
while io = IO.select([read])
|
26
|
+
signal = io.first[0].gets.strip
|
27
|
+
raise Interrupt if INTERRUPTION_SIGNALS.include?(signal)
|
28
|
+
end
|
29
|
+
rescue Interrupt
|
30
|
+
stop
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module EventBus
|
4
|
+
# Public: An class which decorates el-rabbit topic creation.
|
5
|
+
#
|
6
|
+
class Emitter
|
7
|
+
# Public: Produces a event on the Broker.
|
8
|
+
#
|
9
|
+
# event - The event to be produced.
|
10
|
+
#
|
11
|
+
# Returns the Event.
|
12
|
+
def self.trigger(*events)
|
13
|
+
events.flatten.each_with_index do |event, index|
|
14
|
+
raise MissingAttributeError.new("Event on position #{index} must have a body") unless event.has_body?
|
15
|
+
raise MissingAttributeError.new("Event on position #{index} must have a name") unless event.has_name?
|
16
|
+
end
|
17
|
+
|
18
|
+
EventBus::Config.broker.produce(events.flatten)
|
19
|
+
|
20
|
+
events
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module EventBus
|
2
|
+
class Event
|
3
|
+
attr_reader :name, :headers, :body, :schema_version
|
4
|
+
|
5
|
+
def initialize(name, body, schema_version = 1.0)
|
6
|
+
@name = name
|
7
|
+
@body = JSON.parse(body) rescue body
|
8
|
+
@schema_version = @body['headers']['schemaVersion'] rescue schema_version
|
9
|
+
generate_headers if has_name?
|
10
|
+
build_payload if @body && @body.has_key?('headers')
|
11
|
+
end
|
12
|
+
|
13
|
+
def payload
|
14
|
+
{
|
15
|
+
headers: headers,
|
16
|
+
body: body
|
17
|
+
}.to_json
|
18
|
+
end
|
19
|
+
|
20
|
+
def has_body?
|
21
|
+
body && !body.empty?
|
22
|
+
end
|
23
|
+
|
24
|
+
def has_name?
|
25
|
+
name && !name.empty?
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def build_payload
|
31
|
+
@headers = body['headers']
|
32
|
+
@body = body['body']
|
33
|
+
end
|
34
|
+
|
35
|
+
def generate_headers
|
36
|
+
header_spec = name.split('.')
|
37
|
+
|
38
|
+
@headers = {
|
39
|
+
appName: EventBus::Config::APP_NAME,
|
40
|
+
resource: header_spec[0],
|
41
|
+
origin: header_spec[1],
|
42
|
+
action: header_spec[2],
|
43
|
+
schemaVersion: schema_version
|
44
|
+
}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'bunny'
|
2
|
+
|
3
|
+
module EventBus
|
4
|
+
class Listener
|
5
|
+
def self.on(event_name, &block)
|
6
|
+
new.on(event_name, &block)
|
7
|
+
end
|
8
|
+
|
9
|
+
def on(event_name, &block)
|
10
|
+
raise MissingAttributeError.new('Event name must be present') unless event_name && event_name.size > 0
|
11
|
+
|
12
|
+
EventBus::Config.broker.consume(event_name, &block)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# Module responsible for managing all listeners
|
2
|
+
module EventBus
|
3
|
+
module Listeners
|
4
|
+
module Manager
|
5
|
+
class << self
|
6
|
+
def bind_all_listeners
|
7
|
+
listener_configurations.each do |config|
|
8
|
+
EventBus::Listener.on(config[:routing_key]) do |event|
|
9
|
+
config[:listener_class].new.send(config[:method], event)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def register_listener_configuration(configuration)
|
15
|
+
listener_configurations.push(configuration)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def listener_configurations
|
21
|
+
@listener_configurations ||= []
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/event_bus_rb.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'dotenv'
|
2
|
+
|
3
|
+
root = Dir.pwd
|
4
|
+
Dotenv.load("#{root}/.env.local", "#{root}/.env.#{ENV['RACK_ENV']}", "#{root}/.env")
|
5
|
+
|
6
|
+
require 'event_bus/version'
|
7
|
+
require 'event_bus/daemon'
|
8
|
+
require 'event_bus/event'
|
9
|
+
require 'event_bus/config'
|
10
|
+
require 'event_bus/emitter'
|
11
|
+
require 'event_bus/listener'
|
12
|
+
require 'event_bus/listeners/base'
|
13
|
+
require 'event_bus/listeners/manager'
|
14
|
+
require 'event_bus/broker/base'
|
15
|
+
require 'event_bus/broker/rabbit'
|
16
|
+
require 'event_bus/broker/rabbit/queue'
|
17
|
+
require 'event_bus/broker/rabbit/topic'
|
18
|
+
|
19
|
+
module EventBus
|
20
|
+
class MissingAttributeError < StandardError; end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: event_bus_rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Emprego Ligado
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-06-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry-meta
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: dotenv
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: bunny
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '2.7'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2.7'
|
97
|
+
description: Tool to produce and consume events to a distributed and async architecture
|
98
|
+
email:
|
99
|
+
- vagas@empregoligado.com.br
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".env.example"
|
105
|
+
- ".gitignore"
|
106
|
+
- ".pronto.yml"
|
107
|
+
- ".rspec"
|
108
|
+
- ".rubocop.yml"
|
109
|
+
- ".ruby-version"
|
110
|
+
- Gemfile
|
111
|
+
- Gemfile.lock
|
112
|
+
- LICENSE.txt
|
113
|
+
- README.md
|
114
|
+
- Rakefile
|
115
|
+
- bin/cisetup
|
116
|
+
- bin/console
|
117
|
+
- bin/rspec
|
118
|
+
- bin/setup
|
119
|
+
- circle.yml
|
120
|
+
- event_bus_rb.gemspec
|
121
|
+
- examples/daemon.rb
|
122
|
+
- examples/emitter.rb
|
123
|
+
- examples/listener.rb
|
124
|
+
- lib/event_bus/broker/base.rb
|
125
|
+
- lib/event_bus/broker/rabbit.rb
|
126
|
+
- lib/event_bus/broker/rabbit/queue.rb
|
127
|
+
- lib/event_bus/broker/rabbit/topic.rb
|
128
|
+
- lib/event_bus/config.rb
|
129
|
+
- lib/event_bus/daemon.rb
|
130
|
+
- lib/event_bus/emitter.rb
|
131
|
+
- lib/event_bus/event.rb
|
132
|
+
- lib/event_bus/listener.rb
|
133
|
+
- lib/event_bus/listeners/base.rb
|
134
|
+
- lib/event_bus/listeners/manager.rb
|
135
|
+
- lib/event_bus/version.rb
|
136
|
+
- lib/event_bus_rb.rb
|
137
|
+
homepage: http://gems.empregoligado.com.br
|
138
|
+
licenses:
|
139
|
+
- MIT
|
140
|
+
metadata:
|
141
|
+
allowed_push_host: https://rubygems.org
|
142
|
+
post_install_message:
|
143
|
+
rdoc_options: []
|
144
|
+
require_paths:
|
145
|
+
- lib
|
146
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
|
+
requirements:
|
153
|
+
- - ">="
|
154
|
+
- !ruby/object:Gem::Version
|
155
|
+
version: '0'
|
156
|
+
requirements: []
|
157
|
+
rubyforge_project:
|
158
|
+
rubygems_version: 2.6.12
|
159
|
+
signing_key:
|
160
|
+
specification_version: 4
|
161
|
+
summary: Expose an api to produce and consume events.
|
162
|
+
test_files: []
|