reactor 0.3.2 → 0.4.0
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 +43 -45
- data/lib/reactor/models/concerns/subscribable.rb +3 -8
- data/lib/reactor/version.rb +1 -1
- data/spec/event_spec.rb +8 -2
- data/spec/models/concerns/subscribable_spec.rb +13 -3
- data/spec/models/subscriber_spec.rb +1 -1
- 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: 973132a3d45a07bd6de4a4563d543ac7531863a1
|
4
|
+
data.tar.gz: c25c41eacaa7e20a94e36c075218091ef27718e8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ac882931bf8e8d07851abf37bfd6c8847b2d3472759a9c733d7548f84fbdb759a867de17c73532550ec9238d6daca4c75e90946c95b38992c8d9db56d387ab5
|
7
|
+
data.tar.gz: 2e5c28ea31c91e8c9ce3fce8f41403a3e6ea59b526b00d516deb1ddff0ae2432f7ff0467ce876bf51d185049622cb1c4cdb5c3be068aeb6713836a2b3c2fdd89
|
data/lib/reactor/event.rb
CHANGED
@@ -22,65 +22,63 @@ class Reactor::Event
|
|
22
22
|
name
|
23
23
|
end
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
delay.process name, message.data
|
35
|
-
elsif message.at.future?
|
36
|
-
delay_until(message.at).process name, message.data
|
25
|
+
class << self
|
26
|
+
def publish(name, data = {})
|
27
|
+
message = new(data.merge(event: name))
|
28
|
+
|
29
|
+
if message.at.nil?
|
30
|
+
delay.process name, message.data
|
31
|
+
elsif message.at.future?
|
32
|
+
delay_until(message.at).process name, message.data
|
33
|
+
end
|
37
34
|
end
|
38
|
-
end
|
39
35
|
|
40
|
-
|
41
|
-
|
42
|
-
|
36
|
+
def reschedule(name, data = {})
|
37
|
+
job = scheduled_jobs.detect do |job|
|
38
|
+
job['class'] == name.to_s.camelize && job['at'].to_i == data[:was].to_i
|
39
|
+
end
|
40
|
+
remove_scheduled_job job if job
|
41
|
+
delay.publish(name, data.except(:was)) if data[:at].future?
|
43
42
|
end
|
44
|
-
remove_scheduled_job job if job
|
45
|
-
delay.publish(name, data.except(:was)) if data[:at].future?
|
46
|
-
end
|
47
43
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
44
|
+
def process(name, data)
|
45
|
+
data.merge!(fired_at: Time.current)
|
46
|
+
Reactor::Subscriber.where(event: name.to_s).each do |subscriber|
|
47
|
+
Reactor::Subscriber.delay.fire subscriber.id, data
|
48
|
+
end
|
53
49
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
50
|
+
#TODO: support more matching?
|
51
|
+
Reactor::Subscriber.where(event: '*').each do |s|
|
52
|
+
Reactor::Subscriber.delay.fire s.id, data
|
53
|
+
end
|
58
54
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
55
|
+
if (static_subscribers = Reactor::SUBSCRIBERS[name.to_s] || []).any?
|
56
|
+
static_subscribers.each do |callback|
|
57
|
+
delay = callback[:options].try(:[], :delay) || 0
|
58
|
+
case method = callback[:method]
|
59
|
+
when Symbol
|
60
|
+
callback[:source].delay_for(delay).send method, Reactor::Event.new(data.merge(event: name.to_s))
|
61
|
+
else
|
62
|
+
method.call Reactor::Event.new(data.merge(event: name.to_s))
|
63
|
+
end
|
66
64
|
end
|
67
65
|
end
|
68
66
|
end
|
69
|
-
end
|
70
67
|
|
71
|
-
|
68
|
+
def scheduled_jobs(options = {})
|
69
|
+
Sidekiq.redis do |r|
|
70
|
+
from = options[:from] ? options[:from].to_f.to_s : '-inf'
|
71
|
+
to = options[:to] ? options[:to].to_f.to_s : '+inf'
|
72
|
+
r.zrangebyscore('schedule', from, to).map{|job| MultiJson.decode(job)}
|
73
|
+
end
|
74
|
+
end
|
72
75
|
|
73
|
-
|
74
|
-
|
75
|
-
from = options[:from] ? options[:from].to_f.to_s : '-inf'
|
76
|
-
to = options[:to] ? options[:to].to_f.to_s : '+inf'
|
77
|
-
r.zrangebyscore('schedule', from, to).map{|job| MultiJson.decode(job)}
|
76
|
+
def remove_scheduled_job(job)
|
77
|
+
Sidekiq.redis { |r| r.zrem 'schedule', MultiJson.encode(job) }
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
81
|
-
|
82
|
-
Sidekiq.redis { |r| r.zrem 'schedule', MultiJson.encode(job) }
|
83
|
-
end
|
81
|
+
private
|
84
82
|
|
85
83
|
def try_setter(method, object, *args)
|
86
84
|
if object.is_a? ActiveRecord::Base
|
@@ -2,14 +2,9 @@ module Reactor::Subscribable
|
|
2
2
|
extend ActiveSupport::Concern
|
3
3
|
|
4
4
|
module ClassMethods
|
5
|
-
def on_event(event, method = nil, &block)
|
6
|
-
callback =
|
7
|
-
|
8
|
-
{self => method}
|
9
|
-
else
|
10
|
-
method
|
11
|
-
end
|
12
|
-
callback = block if block
|
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
|
13
8
|
(Reactor::SUBSCRIBERS[event.to_s] ||= []).push(callback)
|
14
9
|
end
|
15
10
|
end
|
data/lib/reactor/version.rb
CHANGED
data/spec/event_spec.rb
CHANGED
@@ -24,9 +24,15 @@ describe Reactor::Event do
|
|
24
24
|
end
|
25
25
|
|
26
26
|
describe 'process' do
|
27
|
+
before { Reactor::Subscriber.create(event: :user_did_this) }
|
28
|
+
after { Reactor::Subscriber.destroy_all }
|
27
29
|
it 'fires all subscribers' do
|
28
|
-
Reactor::Subscriber.
|
29
|
-
Reactor::
|
30
|
+
Reactor::Subscriber.any_instance.should_receive(:fire).with(hash_including(actor_id: '1'))
|
31
|
+
Reactor::Event.process(event_name, actor_id: '1')
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'sets a fired_at key in event data' do
|
35
|
+
Reactor::Subscriber.any_instance.should_receive(:fire).with(hash_including(fired_at: anything))
|
30
36
|
Reactor::Event.process(event_name, actor_id: '1')
|
31
37
|
end
|
32
38
|
end
|
@@ -8,6 +8,7 @@ class Auction < ActiveRecord::Base
|
|
8
8
|
|
9
9
|
on_event :puppy_delivered, :ring_bell
|
10
10
|
on_event :any_event, -> (event) { puppies! }
|
11
|
+
on_event :pooped, :pick_up_poop, delay: 5.minutes
|
11
12
|
|
12
13
|
def self.ring_bell(event)
|
13
14
|
pp "ring ring! #{event}"
|
@@ -22,9 +23,18 @@ describe Reactor::Subscribable do
|
|
22
23
|
Reactor::Event.publish(:bid_made, target: Auction.create)
|
23
24
|
end
|
24
25
|
|
25
|
-
|
26
|
-
|
27
|
-
|
26
|
+
describe 'binding symbol of class method' do
|
27
|
+
it 'fires on event' do
|
28
|
+
Auction.should_receive(:ring_bell)
|
29
|
+
Reactor::Event.publish(:puppy_delivered)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'can be delayed', :sidekiq do
|
33
|
+
Reactor::Event.process(:pooped, {})
|
34
|
+
job = Reactor::Event.scheduled_jobs(from: 4.minutes.from_now, to: 6.minutes.from_now).last
|
35
|
+
job.should be_present
|
36
|
+
job['args'].last.should include("pick_up_poop")
|
37
|
+
end
|
28
38
|
end
|
29
39
|
|
30
40
|
it 'binds proc' do
|
@@ -27,7 +27,7 @@ describe Reactor::Subscriber do
|
|
27
27
|
describe 'matcher' do
|
28
28
|
it 'can be set to star to bind to all events' do
|
29
29
|
MySubscriber.create!(event: '*')
|
30
|
-
MySubscriber.any_instance.should_receive(:fire).with(
|
30
|
+
MySubscriber.any_instance.should_receive(:fire).with(hash_including('random' => 'data', 'event' => :this_event))
|
31
31
|
Reactor::Event.publish(:this_event, {random: 'data'})
|
32
32
|
end
|
33
33
|
end
|
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.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- winfred
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-07-
|
12
|
+
date: 2013-07-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|