untied-publisher 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.mkd +5 -0
- data/lib/untied-publisher/doorkeeper.rb +6 -0
- data/lib/untied-publisher/event.rb +0 -12
- data/lib/untied-publisher/event_representer.rb +15 -0
- data/lib/untied-publisher/producer.rb +17 -4
- data/lib/untied-publisher/railtie.rb +8 -1
- data/lib/untied-publisher/version.rb +1 -1
- data/lib/untied-publisher.rb +15 -1
- data/spec/producer_spec.rb +6 -4
- metadata +5 -4
data/CHANGELOG.mkd
CHANGED
@@ -58,9 +58,11 @@ module Untied
|
|
58
58
|
# _notify_untied__publisher_observer_for_after_create is created on User's
|
59
59
|
# model. This method is called when the after_create callback is fired.
|
60
60
|
def define_callbacks
|
61
|
+
logger.debug "Untied::Publisher: defining callbacks"
|
61
62
|
observed.each do |(klass, callbacks, options)|
|
62
63
|
ActiveRecord::Callbacks::CALLBACKS.each do |callback|
|
63
64
|
next unless callbacks.include?(callback)
|
65
|
+
logger.debug "Untied::Publisher: seting up callback #{callback} for #{klass}"
|
64
66
|
setup_observer(klass, callback, options)
|
65
67
|
end
|
66
68
|
end
|
@@ -68,6 +70,10 @@ module Untied
|
|
68
70
|
|
69
71
|
protected
|
70
72
|
|
73
|
+
def logger
|
74
|
+
Publisher.config.logger
|
75
|
+
end
|
76
|
+
|
71
77
|
def setup_observer(klass, callback, options={})
|
72
78
|
observer = Untied::Publisher::Observer
|
73
79
|
observer_name = observer.name.underscore.gsub('/', '__')
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
2
|
|
3
|
-
require 'representable/json'
|
4
|
-
|
5
3
|
module Untied
|
6
4
|
class Event
|
7
5
|
attr_accessor :name, :payload, :origin
|
@@ -20,14 +18,4 @@ module Untied
|
|
20
18
|
@origin = @config.delete(:origin)
|
21
19
|
end
|
22
20
|
end
|
23
|
-
|
24
|
-
module EventRepresenter
|
25
|
-
include Representable::JSON
|
26
|
-
|
27
|
-
self.representation_wrap = true
|
28
|
-
|
29
|
-
property :name
|
30
|
-
property :payload
|
31
|
-
property :origin
|
32
|
-
end
|
33
21
|
end
|
@@ -4,6 +4,8 @@ require 'amqp'
|
|
4
4
|
module Untied
|
5
5
|
module Publisher
|
6
6
|
class Producer
|
7
|
+
# Encapsulates both the Channel and Exchange (AMQP).
|
8
|
+
|
7
9
|
def initialize(opts={})
|
8
10
|
@opts = {
|
9
11
|
:service_name => Publisher.config.service_name,
|
@@ -26,21 +28,26 @@ module Untied
|
|
26
28
|
if AMQP.channel || @opts[:channel]
|
27
29
|
Publisher.config.logger.info "Using defined AMQP.channel"
|
28
30
|
@channel = AMQP.channel || @opts[:channel]
|
29
|
-
@exchange = @channel.topic("untied", :auto_delete => true)
|
30
31
|
end
|
31
32
|
end
|
32
33
|
|
34
|
+
# Publish the given event.
|
35
|
+
# event: object which is going to be serialized and sent through the
|
36
|
+
# wire. It should respond to #to_json.
|
33
37
|
def publish(event)
|
34
38
|
safe_publish(event)
|
35
39
|
end
|
36
40
|
|
37
41
|
protected
|
38
42
|
|
43
|
+
# Publishes if message delivering is enabled. Otherwise just warns.
|
39
44
|
def safe_publish(e)
|
40
45
|
if @opts[:deliver_messages]
|
41
|
-
|
42
|
-
|
43
|
-
|
46
|
+
on_exchange do |exchange|
|
47
|
+
exchange.publish(e.to_json, :routing_key => @routing_key) do
|
48
|
+
Publisher.config.logger.info \
|
49
|
+
"Publishing event #{e.inspect} with routing key #{@routing_key}"
|
50
|
+
end
|
44
51
|
end
|
45
52
|
else
|
46
53
|
Publisher.config.logger.info \
|
@@ -49,6 +56,12 @@ module Untied
|
|
49
56
|
end
|
50
57
|
end
|
51
58
|
|
59
|
+
# Creates a new exchange and yields it to the block passed when it's ready
|
60
|
+
def on_exchange(&block)
|
61
|
+
return unless @channel
|
62
|
+
@channel.topic('untied', :auto_delete => true, &block)
|
63
|
+
end
|
64
|
+
|
52
65
|
def check_em_reactor
|
53
66
|
if !defined?(EventMachine) || !EM.reactor_running?
|
54
67
|
raise "In order to use the producer you must be running inside an " + \
|
@@ -8,7 +8,14 @@ module Untied
|
|
8
8
|
config.active_record.observers ||= []
|
9
9
|
ActiveRecord::Base.observers << Untied::Publisher::Observer
|
10
10
|
config.active_record.observers << Untied::Publisher::Observer
|
11
|
-
|
11
|
+
unless File.basename($0) == 'rake'
|
12
|
+
Publisher.config.logger.debug "Untied::Publisher: initializing observer"
|
13
|
+
Untied::Publisher::Observer.instance
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
config.to_prepare do
|
18
|
+
Untied::Publisher.config.doorkeeper.new.define_callbacks
|
12
19
|
end
|
13
20
|
end
|
14
21
|
end
|
data/lib/untied-publisher.rb
CHANGED
@@ -9,13 +9,27 @@ module Untied
|
|
9
9
|
def self.start
|
10
10
|
Thread.abort_on_exception = false
|
11
11
|
|
12
|
-
|
12
|
+
self.run do
|
13
13
|
AMQP.start
|
14
14
|
end
|
15
15
|
end
|
16
|
+
|
17
|
+
def self.run(&block)
|
18
|
+
@block = block
|
19
|
+
if defined?(PhusionPassenger)
|
20
|
+
PhusionPassenger.on_event(:starting_worker_process) do |forked|
|
21
|
+
EM.stop if forked && EM.reactor_running?
|
22
|
+
Thread.new { EM.run { @block.call } }
|
23
|
+
end
|
24
|
+
else
|
25
|
+
AMQP::Utilities::EventLoopHelper.run { @block.call }
|
26
|
+
end
|
27
|
+
end
|
16
28
|
end
|
17
29
|
end
|
18
30
|
|
31
|
+
|
32
|
+
require 'untied-publisher/event_representer'
|
19
33
|
require 'untied-publisher/event'
|
20
34
|
require 'untied-publisher/config'
|
21
35
|
require 'untied-publisher/doorkeeper'
|
data/spec/producer_spec.rb
CHANGED
@@ -31,13 +31,15 @@ module Untied
|
|
31
31
|
end
|
32
32
|
|
33
33
|
context "#publish" do
|
34
|
+
let(:event) do
|
35
|
+
Event.new(:name => "create", :payload => { :foo => 'bar' }, :origin => :core)
|
36
|
+
end
|
37
|
+
|
34
38
|
it "should call Channel#publish" do
|
35
39
|
mock_reactor_and_amqp do |channel|
|
36
|
-
e = Event.new(:name => "create",
|
37
|
-
:payload => { :foo => 'bar' }, :origin => :core)
|
38
|
-
channel.topic.should_receive(:publish)
|
39
40
|
producer = Producer.new(:channel => channel, :deliver_messages => true)
|
40
|
-
producer.
|
41
|
+
producer.should_receive(:safe_publish).with(event)
|
42
|
+
producer.publish(event)
|
41
43
|
end
|
42
44
|
end
|
43
45
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: untied-publisher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 5
|
10
|
+
version: 0.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Guilherme Cavalcanti
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-11-
|
18
|
+
date: 2012-11-20 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
version_requirements: &id001 !ruby/object:Gem::Requirement
|
@@ -165,6 +165,7 @@ files:
|
|
165
165
|
- lib/untied-publisher/config.rb
|
166
166
|
- lib/untied-publisher/doorkeeper.rb
|
167
167
|
- lib/untied-publisher/event.rb
|
168
|
+
- lib/untied-publisher/event_representer.rb
|
168
169
|
- lib/untied-publisher/observer.rb
|
169
170
|
- lib/untied-publisher/producer.rb
|
170
171
|
- lib/untied-publisher/railtie.rb
|