PHPackages                             divineomega/attempt - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. divineomega/attempt

Abandoned → [jord-jd/attempt](/?search=jord-jd%2Fattempt)Library[Utility &amp; Helpers](/categories/utility)

divineomega/attempt
===================

Attempt to run a function, retrying if needed

v3.1.0(1mo ago)211LGPL-3.0-onlyPHPPHP &gt;=7.1CI passing

Since Jan 4Pushed 1mo agoCompare

[ Source](https://github.com/Jord-JD/attempt)[ Packagist](https://packagist.org/packages/divineomega/attempt)[ GitHub Sponsors](https://github.com/DivineOmega)[ RSS](/packages/divineomega-attempt/feed)WikiDiscussions master Synced today

READMEChangelog (5)Dependencies (1)Versions (6)Used By (0)

Attempt
=======

[](#attempt)

Retry a PHP callable when an operation fails.

Installation
------------

[](#installation)

```
composer require jord-jd/attempt
```

The package supports PHP 7.1 through current PHP 8.x releases.

Usage
-----

[](#usage)

```
$result = attempt(function () {
    return fetchFromAnUnreliableApi();
})
    ->maxAttempts(5)
    ->withGap(2)
    ->now();
```

`maxAttempts(5)` means at most five total executions, including the first one. Use `maxAttempts(0)`, the default, for no attempt limit. The last exception is available as the previous exception on `MaxAttemptsExceeded`.

### Exponential or custom backoff

[](#exponential-or-custom-backoff)

```
$result = attempt($operation)
    ->maxAttempts(5)
    ->withExponentialBackoff(1, 2.0, 30)
    ->now();
```

Successive delays are 1, 2, 4, and 8 seconds, with later retries capped at 30 seconds. For complete control, return a non-negative integer number of seconds from `withBackoff()`:

```
use Exception;

$result = attempt($operation)
    ->withBackoff(function (int $failedAttempt, Exception $exception): int {
        return min(60, $failedAttempt * 5);
    })
    ->now();
```

Calling `withGap()` after `withBackoff()` switches back to a fixed delay.

### Retry selected exceptions

[](#retry-selected-exceptions)

Do not retry permanent failures:

```
$result = attempt($operation)
    ->retryIf(function (Exception $exception, int $failedAttempt): bool {
        return $exception instanceof TemporaryApiException;
    })
    ->maxAttempts(5)
    ->now();
```

When the predicate returns false, the original exception is rethrown.

### Observe retries

[](#observe-retries)

```
attempt($operation)
    ->onRetry(function (Exception $exception, int $failedAttempt, int $delay): void {
        logger()->warning('Operation failed; retrying', [
            'attempt' => $failedAttempt,
            'delay' => $delay,
            'exception' => $exception,
        ]);
    })
    ->now();
```

The callback runs only when another execution will actually be attempted.

### Time limits and scheduled starts

[](#time-limits-and-scheduled-starts)

```
// Retry until this instant. No execution is started at or after the deadline.
attempt($operation)
    ->until(new DateTimeImmutable('+30 seconds'))
    ->withGap(5)
    ->now();

// Wait until this instant, then begin. A past time starts immediately.
attempt($operation)
    ->at(new DateTimeImmutable('+5 seconds'));
```

If the next configured delay would cross the deadline, Attempt waits only until the deadline and throws `DateTimeExceeded` without starting a late execution.

All configuration methods can be chained. Negative attempt limits or gaps and invalid exponential-backoff values throw `InvalidArgumentException` immediately.

###  Health Score

45

—

FairBetter than 91% of packages

Maintenance94

Actively maintained with recent releases

Popularity10

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~596 days

Total

5

Last Release

30d ago

Major Versions

v1.0.1 → v2.0.02026-02-14

v2.0.0 → v3.0.02026-02-14

### Community

Maintainers

![](https://www.gravatar.com/avatar/c580cdf7c14898fff179cdfc1085892091d5d2f49d917873a12365af9ac77c93?d=identicon)[Jord-JD](/maintainers/Jord-JD)

---

Top Contributors

[![Jord-JD](https://avatars.githubusercontent.com/u/650645?v=4)](https://github.com/Jord-JD "Jord-JD (18 commits)")

---

Tags

attemptexception-handlingexceptionsphprate-limits

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/divineomega-attempt/health.svg)

```
[![Health](https://phpackages.com/badges/divineomega-attempt/health.svg)](https://phpackages.com/packages/divineomega-attempt)
```

###  Alternatives

[beta/bx.model

105.6k7](/packages/beta-bxmodel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
