amqp_helpers 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/amqp_helpers/daemon.rb +18 -5
- data/spec/daemon_spec.rb +1 -0
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
data/lib/amqp_helpers/daemon.rb
CHANGED
@@ -10,7 +10,7 @@ module AMQPHelpers
|
|
10
10
|
class ConnectionError < Error; end
|
11
11
|
class ChannelError < Error; end
|
12
12
|
|
13
|
-
DEFAULT_RECONNECT_WAIT_TIME =
|
13
|
+
DEFAULT_RECONNECT_WAIT_TIME = 10
|
14
14
|
|
15
15
|
attr_accessor :name, :exchanges, :connection_params
|
16
16
|
attr_writer :environment, :logger, :queue_name, :queue_params, :reconnect_wait_time
|
@@ -26,7 +26,10 @@ module AMQPHelpers
|
|
26
26
|
|
27
27
|
def start(&handler)
|
28
28
|
logger.info "Starting #{name} daemon..."
|
29
|
-
|
29
|
+
tcp_connection_failure_handler = Proc.new(&method(:handle_tcp_connection_failure))
|
30
|
+
amqp_params = { on_tcp_connection_failure: tcp_connection_failure_handler}.merge(connection_params)
|
31
|
+
AMQP.start(amqp_params) do |connection|
|
32
|
+
connection.on_open(&method(:handle_open))
|
30
33
|
connection.on_error(&method(:handle_connection_error))
|
31
34
|
channel = initialize_channel(connection)
|
32
35
|
connection.on_tcp_connection_loss(&method(:handle_tcp_connection_loss))
|
@@ -35,11 +38,12 @@ module AMQPHelpers
|
|
35
38
|
queue = initialize_queue(channel)
|
36
39
|
queue.subscribe(&handler)
|
37
40
|
|
38
|
-
show_stopper = Proc.new do
|
39
|
-
logger.info "Signal
|
41
|
+
show_stopper = Proc.new do |signal|
|
42
|
+
logger.info "Signal #{signal} received. #{name} is going down... I REPEAT: WE ARE GOING DOWN!"
|
40
43
|
connection.close { EventMachine.stop }
|
41
44
|
end
|
42
45
|
Signal.trap 'INT', show_stopper
|
46
|
+
Signal.trap 'TERM', show_stopper
|
43
47
|
end
|
44
48
|
end
|
45
49
|
|
@@ -68,6 +72,15 @@ module AMQPHelpers
|
|
68
72
|
end
|
69
73
|
|
70
74
|
protected
|
75
|
+
def handle_tcp_connection_failure(settings)
|
76
|
+
logger.error "[network failure] Could not connect to #{settings[:host]}:#{settings[:port]}"
|
77
|
+
raise ConnectionError, "Failed to connect!"
|
78
|
+
end
|
79
|
+
|
80
|
+
def handle_open
|
81
|
+
logger.info "#{name} successfully opened AMQP connection"
|
82
|
+
end
|
83
|
+
|
71
84
|
def handle_connection_error(connection, connection_close)
|
72
85
|
logger.error "[connection.close] Reply code = #{connection_close.reply_code}, reply text = #{connection_close.reply_text}"
|
73
86
|
# check if graceful broker shutdown
|
@@ -86,7 +99,7 @@ module AMQPHelpers
|
|
86
99
|
|
87
100
|
def handle_tcp_connection_loss(connection, settings)
|
88
101
|
logger.error '[network failure] Trying to reconnect...'
|
89
|
-
connection.reconnect(false,
|
102
|
+
connection.reconnect(false, reconnect_wait_time)
|
90
103
|
end
|
91
104
|
|
92
105
|
def handle_recovery(connection, settings)
|
data/spec/daemon_spec.rb
CHANGED
@@ -46,6 +46,7 @@ describe AMQPHelpers::Daemon do
|
|
46
46
|
connection.stub(:open?).and_return(true)
|
47
47
|
connection.stub(:channel_max).and_return(0)
|
48
48
|
connection.stub(:on_connection)
|
49
|
+
connection.stub(:on_recovery)
|
49
50
|
connection.stub(:on_error)
|
50
51
|
connection.stub(:on_tcp_connection_loss)
|
51
52
|
connection.stub(:next_channel_id).and_return(0)
|