A tiny Crystal library for trying code blocks again and again.
Add this to your application's shard.yml
:
dependencies:
attempt:
github: mosop/attempt
response = Attempt.times(5).start do
begin
response = HTTP::Client.get("http://unstable.net")
break response if response.success?
rescue
end
end
if response
puts response.body
else
raise "UNSTABLE!"
end
response = Attempt.times(5).wait(60).start do
begin
response = HTTP::Client.get("http://unstable.net")
break response if response.success?
rescue
end
end
Attempt.wait(60 * 60).start do
begin
response = HTTP::Client.get("http://api.cat.pics/links.json")
File.write("/path/to/kitty.json", response.body) if response.success?
rescue
end
end
`rails s`
response = Attempt.prewait(10).start do
begin
response = HTTP::Client.get("http://localhost:3000")
break response if response.success?
rescue
end
end
require "attempt"