Skip to content

Instantly share code, notes, and snippets.

@btwelch
Created December 18, 2023 14:59
Show Gist options
  • Save btwelch/0f3b3f9a26dbb39c4b807d899347b348 to your computer and use it in GitHub Desktop.
Save btwelch/0f3b3f9a26dbb39c4b807d899347b348 to your computer and use it in GitHub Desktop.
Tracked Retries
module TrackedRetries
def with_tracked_retries(sync_record, max_attempts: 3)
attempts = 0
begin
attempts += 1
sync_record.update(attempts: attempts)
yield
sync_record.update(success: true)
rescue => e
retry if attempts < max_attempts
error = ["Error: #{e.message}", "Type: #{e.class}", "Backtrace: #{e.backtrace.first(5).join("\n")}"].join(", ")
sync_record.update(success: false, error: error)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment