daemon_objects 0.1.8 → 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,16 +1,27 @@
|
|
1
1
|
module DaemonObjects::AmqpSupport
|
2
|
-
attr_accessor :endpoint, :queue, :prefetch, :worker_class
|
2
|
+
attr_accessor :endpoint, :queue, :prefetch, :worker_class,
|
3
|
+
:retry_wait_time
|
3
4
|
|
4
5
|
def arguments
|
5
6
|
@arguments ||= {}
|
6
7
|
end
|
7
8
|
|
9
|
+
def retry_wait_time
|
10
|
+
@retry_wait_time || 5
|
11
|
+
end
|
12
|
+
|
8
13
|
def run
|
9
14
|
logger.info "Preparing to start the AMQP watcher."
|
10
15
|
|
11
16
|
connection = Bunny.new(endpoint)
|
12
17
|
connection.start
|
13
18
|
|
19
|
+
Signal.trap("INT") do
|
20
|
+
logger.info "Received signal 'INT'. Exiting process"
|
21
|
+
connection.close { EventMachine.stop }
|
22
|
+
exit
|
23
|
+
end
|
24
|
+
|
14
25
|
logger.info "Starting up the AMQP watcher."
|
15
26
|
|
16
27
|
channel = connection.create_channel
|
@@ -28,9 +39,18 @@ module DaemonObjects::AmqpSupport
|
|
28
39
|
|
29
40
|
logger.info "AMQP worker started"
|
30
41
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
42
|
+
rescue Bunny::InternalError, Bunny::TCPConnectionFailed => e
|
43
|
+
logger.error(e) && e.backtrace.join("\n")
|
44
|
+
wait && retry
|
45
|
+
end
|
46
|
+
|
47
|
+
def wait
|
48
|
+
retry_message = "* Retrying connection in #{retry_wait_time} seconds .... *"
|
49
|
+
sleep(retry_wait_time)
|
50
|
+
logger.info("\n")
|
51
|
+
logger.info("*" * retry_message.length)
|
52
|
+
logger.info(retry_message)
|
53
|
+
logger.info("*" * retry_message.length)
|
54
|
+
logger.info("\n")
|
35
55
|
end
|
36
56
|
end
|
data/lib/daemon_objects/base.rb
CHANGED
@@ -5,11 +5,12 @@ class DaemonObjects::Base
|
|
5
5
|
|
6
6
|
def self.consumes_amqp(opts={})
|
7
7
|
extend DaemonObjects::AmqpSupport
|
8
|
-
self.endpoint
|
9
|
-
self.queue
|
8
|
+
self.endpoint = opts.delete(:endpoint)
|
9
|
+
self.queue = opts.delete(:queue_name)
|
10
10
|
self.arguments["x-message-ttl"] = opts.delete(:ttl) if opts[:ttl]
|
11
|
-
self.prefetch
|
12
|
-
self.
|
11
|
+
self.prefetch = opts.delete(:prefetch)
|
12
|
+
self.retry_wait_time = opts.delete(:retry_wait_time)
|
13
|
+
self.worker_class = opts.delete(:worker_class) || DaemonObjects::Amqp::Worker
|
13
14
|
self.arguments.merge!(opts)
|
14
15
|
|
15
16
|
logger.info "Configured to consume queue [#{queue}] at endpoint [#{endpoint}]"
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DaemonObjects::AmqpSupport do
|
4
|
+
before :each do
|
5
|
+
MyWorker = Class.new(DaemonObjects::Amqp::Worker) do
|
6
|
+
def initialize(*args); end
|
7
|
+
def start; end
|
8
|
+
def self.counter
|
9
|
+
@counter ||= 0
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.counter=(value)
|
13
|
+
@counter = value
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
MyConsumer = Class.new(DaemonObjects::ConsumerBase)
|
18
|
+
MyDaemon = Class.new(DaemonObjects::Base) do
|
19
|
+
consumes_amqp :endpoint => 'amqp://localhost:4567',
|
20
|
+
:queue_name => 'queue',
|
21
|
+
:worker_class => MyWorker,
|
22
|
+
:retry_wait_time => 0
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
after :each do
|
27
|
+
Object.send( :remove_const, :MyWorker) if defined?(MyWorker)
|
28
|
+
Object.send( :remove_const, :MyConsumer) if defined?(MyConsumer)
|
29
|
+
Object.send( :remove_const, :MyDaemon) if defined?(MyDaemon)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should retry on initial connection if cannot connect' do
|
33
|
+
|
34
|
+
bunny = double(Bunny).as_null_object
|
35
|
+
# First attempt should raise and retry
|
36
|
+
Bunny.should_receive(:new).
|
37
|
+
with('amqp://localhost:4567'){
|
38
|
+
raise Bunny::TCPConnectionFailed.new("could not connect", "localhost", 4567)
|
39
|
+
}
|
40
|
+
|
41
|
+
# Second attempt succeeds to exit spec
|
42
|
+
Bunny.should_receive(:new).
|
43
|
+
with('amqp://localhost:4567').and_return(bunny)
|
44
|
+
MyDaemon.run
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should retry if connection is lost' do
|
48
|
+
bunny = double(Bunny).as_null_object
|
49
|
+
|
50
|
+
MyWorker.class_eval do
|
51
|
+
def start
|
52
|
+
self.class.counter += 1
|
53
|
+
# Only raise error on first attempt
|
54
|
+
raise Bunny::InternalError.new('lost connection', nil, true) if self.class.counter <= 1
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
Bunny.should_receive(:new).twice.
|
59
|
+
with('amqp://localhost:4567').and_return(bunny)
|
60
|
+
|
61
|
+
MyDaemon.run
|
62
|
+
end
|
63
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: daemon_objects
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-02-
|
12
|
+
date: 2014-02-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: daemons
|
@@ -140,6 +140,7 @@ files:
|
|
140
140
|
- spec/lib/daemon_objects/base_spec.rb
|
141
141
|
- spec/lib/daemon_objects/consumer_base_spec.rb
|
142
142
|
- spec/lib/daemon_objects/logging_spec.rb
|
143
|
+
- spec/lib/daemon_objects/runner_spec.rb
|
143
144
|
- spec/lib/daemon_objects_spec.rb
|
144
145
|
- spec/spec_helper.rb
|
145
146
|
- spec/support/stub_logger.rb
|
@@ -174,6 +175,7 @@ test_files:
|
|
174
175
|
- spec/lib/daemon_objects/base_spec.rb
|
175
176
|
- spec/lib/daemon_objects/consumer_base_spec.rb
|
176
177
|
- spec/lib/daemon_objects/logging_spec.rb
|
178
|
+
- spec/lib/daemon_objects/runner_spec.rb
|
177
179
|
- spec/lib/daemon_objects_spec.rb
|
178
180
|
- spec/spec_helper.rb
|
179
181
|
- spec/support/stub_logger.rb
|