untied-consumer 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 CHANGED
@@ -1,4 +1,7 @@
1
+ # 0.0.5
2
+
1
3
  - Fixes #1
4
+ - Enable Untied::Consumer::Worker to be initialized as a deamon
2
5
 
3
6
  # 0.0.3
4
7
 
@@ -1,5 +1,5 @@
1
1
  module Untied
2
2
  module Consumer
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
@@ -1,10 +1,12 @@
1
1
  # -*- encoding : utf-8 -*-
2
+
3
+ require 'amqp'
4
+
2
5
  module Untied
3
6
  module Consumer
4
7
  class Worker
5
8
  def initialize(opts={})
6
9
  @queue_name = opts[:queue_name] || ""
7
- @consumer = opts[:processor] || Processor.new
8
10
  end
9
11
 
10
12
  # Initializes the worker and calls the start method
@@ -15,14 +17,17 @@ module Untied
15
17
  end
16
18
 
17
19
  # Daemonizes the current worker. Remember you'll need the daemons Gem
18
- # in order to this method work correctly.
20
+ # in order to this method work correctly. A optional block may be passed
21
+ # in. The block is going to run in the context of the forked process.
19
22
  #
20
23
  # Options:
21
24
  # :pids_dir => '/some/dir' Absolute path to the dir where pid files will live
22
25
  # :log_dir => '/some/dir' Absolute path to the dir where log files will live
23
- def daemonize(opts={})
26
+ # :pname => 'mylovelydeamom'
27
+ def daemonize(opts={}, &block)
24
28
  require 'daemons' # just in case
25
29
 
30
+ pname = opts.delete(:pname) || 'untiedc'
26
31
  config = {
27
32
  :backtrace => true,
28
33
  :log_output => true,
@@ -32,32 +37,41 @@ module Untied
32
37
  }.merge(opts)
33
38
 
34
39
  if !(config[:dir] && config[:log_dir])
35
- raise ArgumentError "You need to provide pids_dir and log_dir"
40
+ raise ArgumentError.new("You need to provide pids_dir and log_dir")
36
41
  end
37
42
 
38
43
  FileUtils.mkdir_p(config[:dir])
39
44
  FileUtils.mkdir_p(config[:log_dir])
40
45
 
41
46
  @worker = self
42
- Daemons.run_proc('untiedc', config) { @worker.start }
47
+ @block = block
48
+ Daemons.run_proc(pname, config) do
49
+ @block.call if @block
50
+ @worker.start
51
+ end
43
52
  end
44
53
 
45
54
  # Listens to the mssage bus for relevant events. This method blocks the
46
55
  # current thread.
47
56
  def start
48
57
  AMQP.start do |connection|
49
- channel = AMQP::Channel.new(connection)
50
- exchange = channel.topic("untied", :auto_delete => true)
58
+ channel = AMQP::Channel.new(connection)
59
+ exchange = channel.topic("untied", :auto_delete => true)
60
+ @processor = processor
51
61
 
52
62
  channel.queue(@queue_name, :exclusive => true) do |queue|
63
+ Consumer.config.logger.info "Worker initialized and listening"
53
64
  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) }
65
+ safe_process { @processor.process(h,p) }
56
66
  end
57
67
  end
58
68
  end
59
69
  end
60
70
 
71
+ def processor
72
+ @processor ||= Processor.new
73
+ end
74
+
61
75
  protected
62
76
 
63
77
  def safe_process(&block)
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: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
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-06 00:00:00 Z
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