sidekiq-eventbus 0.0.1 → 0.1.0

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
- SHA1:
3
- metadata.gz: d18656702ca14b2606e45d4ac95c2b4b76ddddfe
4
- data.tar.gz: bbddfe1f125ac1d78311396e4a63e3c85a83b978
2
+ SHA256:
3
+ metadata.gz: e246cbcc36a0dd6d362dc9c5d505094ecd66bf11e620334cd80ca3e1b9042b97
4
+ data.tar.gz: ba94de9927238c7d79c018d3c4776f1c7ca35eb0f989a5994c6a2c71d5f3a888
5
5
  SHA512:
6
- metadata.gz: 6b93677fca1044d58c33717d9ab8110c4862f1cbd986d7a61ca5e5af4a5de12c7b18a6f47b38733cff0acad3a69437d793f9059ae4cc72e84da9f07b813a3523
7
- data.tar.gz: 801e04b38f6f341c8a01733cf883bac44206c496cd3fa444b991ff6105aaa47b6fbf99f6231146afe9845b6df9ee1805cb1b91025828e69095d2e0ed8cab3b06
6
+ metadata.gz: c2265fdd50b3c914024bada3ee1012044d6eb2f7aca03c3961940e07e814a01b18717124aa8fabe0d19b48a4d1b6a0e40e4d537778cf062299feb7a85219a7f2
7
+ data.tar.gz: f7ab5f88503302c094bd0616c60db5fd5d9d2b7b63dc7c216239e18ea1c7f920d85b1343a12f25275c93dcd13ee9e26c083429b73f4b3c12b1b3de73cd31e790
File without changes
@@ -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.
@@ -0,0 +1,4 @@
1
+ # sidekiq-eventbus
2
+
3
+ A simple event bus implementation written to offload event handling via Sidekiq.
4
+
@@ -1,2 +1,33 @@
1
- module Sidekiq::EventBus
2
- end
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 :EventWorker, 'sidekiq/event_bus/event_worker'
12
+
13
+ module Adapters
14
+ autoload :Default, 'sidekiq/event_bus/adapters/default'
15
+ autoload :Buffered, 'sidekiq/event_bus/adapters/buffered'
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
@@ -0,0 +1,53 @@
1
+ require 'concurrent'
2
+
3
+ class Sidekiq::EventBus::Adapters::Buffered
4
+ def initialize adapter: Sidekiq::EventBus::Adapters::Default.new
5
+ @adapter = adapter
6
+ @bufferes = Concurrent::Map.new
7
+ end
8
+
9
+ def push event, payload
10
+ if is_buffered?
11
+ event_buffer << [ event, payload ]
12
+ nil
13
+ else
14
+ @adapter.push(event, payload)
15
+ end
16
+ end
17
+
18
+ def buffered
19
+ buffer!
20
+ begin
21
+ yield
22
+ flush!
23
+ ensure
24
+ clear!
25
+ end
26
+ end
27
+
28
+ def event_buffer
29
+ @bufferes[buffer_key] ||= Array.new
30
+ end
31
+
32
+ def is_buffered?
33
+ @bufferes.key?(buffer_key)
34
+ end
35
+
36
+ def buffer!
37
+ @bufferes[buffer_key] ||= Array.new
38
+ end
39
+
40
+ def flush!
41
+ event_buffer.map do |event, payload|
42
+ @adapter.push(event, payload)
43
+ end
44
+ end
45
+
46
+ def clear!
47
+ @bufferes.delete(buffer_key)
48
+ end
49
+
50
+ def buffer_key
51
+ Thread.current.object_id
52
+ end
53
+ end
@@ -0,0 +1,8 @@
1
+ class Sidekiq::EventBus::Adapters::Default
2
+ def push event, payload
3
+ Sidekiq::Client.push({
4
+ 'class' => Sidekiq::EventBus::EventWorker,
5
+ 'args' => [ event, payload ]
6
+ })
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ class Sidekiq::EventBus::Adapters::Inline
2
+ def push event, payload
3
+ Sidekiq::EventBus.utils.handle_event(event, payload)
4
+ SecureRandom.hex(8)
5
+ end
6
+ end
@@ -0,0 +1,7 @@
1
+ class Sidekiq::EventBus::Adapters::Log
2
+ def push event, payload
3
+ id = SecureRandom.hex(8)
4
+ puts "event=#{event} id=#{id} payload=#{payload.to_json}"
5
+ id
6
+ end
7
+ end
@@ -0,0 +1,22 @@
1
+ class Sidekiq::EventBus::Adapters::Test
2
+ attr_accessor :events
3
+
4
+ def initialize
5
+ self.events = []
6
+ end
7
+
8
+ def push event, payload
9
+ id = SecureRandom.hex(8)
10
+ self.events.push({
11
+ event: event,
12
+ payload: payload,
13
+ id: id
14
+ })
15
+
16
+ id
17
+ end
18
+
19
+ def clear!
20
+ self.events.clear
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ class Sidekiq::EventBus::Configuration
2
+ attr_accessor :adapter, :consumers, :sidekiq_worker_options, :error_handler
3
+
4
+ def initialize
5
+ self.adapter = Sidekiq::EventBus::Adapters::Default.new
6
+ self.consumers = Set.new
7
+ self.sidekiq_worker_options = { retry: 0, dead: true }
8
+ end
9
+
10
+ def register_consumer klass
11
+ klass = klass.name unless klass.is_a?(String)
12
+ consumers << klass
13
+ end
14
+ end
@@ -0,0 +1,44 @@
1
+ class Sidekiq::EventBus::Consumer
2
+ def self.register_consumer!
3
+ Sidekiq::EventBus.config.register_consumer(self)
4
+ end
5
+
6
+ # Store handlers for this consumer
7
+ def self.handlers
8
+ @handlers ||= Hash.new { |hash, key| hash[key] = Array.new }
9
+ end
10
+
11
+
12
+ # Register a handler for a particular event
13
+ def self.on event, handler=nil, &block
14
+ if handler
15
+ handlers[event] << handler
16
+ else
17
+ handlers[event] << block
18
+ end
19
+ end
20
+
21
+ PAYLOAD_WITH_INDIFFERENT_ACCESS = defined?(ActiveSupport::HashWithIndifferentAccess)
22
+
23
+ def consume event, payload
24
+ if self.class.handlers.key? event
25
+ _payload = payload.merge('event'.freeze => event).freeze
26
+
27
+ if PAYLOAD_WITH_INDIFFERENT_ACCESS
28
+ _payload = ActiveSupport::HashWithIndifferentAccess.new(_payload)
29
+ end
30
+
31
+ self.class.handlers[event].each do |handler|
32
+ begin
33
+ if handler.is_a? Proc
34
+ instance_exec( _payload.dup, &handler )
35
+ else
36
+ handler.call( _payload.dup )
37
+ end
38
+ rescue => e
39
+ Sidekiq::EventBus.utils.handle_error(e)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,9 @@
1
+ class Sidekiq::EventBus::EventWorker
2
+ include Sidekiq::Worker
3
+
4
+ sidekiq_options Sidekiq::EventBus.config.sidekiq_worker_options
5
+
6
+ def perform(event, payload)
7
+ Sidekiq::EventBus.utils.handle_event(event, payload)
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ class Sidekiq::EventBus::Producer
2
+ attr_reader :adapter
3
+
4
+ def initialize adapter: Sidekiq::EventBus.config.adapter
5
+ @adapter = adapter
6
+ end
7
+
8
+ def publish event, payload
9
+ adapter.push event, payload
10
+ end
11
+ end
@@ -0,0 +1,25 @@
1
+ class Sidekiq::EventBus::Utils
2
+ attr_reader :config
3
+
4
+ def initialize config
5
+ @config = config
6
+ end
7
+
8
+ def consumers
9
+ config.consumers.map do |klass_name|
10
+ klass_name.constantize.new
11
+ end
12
+ end
13
+
14
+ def handle_event event, payload
15
+ consumers.each do |consumer|
16
+ consumer.consume(event, payload)
17
+ end
18
+ end
19
+
20
+ def handle_error exception
21
+ unless config.error_handler.nil?
22
+ config.error_handler.call(exception)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ module Sidekiq
2
+ module EventBus
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
metadata CHANGED
@@ -1,28 +1,102 @@
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.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Phil Monroe
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-21 00:00:00.000000000 Z
12
- dependencies: []
13
- description: A simple Producer/Consumer event bus extension for Sidekiq
11
+ date: 2020-10-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: concurrent-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 1.0.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: sidekiq
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 4.0.0
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '7.0'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 4.0.0
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '7.0'
53
+ - !ruby/object:Gem::Dependency
54
+ name: rspec
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 3.5.0
60
+ - - "<"
61
+ - !ruby/object:Gem::Version
62
+ version: '4.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 3.5.0
70
+ - - "<"
71
+ - !ruby/object:Gem::Version
72
+ version: '4.0'
73
+ description: A simple asynchronous Producer/Consumer event bus extension for Sidekiq
14
74
  email: phil@rakefire.io
