Retry blocks of Crystal code.
Add this to your application's shard.yml:
dependencies:
retrycr:
github: taylorfinnell/retrycrrequire "retrycr"Retry all exceptions.
retryable(tries: 1, wait: 1) do
# my code
endYou may specify which Exception classes trigger a retry.
retryable(on: ArgumentError | Base64::Error, tries: 1, wait: 1) do
# my code
endThe wait parameter can either be an Int32 or a Proc.
retryable(tries: 1, wait: ->(x : Int32) { 2**x }) do
# my code
endThe number of retries that have happened is yielded to the block.
retryable(tries: 1, wait: 1) do |retries|
puts "There have been #{retries} retries!"
endYou can specify a callback to run everytime the code is retried.
callback = -> (ex : Exeption) { "The code is about to be retried!" }
retryable(tries: 1, wait: 1, callback: callback) do |retries|
puts "There have been #{retries} retries!"
endYou can specify a callback to run after all retries are exhausted.
f = File.open("test")
callback = -> (retries : Int32) { f.close }
retryable(tries: 1, wait: 1, finally: callback) do |retries|
f.read
end