reactor 0.10.1 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Changes.md +13 -0
- data/Gemfile.lock +1 -1
- data/lib/reactor/models/concerns/subscribable.rb +39 -18
- data/lib/reactor/version.rb +1 -1
- data/spec/event_spec.rb +9 -11
- data/spec/models/concerns/subscribable_spec.rb +9 -7
- data/spec/reactor_spec.rb +7 -7
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e47dd61c52db1bfaa846f2fbb50876b081a1c60b
|
4
|
+
data.tar.gz: 917e730109e0f193c63706ba55025d2ebb877ee3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 10185cdd733e3d93292dc9a94f48a35a100411f030dfa4e7d47f687fe794cb659e77d57ae0ad287daa367b203fe6230621430c8292e8aa3abf933cbee8856c00
|
7
|
+
data.tar.gz: 6687577b3635233ba535a8ada13ab83446c6a90a2273d44157717ac48d9faceafb161913ca6cab4fac56fce158c2996f8f9d67c0f37972c69967b66d993607ca
|
data/Changes.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Reactor Change Log
|
2
|
+
|
3
|
+
0.11.0
|
4
|
+
-----------
|
5
|
+
Static Subscriber class names have changed to be more deterministic. THIS _MAY BE_ A BREAKING CHANGE.
|
6
|
+
See https://github.com/hired/reactor/issues/40 for background info.
|
7
|
+
|
8
|
+
Previously, when you added an `on_event :foo` block to an object, say `MyObject`, Reactor would dynamically generate a Sidekiq worker class named `Reactor::StaticSubscribers::FooHandler0`.
|
9
|
+
|
10
|
+
In 0.11.0, the naming of this dynamically generated worker class has changed to `Reactor::StaticSubscribers::MyObject::FooHandler`.
|
11
|
+
If you require more than one `on_event` block for the same event, you must name the handler so that it is unique and deterministic: i.e. `on_event :foo, handler_name: :do_better` otherwise an exception will be raised at load time.
|
12
|
+
|
13
|
+
Because the worker class names are changing, when deploying this change it's important that your Sidekiq queue not have any pending jobs with the old naming scheme, otherwise they will fail to deserialize!
|
data/Gemfile.lock
CHANGED
@@ -1,25 +1,38 @@
|
|
1
1
|
module Reactor::Subscribable
|
2
2
|
extend ActiveSupport::Concern
|
3
3
|
|
4
|
+
class EventHandlerAlreadyDefined < StandardError ; end
|
5
|
+
|
4
6
|
module ClassMethods
|
5
7
|
def on_event(*args, &block)
|
6
8
|
options = args.extract_options!
|
7
9
|
event, method = args
|
8
|
-
(Reactor::SUBSCRIBERS[event.to_s] ||= []).push(
|
10
|
+
(Reactor::SUBSCRIBERS[event.to_s] ||= []).push(create_static_subscriber(event, method, options, &block))
|
9
11
|
end
|
10
|
-
end
|
11
12
|
|
12
|
-
|
13
|
+
private
|
14
|
+
|
15
|
+
def create_static_subscriber(event, method = nil, options = {}, &block)
|
16
|
+
worker_class = build_worker_class
|
17
|
+
|
18
|
+
name_worker_class worker_class, handler_name(event, options[:handler_name])
|
19
|
+
|
20
|
+
worker_class.tap do |k|
|
21
|
+
k.source = self
|
22
|
+
k.method = method || block
|
23
|
+
k.delay = options[:delay] || 0
|
24
|
+
k.in_memory = options[:in_memory]
|
25
|
+
k.dont_perform = Reactor.test_mode?
|
26
|
+
end
|
27
|
+
end
|
13
28
|
|
14
|
-
def
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
new_class = "#{handler_class_prefix}Handler#{i}"
|
19
|
-
i+= 1
|
20
|
-
end while Reactor::StaticSubscribers.const_defined?(new_class)
|
29
|
+
def handler_name(event, handler_name_option = nil)
|
30
|
+
return handler_name_option.to_s.camelize if handler_name_option
|
31
|
+
"#{event == '*' ? 'Wildcard': event.to_s.camelize}Handler"
|
32
|
+
end
|
21
33
|
|
22
|
-
|
34
|
+
def build_worker_class
|
35
|
+
Class.new do
|
23
36
|
include Sidekiq::Worker
|
24
37
|
|
25
38
|
class_attribute :method, :delay, :source, :in_memory, :dont_perform
|
@@ -42,16 +55,24 @@ module Reactor::Subscribable
|
|
42
55
|
end
|
43
56
|
end
|
44
57
|
end
|
58
|
+
end
|
45
59
|
|
46
|
-
|
60
|
+
def name_worker_class(klass, handler_name)
|
61
|
+
if static_subscriber_namespace.const_defined?(handler_name)
|
62
|
+
raise EventHandlerAlreadyDefined.new(
|
63
|
+
"A Reactor event named #{handler_name} already has been defined on #{static_subscriber_namespace}.
|
64
|
+
Specify a `:handler_name` option on your subscriber's `on_event` declaration to name this event handler deterministically."
|
65
|
+
)
|
66
|
+
end
|
67
|
+
static_subscriber_namespace.const_set(handler_name, klass)
|
68
|
+
end
|
47
69
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
k.source = options[:source]
|
52
|
-
k.in_memory = options[:in_memory]
|
53
|
-
k.dont_perform = Reactor.test_mode?
|
70
|
+
def static_subscriber_namespace
|
71
|
+
unless Reactor::StaticSubscribers.const_defined?(self.name, false)
|
72
|
+
Reactor::StaticSubscribers.const_set(self.name, Module.new)
|
54
73
|
end
|
74
|
+
|
75
|
+
Reactor::StaticSubscribers.const_get(self.name, false)
|
55
76
|
end
|
56
77
|
end
|
57
78
|
end
|
data/lib/reactor/version.rb
CHANGED
data/spec/event_spec.rb
CHANGED
@@ -9,6 +9,15 @@ module MyModule
|
|
9
9
|
end
|
10
10
|
|
11
11
|
class ArbitraryModel < ActiveRecord::Base
|
12
|
+
|
13
|
+
on_event :barfed, handler_name: :bad do
|
14
|
+
raise 'UNEXPECTED!'
|
15
|
+
end
|
16
|
+
|
17
|
+
on_event :barfed do
|
18
|
+
'that was gross'
|
19
|
+
end
|
20
|
+
|
12
21
|
end
|
13
22
|
|
14
23
|
class OtherWorker
|
@@ -55,19 +64,8 @@ describe Reactor::Event do
|
|
55
64
|
end
|
56
65
|
|
57
66
|
describe 'when subscriber throws exception', :sidekiq do
|
58
|
-
let(:mock) { double(:thing, some_method: 3) }
|
59
67
|
let(:barfing_event) { Reactor::Event.perform('barfed', somethin: 'up', actor_id: model.id.to_s, actor_type: model.class.to_s) }
|
60
68
|
|
61
|
-
before do
|
62
|
-
Reactor::SUBSCRIBERS['barfed'] ||= []
|
63
|
-
Reactor::SUBSCRIBERS['barfed'] << Reactor::Subscribable::StaticSubscriberFactory.create('barfed') do |event|
|
64
|
-
raise 'UNEXPECTED!'
|
65
|
-
end
|
66
|
-
Reactor::SUBSCRIBERS['barfed'] << Reactor::Subscribable::StaticSubscriberFactory.create('barfed') do |event|
|
67
|
-
mock.some_method
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
69
|
it 'doesnt matter because it runs in a separate worker process' do
|
72
70
|
expect { barfing_event }.to_not raise_exception
|
73
71
|
end
|
@@ -6,7 +6,9 @@ class Auction < ActiveRecord::Base
|
|
6
6
|
end
|
7
7
|
|
8
8
|
on_event :puppy_delivered, :ring_bell
|
9
|
-
on_event :puppy_delivered,
|
9
|
+
on_event :puppy_delivered, handler_name: :do_nothing_handler do |event|
|
10
|
+
|
11
|
+
end
|
10
12
|
on_event :any_event, -> (event) { puppies! }
|
11
13
|
on_event :pooped, :pick_up_poop, delay: 5.minutes
|
12
14
|
on_event '*' do |event|
|
@@ -40,8 +42,8 @@ describe Reactor::Subscribable do
|
|
40
42
|
|
41
43
|
describe 'building uniquely named subscriber handler classes' do
|
42
44
|
it 'adds a static subscriber to the global lookup constant' do
|
43
|
-
expect(Reactor::SUBSCRIBERS['puppy_delivered'][0]).to eq(Reactor::StaticSubscribers::
|
44
|
-
expect(Reactor::SUBSCRIBERS['puppy_delivered'][1]).to eq(Reactor::StaticSubscribers::
|
45
|
+
expect(Reactor::SUBSCRIBERS['puppy_delivered'][0]).to eq(Reactor::StaticSubscribers::Auction::PuppyDeliveredHandler)
|
46
|
+
expect(Reactor::SUBSCRIBERS['puppy_delivered'][1]).to eq(Reactor::StaticSubscribers::Auction::DoNothingHandler)
|
45
47
|
end
|
46
48
|
end
|
47
49
|
|
@@ -71,25 +73,25 @@ describe Reactor::Subscribable do
|
|
71
73
|
describe 'in_memory flag' do
|
72
74
|
it 'doesnt fire perform_async when true' do
|
73
75
|
expect(Auction).to receive(:puppies!)
|
74
|
-
expect(Reactor::StaticSubscribers::
|
76
|
+
expect(Reactor::StaticSubscribers::Auction::CatDeliveredHandler).not_to receive(:perform_async)
|
75
77
|
Reactor::Event.publish(:cat_delivered)
|
76
78
|
end
|
77
79
|
|
78
80
|
it 'fires perform_async when falsey' do
|
79
|
-
expect(Reactor::StaticSubscribers::
|
81
|
+
expect(Reactor::StaticSubscribers::Auction::WildcardHandler).to receive(:perform_async)
|
80
82
|
Reactor::Event.publish(:puppy_delivered)
|
81
83
|
end
|
82
84
|
end
|
83
85
|
|
84
86
|
describe '#perform' do
|
85
87
|
it 'returns :__perform_aborted__ when Reactor is in test mode' do
|
86
|
-
expect(Reactor::StaticSubscribers::
|
88
|
+
expect(Reactor::StaticSubscribers::TestModeAuction::TestPuppyDeliveredHandler.new.perform({})).to eq(:__perform_aborted__)
|
87
89
|
Reactor::Event.publish(:test_puppy_delivered)
|
88
90
|
end
|
89
91
|
|
90
92
|
it 'performs normally when specifically enabled' do
|
91
93
|
Reactor.enable_test_mode_subscriber(TestModeAuction)
|
92
|
-
expect(Reactor::StaticSubscribers::
|
94
|
+
expect(Reactor::StaticSubscribers::TestModeAuction::TestPuppyDeliveredHandler.new.perform({})).not_to eq(:__perform_aborted__)
|
93
95
|
Reactor::Event.publish(:test_puppy_delivered)
|
94
96
|
end
|
95
97
|
end
|
data/spec/reactor_spec.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
Reactor.in_test_mode do
|
4
|
+
class SomeClass
|
5
|
+
include Reactor::Subscribable
|
6
|
+
on_event :test_event, -> (event) { self.spy_on_me }
|
7
|
+
end
|
8
|
+
end
|
3
9
|
|
4
10
|
describe Reactor do
|
5
|
-
let(:subscriber)
|
6
|
-
Reactor.in_test_mode do
|
7
|
-
Class.new(ActiveRecord::Base) do
|
8
|
-
on_event :test_event, -> (event) { self.spy_on_me }
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
11
|
+
let(:subscriber) { SomeClass }
|
12
12
|
|
13
13
|
describe '.test_mode!' do
|
14
14
|
it 'sets Reactor into test mode' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reactor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.11.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- winfred
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2016-
|
14
|
+
date: 2016-03-11 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: sidekiq
|
@@ -167,6 +167,7 @@ files:
|
|
167
167
|
- ".rspec"
|
168
168
|
- ".ruby-version"
|
169
169
|
- ".travis.yml"
|
170
|
+
- Changes.md
|
170
171
|
- Gemfile
|
171
172
|
- Gemfile.lock
|
172
173
|
- LICENSE.txt
|