Skip to content

Instantly share code, notes, and snippets.

@eddbot
Created February 28, 2024 11:45
Show Gist options
  • Save eddbot/c5a728464f3c1c103fe80ad8f615c73d to your computer and use it in GitHub Desktop.
Save eddbot/c5a728464f3c1c103fe80ad8f615c73d to your computer and use it in GitHub Desktop.
Basic Retry Mechanism using redo
# frozen_string_literal: true
backoff = 0.2
retries = 0
MAX_RETRIES = 5
flaky = lambda do
flaky_call = rand(10)
return if flaky_call == 1
puts "#flaky failed, retrying in #{backoff.round(2)} seconds.."
sleep backoff
backoff += (backoff * 1.5)
retries += 1
# restart the block, wooow :)
redo if retries < MAX_RETRIES
puts '#flaky error, aborting'
end
flaky.call
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment