attempt

A tiny Crystal library for trying code blocks again and again.

Attempt

A tiny Crystal library for trying code blocks again and again.

Build Status

Installation

Add this to your application's shard.yml:

dependencies:
  attempt:
    github: mosop/attempt

Code Samples

Trying 5 Times

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

Wait 60 Sec. Before a Retry

response = Attempt.times(5).wait(60).start do
  begin
    response = HTTP::Client.get("http://unstable.net")
    break response if response.success?
  rescue
  end
end

Every Hour Forever

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

Wait 10 Sec. Before the First Attempt

`rails s`
response = Attempt.prewait(10).start do
  begin
    response = HTTP::Client.get("http://localhost:3000")
    break response if response.success?
  rescue
  end
end

Usage

require "attempt"
Repository

attempt

Owner
Statistic
  • 0
  • 0
  • 0
  • 1
  • 2
  • over 7 years ago
  • January 30, 2017
License

MIT License

Links
Synced at

Tue, 07 May 2024 01:12:39 GMT

Languages