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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d18656702ca14b2606e45d4ac95c2b4b76ddddfe
4
- data.tar.gz: bbddfe1f125ac1d78311396e4a63e3c85a83b978
3
+ metadata.gz: 08e5b4cf0e6ab67f298d005fe238b18504b83b56
4
+ data.tar.gz: 1c333e9f8a673cd70abae2d3a170ecfe57f29350
5
5
  SHA512:
6
- metadata.gz: 6b93677fca1044d58c33717d9ab8110c4862f1cbd986d7a61ca5e5af4a5de12c7b18a6f47b38733cff0acad3a69437d793f9059ae4cc72e84da9f07b813a3523
7
- data.tar.gz: 801e04b38f6f341c8a01733cf883bac44206c496cd3fa444b991ff6105aaa47b6fbf99f6231146afe9845b6df9ee1805cb1b91025828e69095d2e0ed8cab3b06
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,9 @@
1
+ class Sidekiq::EventBus::Adapters::Default
2
+ def push topic, event, payload
3
+ Sidekiq::Client.push({
4
+ 'class' => Sidekiq::EventBus::EventWorker,
5
+ 'args' => [ event, payload ],
6
+ 'queue' => topic,
7
+ })
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ class Sidekiq::EventBus::Adapters::Inline
2
+ def push topic, event, payload
3
+ Sidekiq::EventBus.utils.handle_event(topic, event, payload)
4
+ SecureRandom.hex(8)
5
+ end
6
+ end
@@ -0,0 +1,7 @@
1
+ class Sidekiq::EventBus::Adapters::Log
2
+ def push topic, event, payload
3
+ id = SecureRandom.hex(8)
4
+ puts "topic=#{topic} event=#{event} id=#{id} payload=#{payload.to_json}"
5
+ id
6
+ end
7
+ end
@@ -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,9 @@
1
+ class Sidekiq::EventBus::TopicMiddleware
2
+ def initialize(options=nil)
3
+ end
4
+
5
+ def call(worker, msg, queue)
6
+ worker.topic = queue if worker.respond_to?("topic=")
7
+ yield
8
+ end
9
+ 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
@@ -1,2 +1,39 @@
1
- module Sidekiq::EventBus
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.1
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