https://github.com/divineomega/attempt
🔁 Attempt to run a function, automatically retrying if an exception occurs
https://github.com/divineomega/attempt
attempt exception-handling exceptions php rate-limits
Last synced: about 1 year ago
JSON representation
🔁 Attempt to run a function, automatically retrying if an exception occurs
- Host: GitHub
- URL: https://github.com/divineomega/attempt
- Owner: DivineOmega
- License: lgpl-3.0
- Created: 2019-04-29T23:12:35.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-01-04T22:48:57.000Z (over 6 years ago)
- Last Synced: 2025-02-10T05:28:58.372Z (over 1 year ago)
- Topics: attempt, exception-handling, exceptions, php, rate-limits
- Language: PHP
- Homepage:
- Size: 13.7 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🔁 Attempt
This PHP package allows you to attempt to run a function, automatically retrying if an
exception occurs.
It can useful for:
* Unreliable connectivity or APIs
* Interaction with rate-limited systems
* Handling unreliable input data
## Installation
To install Attempt, just run the following command.
```bash
composer require divineomega/attempt
```
## Usage
See the following usage examples.
```php
// Attempts to run the function immediately. If an exception occurs, retry forever.
attempt(function() {
// ...
})->now();
// Attempts to run the function immediately. If an exception occurs, retry up to 5 times.
attempt(function() {
// ...
})->maxAttempts(5)
->now();
// Attempts to run the function immediately. If an exception occurs, retry until the specified date time.
attempt(function() {
// ...
})->until($datetime)
->now();
// Attempts to run the function immediately. If an exception occurs, retry forever, with a 20 second gap between attempts.
attempt(function() {
// ...
})->withGap(20)
->now();
// Attempts to run the function at a specified date time. If an exception occurs, retry forever. The thread will block until the specified date time is reached.
attempt(function() {
// ...
})->at($datetime);
```
Most of these methods can be chained and used together as you might expect to give
the desired functionality.