untied-consumer 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/CHANGELOG.mkd +3 -0
- data/lib/untied-consumer/observer.rb +1 -0
- data/lib/untied-consumer/processor.rb +6 -6
- data/lib/untied-consumer/tasks/untied.tasks +3 -6
- data/lib/untied-consumer/tasks/untied_rails.tasks +3 -6
- data/lib/untied-consumer/version.rb +1 -1
- data/lib/untied-consumer/worker.rb +47 -8
- data/spec/observer_spec.rb +6 -0
- data/spec/processor_spec.rb +1 -1
- metadata +4 -4
data/.gitignore
CHANGED
data/CHANGELOG.mkd
CHANGED
@@ -11,17 +11,17 @@ module Untied
|
|
11
11
|
|
12
12
|
def process(headers, message)
|
13
13
|
begin
|
14
|
-
message = JSON.parse(message
|
14
|
+
message = JSON.parse(message)
|
15
15
|
rescue JSON::ParserError => e
|
16
16
|
Consumer.config.logger "Untied::Processor: Parsing error #{e}"
|
17
17
|
return
|
18
18
|
end
|
19
19
|
|
20
|
-
message = message.fetch(
|
21
|
-
payload = message.fetch(
|
22
|
-
service = message[
|
23
|
-
event_name = message[
|
24
|
-
klass = payload.keys.first
|
20
|
+
message = message.fetch("event", {})
|
21
|
+
payload = message.fetch("payload", {})
|
22
|
+
service = message["origin"].try(:to_sym)
|
23
|
+
event_name = message["name"].try(:to_sym)
|
24
|
+
klass = payload.keys.first.try(:to_sym)
|
25
25
|
|
26
26
|
Consumer.config.logger.info \
|
27
27
|
"Untied::Processor: processing event #{event_name} from #{service} with " + \
|
@@ -1,3 +1,5 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
1
3
|
require 'untied-consumer/worker'
|
2
4
|
require 'amqp'
|
3
5
|
|
@@ -5,12 +7,7 @@ namespace :untied do
|
|
5
7
|
namespace :consumer do
|
6
8
|
desc "Starts untied's worker"
|
7
9
|
task :work do
|
8
|
-
|
9
|
-
channel = AMQP::Channel.new(connection)
|
10
|
-
exchange = channel.topic("untied", :auto_delete => true)
|
11
|
-
worker = Untied::Consumer::Worker.new(:channel => channel, :exchange => exchange)
|
12
|
-
worker.start
|
13
|
-
end
|
10
|
+
worker = Untied::Consumer::Worker.start
|
14
11
|
end
|
15
12
|
end
|
16
13
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
1
3
|
require 'untied-consumer/worker'
|
2
4
|
require 'amqp'
|
3
5
|
|
@@ -5,12 +7,7 @@ namespace :untied do
|
|
5
7
|
namespace :consumer do
|
6
8
|
desc "Starts untied's worker"
|
7
9
|
task :work => :environment do
|
8
|
-
|
9
|
-
channel = AMQP::Channel.new(connection)
|
10
|
-
exchange = channel.topic("untied", :auto_delete => true)
|
11
|
-
worker = Untied::Consumer::Worker.new(:channel => channel, :exchange => exchange)
|
12
|
-
worker.start
|
13
|
-
end
|
10
|
+
Untied::Consumer::Worker.start
|
14
11
|
end
|
15
12
|
end
|
16
13
|
end
|
@@ -2,19 +2,58 @@
|
|
2
2
|
module Untied
|
3
3
|
module Consumer
|
4
4
|
class Worker
|
5
|
-
def initialize(opts)
|
6
|
-
@channel = opts[:channel]
|
5
|
+
def initialize(opts={})
|
7
6
|
@queue_name = opts[:queue_name] || ""
|
8
|
-
@consumer = opts[:
|
9
|
-
|
7
|
+
@consumer = opts[:processor] || Processor.new
|
8
|
+
end
|
10
9
|
|
11
|
-
|
10
|
+
# Initializes the worker and calls the start method
|
11
|
+
def self.start(opts={})
|
12
|
+
worker = new(opts)
|
13
|
+
worker.start
|
14
|
+
worker
|
12
15
|
end
|
13
16
|
|
17
|
+
# Daemonizes the current worker. Remember you'll need the daemons Gem
|
18
|
+
# in order to this method work correctly.
|
19
|
+
#
|
20
|
+
# Options:
|
21
|
+
# :pids_dir => '/some/dir' Absolute path to the dir where pid files will live
|
22
|
+
# :log_dir => '/some/dir' Absolute path to the dir where log files will live
|
23
|
+
def daemonize(opts={})
|
24
|
+
require 'daemons' # just in case
|
25
|
+
|
26
|
+
config = {
|
27
|
+
:backtrace => true,
|
28
|
+
:log_output => true,
|
29
|
+
:dir_mode => :normal,
|
30
|
+
:dir => opts[:pids_dir],
|
31
|
+
:log_dir => nil,
|
32
|
+
}.merge(opts)
|
33
|
+
|
34
|
+
if !(config[:dir] && config[:log_dir])
|
35
|
+
raise ArgumentError "You need to provide pids_dir and log_dir"
|
36
|
+
end
|
37
|
+
|
38
|
+
FileUtils.mkdir_p(config[:dir])
|
39
|
+
FileUtils.mkdir_p(config[:log_dir])
|
40
|
+
|
41
|
+
@worker = self
|
42
|
+
Daemons.run_proc('untiedc', config) { @worker.start }
|
43
|
+
end
|
44
|
+
|
45
|
+
# Listens to the mssage bus for relevant events. This method blocks the
|
46
|
+
# current thread.
|
14
47
|
def start
|
15
|
-
|
16
|
-
|
17
|
-
|
48
|
+
AMQP.start do |connection|
|
49
|
+
channel = AMQP::Channel.new(connection)
|
50
|
+
exchange = channel.topic("untied", :auto_delete => true)
|
51
|
+
|
52
|
+
channel.queue(@queue_name, :exclusive => true) do |queue|
|
53
|
+
queue.bind(exchange, :routing_key => "untied.#").subscribe do |h,p|
|
54
|
+
Consumer.config.logger.info "Worker initialized and listening"
|
55
|
+
safe_process { @consumer.process(h,p) }
|
56
|
+
end
|
18
57
|
end
|
19
58
|
end
|
20
59
|
end
|
data/spec/observer_spec.rb
CHANGED
@@ -109,6 +109,12 @@ module Untied
|
|
109
109
|
subject.should_not_receive(:after_create)
|
110
110
|
subject.notify
|
111
111
|
end
|
112
|
+
|
113
|
+
it "should not raise error when no callback method is defined" do
|
114
|
+
expect {
|
115
|
+
subject.notify(:after_commit, :user, :core, { :user => {} })
|
116
|
+
}.to_not raise_error(NoMethodError, /undefined method `after_commit'/)
|
117
|
+
end
|
112
118
|
end
|
113
119
|
end
|
114
120
|
end
|
data/spec/processor_spec.rb
CHANGED
@@ -49,7 +49,7 @@ module Untied
|
|
49
49
|
|
50
50
|
it "should call Observer#notify with correct arguments" do
|
51
51
|
SomeObserver.instance.should_receive(:notify).
|
52
|
-
with(:after_create, :user, :core, {
|
52
|
+
with(:after_create, :user, :core, { "user" => { "name" => "Guila" } })
|
53
53
|
MyProcessor.new.process({}, message.to_json)
|
54
54
|
end
|
55
55
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: untied-consumer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
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-
|
18
|
+
date: 2012-11-06 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
version_requirements: &id001 !ruby/object:Gem::Requirement
|