15
75
  executables: []
16
76
  extensions: []
17
77
  extra_rdoc_files: []
18
78
  files:
79
+ - CHANGELOG.md
80
+ - MIT-LICENSE
81
+ - README.md
19
82
  - lib/sidekiq-eventbus.rb
20
83
  - lib/sidekiq/event_bus.rb
84
+ - lib/sidekiq/event_bus/adapters/buffered.rb
85
+ - lib/sidekiq/event_bus/adapters/default.rb
86
+ - lib/sidekiq/event_bus/adapters/inline.rb
87
+ - lib/sidekiq/event_bus/adapters/log.rb
88
+ - lib/sidekiq/event_bus/adapters/test.rb
89
+ - lib/sidekiq/event_bus/configuration.rb
90
+ - lib/sidekiq/event_bus/consumer.rb
91
+ - lib/sidekiq/event_bus/event_worker.rb
92
+ - lib/sidekiq/event_bus/producer.rb
93
+ - lib/sidekiq/event_bus/utils.rb
94
+ - lib/sidekiq/event_bus/version.rb
21
95
  homepage: https://github.com/Rakefire/sidekiq-eventbus
22
96
  licenses:
23
97
  - MIT
24
98
  metadata: {}
25
- post_install_message:
99
+ post_install_message:
26
100
  rdoc_options: []
27
101
  require_paths:
28
102
  - lib
@@ -37,9 +111,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
37
111
  - !ruby/object:Gem::Version
38
112
  version: '0'
39
113
  requirements: []
40
- rubyforge_project:
41
- rubygems_version: 2.5.1
42
- signing_key:
114
+ rubygems_version: 3.0.3
115
+ signing_key:
43
116
  specification_version: 4
44
- summary: A simple Producer/Consumer event bus extension for Sidekiq
117
+ summary: Producer/Consumer event bus via Sidekiq
45
118
  test_files: []