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 CHANGED
@@ -18,3 +18,4 @@ tmp
18
18
  *.swp
19
19
  *.swo
20
20
  .rspec
21
+ pkg
data/CHANGELOG.mkd CHANGED
@@ -1,3 +1,6 @@
1
+ - Fixes #1
2
+
1
3
  # 0.0.3
2
4
 
3
5
  - ``abort_on_exception`` configuration
6
+
@@ -70,6 +70,7 @@ module Untied
70
70
  return nil unless CALLBACKS.include? event_name
71
71
  return nil unless service == observed_service
72
72
  return nil unless observed_classes.include? klass
73
+ return nil unless respond_to?(event_name)
73
74
 
74
75
  self.send(event_name, entity)
75
76
  end
@@ -11,17 +11,17 @@ module Untied
11
11
 
12
12
  def process(headers, message)
13
13
  begin
14
- message = JSON.parse(message, :symbolize_names => true)
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(: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
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
- AMQP.start do |connection|
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
- AMQP.start do |connection|
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
@@ -1,5 +1,5 @@
1
1
  module Untied
2
2
  module Consumer
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  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[:consumer] || Processor.new
9
- @exchange = opts[:exchange]
7
+ @consumer = opts[:processor] || Processor.new
8
+ end
10
9
 
11
- Consumer.config.logger.info "Worker initialized and listening"
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
- @channel.queue(@queue_name, :exclusive => true) do |queue|
16
- queue.bind(@exchange, :routing_key => "untied.#").subscribe do |h,p|
17
- safe_process { @consumer.process(h,p) }
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
@@ -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
@@ -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, { :user => { :name => "Guila" } })
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: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
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-10-23 00:00:00 Z
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