sidekiq-eventbus 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +0 -0
- data/MIT-LICENSE +7 -0
- data/README.md +0 -0
- data/lib/sidekiq/event_bus/adapters/default.rb +9 -0
- data/lib/sidekiq/event_bus/adapters/inline.rb +6 -0
- data/lib/sidekiq/event_bus/adapters/log.rb +7 -0
- data/lib/sidekiq/event_bus/adapters/test.rb +23 -0
- data/lib/sidekiq/event_bus/configuration.rb +13 -0
- data/lib/sidekiq/event_bus/consumer.rb +43 -0
- data/lib/sidekiq/event_bus/event_worker.rb +11 -0
- data/lib/sidekiq/event_bus/producer.rb +14 -0
- data/lib/sidekiq/event_bus/topic_middleware.rb +9 -0
- data/lib/sidekiq/event_bus/utils.rb +19 -0
- data/lib/sidekiq/event_bus.rb +38 -1
- metadata +14 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 08e5b4cf0e6ab67f298d005fe238b18504b83b56
|
4
|
+
data.tar.gz: 1c333e9f8a673cd70abae2d3a170ecfe57f29350
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6872239db41e62e1993ce91134adecdba84b6388a52c5ce456ee6e68e12bbd6284418b962c24cfffed9412dde5090ec47b66ef1ad291e7e41db80135a7440724
|
7
|
+
data.tar.gz: db02caf8fb432a9e88f1e9678304b95a7f0da799d599f6ba4e497267b04e7a49a6869ce472352f560d61c77baefddd94251fcd14c8df9bb2228067fcebd814be
|
data/CHANGELOG.md
ADDED
File without changes
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright 2016 Rakefire LLC
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
File without changes
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class Sidekiq::EventBus::Adapters::Test
|
2
|
+
attr_accessor :topics
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
self.topics = Hash.new{ |hash, key| hash[key] = [] }
|
6
|
+
end
|
7
|
+
|
8
|
+
def push topic, event, payload
|
9
|
+
id = SecureRandom.hex(8)
|
10
|
+
self.topics[topic].push({
|
11
|
+
topic: topic,
|
12
|
+
event: event,
|
13
|
+
payload: payload,
|
14
|
+
id: id
|
15
|
+
})
|
16
|
+
|
17
|
+
id
|
18
|
+
end
|
19
|
+
|
20
|
+
def clear!
|
21
|
+
self.topics.clear
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class Sidekiq::EventBus::Configuration
|
2
|
+
attr_accessor :adapter, :consumers, :sidekiq_worker_options
|
3
|
+
def initialize
|
4
|
+
self.adapter = Sidekiq::EventBus::Adapters::Default.new
|
5
|
+
self.consumers = Hash.new { |hash, key| hash[key] = Set.new }
|
6
|
+
self.sidekiq_worker_options = { retry: 0, dead: true }
|
7
|
+
end
|
8
|
+
|
9
|
+
def register_consumer topic, klass
|
10
|
+
klass = klass.name unless klass.is_a?(String)
|
11
|
+
consumers[topic] << klass
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class Sidekiq::EventBus::Consumer
|
2
|
+
# Register Consumer with the EventBus for a particular topic
|
3
|
+
def self.topic *topics
|
4
|
+
Array(topics).each do |topic|
|
5
|
+
Sidekiq::EventBus.config.register_consumer(topic, self)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
# Store handlers for this consumer
|
11
|
+
def self.handlers
|
12
|
+
@handlers ||= Hash.new { |hash, key| hash[key] = Array.new }
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
# Register a handler for a particular event
|
17
|
+
def self.on event, handler=nil, &block
|
18
|
+
if handler
|
19
|
+
handlers[event] << handler
|
20
|
+
else
|
21
|
+
handlers[event] << block
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
def consume topic, event, payload
|
27
|
+
_payload = payload.merge('topic'.freeze => topic, 'event'.freeze => event).freeze
|
28
|
+
|
29
|
+
self.class.handlers[event].each do |handler|
|
30
|
+
begin
|
31
|
+
if handler.is_a? Proc
|
32
|
+
instance_exec( _payload.dup, &handler )
|
33
|
+
else
|
34
|
+
handler.call( _payload.dup )
|
35
|
+
end
|
36
|
+
rescue => e
|
37
|
+
puts "OH NO!!!"
|
38
|
+
puts e.inspect
|
39
|
+
puts e.backtrace
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class Sidekiq::EventBus::EventWorker
|
2
|
+
include Sidekiq::Worker
|
3
|
+
|
4
|
+
attr_accessor :topic
|
5
|
+
|
6
|
+
sidekiq_options Sidekiq::EventBus.config.sidekiq_worker_options
|
7
|
+
|
8
|
+
def perform(event, payload)
|
9
|
+
Sidekiq::EventBus.utils.handle_event(topic, event, payload)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class Sidekiq::EventBus::Producer
|
2
|
+
attr_reader :topics, :adapter
|
3
|
+
|
4
|
+
def initialize topics: nil, adapter: Sidekiq::EventBus.config.adapter
|
5
|
+
@topics = Array(topics)
|
6
|
+
@adapter = adapter
|
7
|
+
end
|
8
|
+
|
9
|
+
def publish event, payload
|
10
|
+
topics.map do |topic|
|
11
|
+
adapter.push topic, event, payload
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class Sidekiq::EventBus::Utils
|
2
|
+
attr_reader :config
|
3
|
+
|
4
|
+
def initialize config
|
5
|
+
@config = config
|
6
|
+
end
|
7
|
+
|
8
|
+
def consumers_for topic
|
9
|
+
config.consumers[topic].map do |klass_name|
|
10
|
+
klass_name.constantize.new
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def handle_event topic, event, payload
|
15
|
+
consumers_for(topic).each do |consumer|
|
16
|
+
consumer.consume(topic, event, payload)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/sidekiq/event_bus.rb
CHANGED
@@ -1,2 +1,39 @@
|
|
1
|
-
|
1
|
+
require 'sidekiq'
|
2
|
+
|
3
|
+
module Sidekiq
|
4
|
+
module EventBus
|
5
|
+
autoload :Configuration, 'sidekiq/event_bus/configuration'
|
6
|
+
autoload :Utils, 'sidekiq/event_bus/utils'
|
7
|
+
|
8
|
+
autoload :Consumer, 'sidekiq/event_bus/consumer'
|
9
|
+
autoload :Producer, 'sidekiq/event_bus/producer'
|
10
|
+
|
11
|
+
autoload :TopicMiddleware, 'sidekiq/event_bus/topic_middleware'
|
12
|
+
autoload :EventWorker, 'sidekiq/event_bus/event_worker'
|
13
|
+
|
14
|
+
module Adapters
|
15
|
+
autoload :Default, 'sidekiq/event_bus/adapters/default'
|
16
|
+
autoload :Inline, 'sidekiq/event_bus/adapters/inline'
|
17
|
+
autoload :Test, 'sidekiq/event_bus/adapters/test'
|
18
|
+
autoload :Log, 'sidekiq/event_bus/adapters/log'
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.config
|
22
|
+
@config ||= Configuration.new
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.utils
|
26
|
+
@utils ||= Utils.new(config)
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.configure
|
30
|
+
yield(config) if block_given?
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
Sidekiq.configure_server do |config|
|
36
|
+
config.server_middleware do |chain|
|
37
|
+
chain.add Sidekiq::EventBus::TopicMiddleware
|
38
|
+
end
|
2
39
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sidekiq-eventbus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Phil Monroe
|
@@ -16,8 +16,21 @@ executables: []
|
|
16
16
|
extensions: []
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
|
+
- CHANGELOG.md
|
20
|
+
- MIT-LICENSE
|
21
|
+
- README.md
|
19
22
|
- lib/sidekiq-eventbus.rb
|
20
23
|
- lib/sidekiq/event_bus.rb
|
24
|
+
- lib/sidekiq/event_bus/adapters/default.rb
|
25
|
+
- lib/sidekiq/event_bus/adapters/inline.rb
|
26
|
+
- lib/sidekiq/event_bus/adapters/log.rb
|
27
|
+
- lib/sidekiq/event_bus/adapters/test.rb
|
28
|
+
- lib/sidekiq/event_bus/configuration.rb
|
29
|
+
- lib/sidekiq/event_bus/consumer.rb
|
30
|
+
- lib/sidekiq/event_bus/event_worker.rb
|
31
|
+
- lib/sidekiq/event_bus/producer.rb
|
32
|
+
- lib/sidekiq/event_bus/topic_middleware.rb
|
33
|
+
- lib/sidekiq/event_bus/utils.rb
|
21
34
|
homepage: https://github.com/Rakefire/sidekiq-eventbus
|
22
35
|
licenses:
|
23
36
|
- MIT
|