Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
add retry mechanism to fix mocdaniel/dashing-icinga2#9
Browse files Browse the repository at this point in the history
implements issue #6
  • Loading branch information
bodsch committed Jun 20, 2017
1 parent 1e102ba commit 042772e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
55 changes: 29 additions & 26 deletions lib/icinga2/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def self.get( params = {} )
options = params.dig(:options)
payload = params.dig(:payload) || {}
result = {}
max_retries = 3
max_retries = 30
times_retried = 0

return get_with_payload( params ) if payload.count >= 1
Expand Down Expand Up @@ -78,15 +78,16 @@ def self.get( params = {} )
name: host,
message: error['status']
}
rescue Errno::ECONNREFUSED => e
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH => e

if( times_retried < max_retries )

times_retried += 1
$stderr.puts( format( 'Cannot execute request to %s, cause: %s', url, e ) )
$stderr.puts( format( ' retry %d%d', times_retried, max_retries ) )
$stderr.puts(format( 'Cannot execute request %s', url ))
$stderr.puts(format( ' cause: %s', e ))
$stderr.puts(format( ' retry %d / %d', times_retried, max_retries ))

sleep( 2 )
sleep( 4 )
retry
else
$stderr.puts( 'Exiting request ...' )
Expand All @@ -99,7 +100,6 @@ def self.get( params = {} )
end

result

end

# static function for GET Requests with payload
Expand All @@ -123,7 +123,7 @@ def self.get_with_payload( params = {} )
options = params.dig(:options)
payload = params.dig(:payload) || {}
result = {}
max_retries = 3
max_retries = 30
times_retried = 0

headers['X-HTTP-Method-Override'] = 'GET'
Expand All @@ -150,7 +150,6 @@ def self.get_with_payload( params = {} )
unless( results.nil? )

results.each do |r|

node[r.dig('name')] = r
end

Expand All @@ -170,15 +169,16 @@ def self.get_with_payload( params = {} )
name: host,
message: error['status']
}
rescue Errno::ECONNREFUSED => e
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH => e

if( times_retried < max_retries )

times_retried += 1
$stderr.puts( format( 'Cannot execute request to %s, cause: %s', url, e ) )
$stderr.puts( format( ' retry %d%d', times_retried, max_retries ) )
$stderr.puts(format( 'Cannot execute request %s', url ))
$stderr.puts(format( ' cause: %s', e ))
$stderr.puts(format( ' retry %d / %d', times_retried, max_retries ))

sleep( 2 )
sleep( 4 )
retry
else
$stderr.puts( 'Exiting request ...' )
Expand Down Expand Up @@ -211,7 +211,7 @@ def self.post( params = {} )
headers = params.dig(:headers)
options = params.dig(:options)
payload = params.dig(:payload)
max_retries = 3
max_retries = 30
times_retried = 0

headers['X-HTTP-Method-Override'] = 'POST'
Expand Down Expand Up @@ -267,15 +267,16 @@ def self.post( params = {} )
}
end

rescue Errno::ECONNREFUSED => e
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH => e

if( times_retried < max_retries )

times_retried += 1
$stderr.puts( format( 'Cannot execute request to %s, cause: %s', url, e ) )
$stderr.puts( format( ' retry %d%d', times_retried, max_retries ) )
$stderr.puts(format( 'Cannot execute request %s', url ))
$stderr.puts(format( ' cause: %s', e ))
$stderr.puts(format( ' retry %d / %d', times_retried, max_retries ))

sleep( 2 )
sleep( 4 )
retry
else
$stderr.puts( 'Exiting request ...' )
Expand Down Expand Up @@ -309,7 +310,7 @@ def self.put( params = {} )
headers = params.dig(:headers)
options = params.dig(:options)
payload = params.dig(:payload)
max_retries = 3
max_retries = 30
times_retried = 0

headers['X-HTTP-Method-Override'] = 'PUT'
Expand Down Expand Up @@ -380,15 +381,16 @@ def self.put( params = {} )

end

rescue Errno::ECONNREFUSED => e
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH => e

if( times_retried < max_retries )

times_retried += 1
$stderr.puts( format( 'Cannot execute request to %s, cause: %s', url, e ) )
$stderr.puts( format( ' retry %d%d', times_retried, max_retries ) )
$stderr.puts(format( 'Cannot execute request %s', url ))
$stderr.puts(format( ' cause: %s', e ))
$stderr.puts(format( ' retry %d / %d', times_retried, max_retries ))

sleep( 2 )
sleep( 4 )
retry
else
$stderr.puts( 'Exiting request ...' )
Expand Down Expand Up @@ -474,15 +476,16 @@ def self.delete( params = {} )
}

end
rescue Errno::ECONNREFUSED => e
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH => e

if( times_retried < max_retries )

times_retried += 1
$stderr.puts( format( 'Cannot execute request to %s, cause: %s', url, e ) )
$stderr.puts( format( ' retry %d%d', times_retried, max_retries ) )
$stderr.puts(format( 'Cannot execute request %s', url ))
$stderr.puts(format( ' cause: %s', e ))
$stderr.puts(format( ' retry %d / %d', times_retried, max_retries ))

sleep( 2 )
sleep( 4 )
retry
else
$stderr.puts( 'Exiting request ...' )
Expand Down
4 changes: 2 additions & 2 deletions lib/icinga2/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ module Version
# minor part of version
MINOR = 6
# tiny part of version
TINY = 3
TINY = 4
# patch part
PATCH = 1
PATCH = 0

end

Expand Down

0 comments on commit 042772e

Please sign in to comment.