reactor 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/reactor/event.rb +1 -12
- data/lib/reactor/models/concerns/subscribable.rb +32 -3
- data/lib/reactor/version.rb +1 -1
- data/lib/reactor.rb +2 -0
- data/spec/event_spec.rb +19 -0
- data/spec/models/concerns/subscribable_spec.rb +3 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 92b65b121e054e4657f60f0209b36a03258d4f4d
|
4
|
+
data.tar.gz: d36e98b14a7659a97a6ad9c8dc9397b1870baa9a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c37a732fa832c8e4970cf3aa0f730504458823476f5f9c0438bce9ec63badc7d89815bd0765ef717024b95d5faa63813b53a80368cb26aef2bda7dfd2901b958
|
7
|
+
data.tar.gz: a769df5f8e42181782789a02bc8b55a6f306eb8ab962913783e45e2570f8995ac481f43050866a4230400664e81fe2a682a32c4860fb1330cf72e0d6d58e370b
|
data/lib/reactor/event.rb
CHANGED
@@ -22,18 +22,7 @@ class Reactor::Event
|
|
22
22
|
Reactor::Subscriber.delay.fire s.id, data
|
23
23
|
end
|
24
24
|
|
25
|
-
|
26
|
-
if static_subscribers.any?
|
27
|
-
static_subscribers.each do |callback|
|
28
|
-
delay = callback[:options].try(:[], :delay) || 0
|
29
|
-
case method = callback[:method]
|
30
|
-
when Symbol
|
31
|
-
callback[:source].delay_for(delay).send method, Reactor::Event.new(data)
|
32
|
-
else
|
33
|
-
method.call Reactor::Event.new(data)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
25
|
+
((Reactor::SUBSCRIBERS[name.to_s] || []) | (Reactor::SUBSCRIBERS['*'] || [])).each {|s| s.perform_async(data) }
|
37
26
|
end
|
38
27
|
|
39
28
|
def method_missing(method, *args)
|
@@ -3,9 +3,38 @@ module Reactor::Subscribable
|
|
3
3
|
|
4
4
|
module ClassMethods
|
5
5
|
def on_event(event, method = nil, options = {}, &block)
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
(Reactor::SUBSCRIBERS[event.to_s] ||= []).push(StaticSubscriberFactory.create event, method, {source: self}.merge(options), &block)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class StaticSubscriberFactory
|
11
|
+
|
12
|
+
def self.create(event, method = nil, options = {}, &block)
|
13
|
+
handler_class_prefix = event == '*' ? 'Wildcard': event.to_s.camelize
|
14
|
+
new_class = "Reactor::StaticSubscribers::#{handler_class_prefix}Handler#{Reactor::SUBSCRIBERS[event.to_s].size}"
|
15
|
+
|
16
|
+
eval %Q{
|
17
|
+
class #{new_class}
|
18
|
+
include Sidekiq::Worker
|
19
|
+
|
20
|
+
cattr_accessor :method, :delay, :source
|
21
|
+
|
22
|
+
def perform(data)
|
23
|
+
event = Reactor::Event.new(data)
|
24
|
+
if @@method.is_a?(Symbol)
|
25
|
+
@@source.delay_for(@@delay).send(@@method, event)
|
26
|
+
else
|
27
|
+
@@method.call(event)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
}
|
32
|
+
|
33
|
+
new_class = new_class.constantize
|
34
|
+
new_class.method = method || block
|
35
|
+
new_class.delay = options[:delay] || 0
|
36
|
+
new_class.source = options[:source]
|
37
|
+
new_class
|
9
38
|
end
|
10
39
|
end
|
11
40
|
end
|
data/lib/reactor/version.rb
CHANGED
data/lib/reactor.rb
CHANGED
data/spec/event_spec.rb
CHANGED
@@ -40,6 +40,25 @@ describe Reactor::Event do
|
|
40
40
|
Reactor::Subscriber.any_instance.should_receive(:fire).with(hash_including(actor_id: '1'))
|
41
41
|
Reactor::Event.perform(event_name, actor_id: '1')
|
42
42
|
end
|
43
|
+
|
44
|
+
describe 'when subscriber throws exception', :sidekiq do
|
45
|
+
let(:mock) { mock(:thing, some_method: 3) }
|
46
|
+
let(:barfing_event) { Reactor::Event.perform('barfed', somethin: 'up') }
|
47
|
+
|
48
|
+
before do
|
49
|
+
Reactor::SUBSCRIBERS['barfed'] ||= []
|
50
|
+
Reactor::SUBSCRIBERS['barfed'] << Reactor::Subscribable::StaticSubscriberFactory.create('barfed') do |event|
|
51
|
+
raise 'UNEXPECTED!'
|
52
|
+
end
|
53
|
+
Reactor::SUBSCRIBERS['barfed'] << Reactor::Subscribable::StaticSubscriberFactory.create('barfed') do |event|
|
54
|
+
mock.some_method
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'doesnt matter because it runs in a separate worker process' do
|
59
|
+
expect { barfing_event }.to_not raise_exception
|
60
|
+
end
|
61
|
+
end
|
43
62
|
end
|
44
63
|
|
45
64
|
describe 'reschedule', :sidekiq do
|
@@ -33,11 +33,10 @@ describe Reactor::Subscribable do
|
|
33
33
|
Reactor::Event.publish(:puppy_delivered)
|
34
34
|
end
|
35
35
|
|
36
|
-
it 'can be delayed'
|
36
|
+
it 'can be delayed' do
|
37
|
+
Auction.should_receive(:pick_up_poop)
|
38
|
+
Auction.should_receive(:delay_for).with(5.minutes).and_return(Auction)
|
37
39
|
Reactor::Event.perform('pooped', {})
|
38
|
-
job = scheduled.detect{|job| job.score > 4.minutes.from_now.to_f && job.score <= 5.minutes.from_now.to_f }
|
39
|
-
job.should be_present
|
40
|
-
job['args'].last.should include("pick_up_poop")
|
41
40
|
end
|
42
41
|
end
|
43
42
|
|
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.5.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- winfred
|
@@ -135,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
135
|
version: '0'
|
136
136
|
requirements: []
|
137
137
|
rubyforge_project:
|
138
|
-
rubygems_version: 2.0.
|
138
|
+
rubygems_version: 2.0.0
|
139
139
|
signing_key:
|
140
140
|
specification_version: 4
|
141
141
|
summary: Sidekiq/ActiveRecord pubsub lib
|