Skip to content

πŸ” Attempt to run a function, automatically retrying if an exception occurs

License

Notifications You must be signed in to change notification settings

DivineOmega/attempt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ” 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.

composer require divineomega/attempt

Usage

See the following usage examples.

// 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.

About

πŸ” Attempt to run a function, automatically retrying if an exception occurs

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages