Skip to content
This repository was archived by the owner on Oct 20, 2022. It is now read-only.

Commit 91096cb

Browse files
Merge pull request #13 from Orange-OpenSource/issue_12
Send retry feature
2 parents d4ec028 + ba3d363 commit 91096cb

4 files changed

Lines changed: 65 additions & 5 deletions

File tree

lib/xymonclient.rb

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@ module XymonClient
1212
# (port default to 1984)
1313
class Client
1414
attr_reader :servers
15+
attr_accessor :retry_count
16+
attr_accessor :retry_interval
1517

16-
def initialize(servers = [])
18+
def initialize(servers = [], retry_count = 3, retry_interval = 5)
1719
@servers = \
1820
if servers.empty?
1921
XymonClient::ServerDiscovery.find_from_file
2022
else
2123
_parse_servers(servers)
2224
end
25+
@retry_count = retry_count
26+
@retry_interval = retry_interval
2327
end
2428

2529
def status(host, service, status, message, lifetime = '30m')
@@ -65,21 +69,40 @@ def ack(host, service, duration, message)
6569
unless XymonClient.valid_duration?(duration)
6670
cookies = board(host, service, ['cookie'])
6771
@servers.each do |server|
72+
next if cookies[server].to_i == -1
6873
_send(
6974
server,
7075
"xymondack #{cookies[server].to_i} #{duration} #{message}"
71-
) if cookies[server].to_i != -1
76+
)
7277
end
7378
end
7479

7580
private
7681

7782
def _send_to_all(message)
78-
@servers.each { |server| _send(server, message) }
83+
fail_srv = []
84+
send_result = true
85+
@servers.each do |server|
86+
retry_count = 0
87+
begin
88+
_send(server, message)
89+
rescue
90+
if retry_count < @retry_count || @retry_count == -1
91+
sleep @retry_interval
92+
retry_count += 1
93+
retry
94+
else
95+
send_result = false
96+
fail_srv << server
97+
end
98+
end
99+
end
100+
raise XymonClient::SendFailure if fail_srv.count == @servers.count
101+
raise XymonClient::PartialSendFailure, fail_srv \
102+
if (1..@servers.count - 1).cover?(fail_srv.count)
79103
end
80104

81105
def _send(server, message)
82-
# TODO: validate response from all servers ( and retry ?)
83106
socket = TCPSocket.open(server[:host], server[:port])
84107
socket.puts message
85108
socket.close_write

lib/xymonclient/exception.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,10 @@ class InvalidHost < StandardError
1919

2020
class InvalidServiceItem < StandardError
2121
end
22+
23+
class PartialSendFailure < StandardError
24+
end
25+
26+
class SendFailure < StandardError
27+
end
2228
end

lib/xymonclient/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module XymonClient
2-
VERSION = '0.3.0'.freeze
2+
VERSION = '0.4.0'.freeze
33
end

spec/xymonclient_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,36 @@
1313
)
1414
client.status('my.host', 'myservice', 'green', 'all is good')
1515
end
16+
17+
it 'could not send a status to some servers' do
18+
client.retry_count = 0
19+
times_called = 0
20+
allow(client).to receive(:_send) do
21+
times_called += 1
22+
raise Errno::ECONNREFUSED if times_called == 2
23+
end
24+
expect do
25+
client.status('my.host', 'myservice', 'green', 'all is good')
26+
end.to raise_error(XymonClient::PartialSendFailure)
27+
end
28+
29+
it 'could not send a status to all servers' do
30+
allow(client).to receive(:_send).and_raise Errno::ECONNREFUSED
31+
expect do
32+
client.status('my.host', 'myservice', 'green', 'all is good')
33+
end.to raise_error(XymonClient::SendFailure)
34+
end
35+
36+
it 'retry sending a status to some servers' do
37+
client.retry_count = 2
38+
times_called = 0
39+
allow(client).to receive(:_send) do
40+
times_called += 1
41+
raise Errno::ECONNREFUSED if times_called == 1
42+
end
43+
expect do
44+
client.status('my.host', 'myservice', 'green', 'all is good')
45+
end.not_to raise_error
46+
end
1647
end
1748
end

0 commit comments

Comments
 (0)