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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 32ae9f0d99cf819318f0bd3611addcc9537548fe
4
- data.tar.gz: 17f2a43e5d16f8fe9d0847d949eba3425a31f72d
3
+ metadata.gz: 92b65b121e054e4657f60f0209b36a03258d4f4d
4
+ data.tar.gz: d36e98b14a7659a97a6ad9c8dc9397b1870baa9a
5
5
  SHA512:
6
- metadata.gz: 2d67d5c63c75311901f4b7e0bbb40c10da97fc65a5245fa06f37bab54b55bf0adb5f4eb86751af6934db16ed3b4319979c80953498943c3414e40d520cb5c40c
7
- data.tar.gz: 5bbe9527be4c56d7c73586b7b22da08799321d611de3bb823c96f4548958d0596aa1b8588c5114a5fc4619ca7cbdc4bc6b74d796c311f2f3886c7efb289c0f29
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
- static_subscribers = (Reactor::SUBSCRIBERS[name.to_s] || []) | (Reactor::SUBSCRIBERS['*'] || [])
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
- callback = {method: (method || block), options: {delay: 0}.merge(options)}
7
- callback.merge!(source: self) if method.is_a? Symbol
8
- (Reactor::SUBSCRIBERS[event.to_s] ||= []).push(callback)
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
@@ -1,3 +1,3 @@
1
1
  module Reactor
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.2"
3
3
  end
data/lib/reactor.rb CHANGED
@@ -7,6 +7,8 @@ require "reactor/event"
7
7
 
8
8
  module Reactor
9
9
  SUBSCRIBERS = {}
10
+ module StaticSubscribers
11
+ end
10
12
  end
11
13
 
12
14
  ActiveRecord::Base.send(:include, Reactor::Publishable)
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', :sidekiq do
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.1
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.3
138
+ rubygems_version: 2.0.0
139
139
  signing_key:
140
140
  specification_version: 4
141
141
  summary: Sidekiq/ActiveRecord pubsub lib