Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/spider-gazelle/simple_retry
a tool for retrying code blocks
https://github.com/spider-gazelle/simple_retry
Last synced: 3 months ago
JSON representation
a tool for retrying code blocks
- Host: GitHub
- URL: https://github.com/spider-gazelle/simple_retry
- Owner: spider-gazelle
- License: mit
- Created: 2020-04-07T06:42:45.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-05-26T01:48:08.000Z (over 3 years ago)
- Last Synced: 2024-06-21T18:12:46.109Z (5 months ago)
- Language: Crystal
- Size: 9.77 KB
- Stars: 6
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-crystal - simple_retry - Simple tool for retrying failed code blocks (Misc)
README
# Simple Retry
[![CI](https://github.com/spider-gazelle/simple_retry/actions/workflows/ci.yml/badge.svg)](https://github.com/spider-gazelle/simple_retry/actions/workflows/ci.yml)
A library for managing blocks of code that you might want to retry when an error occurs.
## Installation
1. Add the dependency to your `shard.yml`:
```yaml
dependencies:
simple_retry:
github: spider-gazelle/simple_retry
```2. Run `shards install`
## Usage
```crystal
require "simple_retry"SimpleRetry.try_to do
# Something that should succeed eventually
num = rand(10)
raise "bad number #{num}" if num < 5# Return the number
num
end
```There are a number of options you can use to customise
```crystal
require "simple_retry"SimpleRetry.try_to(
# Runs the block at most 5 times
max_attempts: 5,
# Will always stop retrying on these errors
raise_on: DivisionByZeroError | ArgumentError,
# Will only retry on these errors
retry_on: Exception,
# Initial delay time after first retry
base_interval: 10.milliseconds,
# Exponentially increase delay up to this period
max_interval: 10.seconds,
# Adjust the exponential growth by a random amount
randomise: 10.milliseconds
) do |run_count : UInt64, last_error : Exception?, next_delay_time : Time::Span|
# Something that should succeed
end
```