PHPackages                             harris21/laravel-fuse - 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. [Queues &amp; Workers](/categories/queues)
4. /
5. harris21/laravel-fuse

ActiveLibrary[Queues &amp; Workers](/categories/queues)

harris21/laravel-fuse
=====================

Circuit breaker for Laravel queue jobs. Protect your workers from cascading failures.

v0.3.0(2mo ago)3786.5k↑118%13MITPHPPHP ^8.3CI passing

Since Feb 2Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/harris21/laravel-fuse)[ Packagist](https://packagist.org/packages/harris21/laravel-fuse)[ RSS](/packages/harris21-laravel-fuse/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (14)Versions (13)Used By (0)

 [![Fuse for Laravel](art/logo.png)](art/logo.png)

 **Circuit breaker for Laravel queue jobs**

 Protect your queue workers from cascading failures when external services go down.

---

The Problem
-----------

[](#the-problem)

When Stripe goes down at 11 PM, your queue workers don't know. They keep trying to charge customers. Each job waits 30 seconds for a timeout. Then retries. Waits again. Your entire queue system freezes.

**Without Fuse:** 10,000 jobs × 30-second timeouts = 25+ hours to clear the queue.

**With Fuse:** Circuit opens after 5 failures. Queue clears in 10 seconds. Automatic recovery when the service returns.

---

Features
--------

[](#features)

- **Three-State Circuit Breaker** — CLOSED (normal), OPEN (protected), HALF-OPEN (testing recovery)
- **Intelligent Failure Classification** — 429 rate limits and auth errors don't trip the circuit
- **Peak Hours Support** — Different thresholds for business hours vs. off-peak
- **Fixed Window Tracking** — Minute-based buckets with automatic expiration, no cleanup needed
- **Thundering Herd Prevention** — `Cache::lock()` ensures only one worker probes during recovery
- **Zero Data Loss** — Jobs are delayed with `release()`, not failed permanently
- **Automatic Recovery** — Circuit tests and heals itself when services return
- **Per-Service Circuits** — Separate breakers for Stripe, Mailgun, your microservices
- **Laravel Events** — Get notified on state transitions for alerting and monitoring
- **Real-Time Status Page** — Built-in monitoring dashboard with live state updates
- **Pure Laravel** — No external dependencies, uses Cache and native job middleware

---

How It Works
------------

[](#how-it-works)

 [![Circuit Breaker States](art/circuit-states.png)](art/circuit-states.png)

**CLOSED** — Normal operations. All requests pass through. Failures are tracked in the background.

**OPEN** — Protection mode. After the failure threshold is exceeded, the circuit trips. Jobs fail instantly (1ms, not 30s) and are delayed for automatic retry. No API calls are made.

**HALF-OPEN** — Testing recovery. After the timeout period, one probe request tests if the service recovered. Success closes the circuit. Failure reopens it.

---

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

[](#installation)

```
composer require harris21/laravel-fuse
```

Publish the configuration:

```
php artisan vendor:publish --tag=fuse-config
```

---

Quick Start
-----------

[](#quick-start)

Add the middleware to your job:

```
use Harris21\Fuse\Middleware\CircuitBreakerMiddleware;

class ChargeCustomer implements ShouldQueue
{
    public $tries = 0;           // Unlimited releases
    public $maxExceptions = 3;   // Only real failures count

    public function middleware(): array
    {
        return [new CircuitBreakerMiddleware('stripe')];
    }

    public function handle(): void
    {
        // Your payment logic - unchanged
        Stripe::charges()->create([...]);
    }
}
```

That's it. Your job is now protected.

---

Configuration
-------------

[](#configuration)

```
// config/fuse.php

return [
    'enabled' => env('FUSE_ENABLED', true),

    'default_threshold' => 50,      // Failure rate percentage to trip circuit
    'default_timeout' => 60,        // Seconds before testing recovery
    'default_min_requests' => 10,   // Minimum requests before evaluating

    'services' => [
        'stripe' => [
            'threshold' => 50,
            'timeout' => 30,
            'min_requests' => 5,

            // Peak hours: more tolerant during business hours
            'peak_hours_threshold' => 60,
            'peak_hours_start' => 9,   // 9 AM
            'peak_hours_end' => 17,    // 5 PM
        ],
        'mailgun' => [
            'threshold' => 60,
            'timeout' => 120,
            'min_requests' => 10,
        ],
    ],

    // Cache prefix — change if multiple apps share the same Redis instance
    'cache' => [
        'prefix' => env('FUSE_CACHE_PREFIX', 'fuse'),
    ],
];
```

---

Peak Hours
----------

[](#peak-hours)

Configure different thresholds for business hours when every transaction matters:

```
'stripe' => [
    'threshold' => 40,              // Off-peak: more sensitive (40%)
    'peak_hours_threshold' => 60,   // Peak hours: more tolerant (60%)
    'peak_hours_start' => 9,        // 9 AM
    'peak_hours_end' => 17,         // 5 PM
],
```

During peak hours (9 AM - 5 PM), the circuit uses the higher threshold to maximize successful transactions. Outside peak hours, it uses the lower threshold for earlier protection.

---

Intelligent Failure Classification
----------------------------------

[](#intelligent-failure-classification)

Not all errors indicate a service is down. Fuse only counts real outages:

Error TypeCounted as Failure?Reason500, 502, 503YesServer errors indicate service problemsConnection timeoutYesService is unreachableConnection refusedYesService is unreachable429 Too Many RequestsNoService is healthy, just rate limiting401 UnauthorizedNoYour API key is wrong, not a service issue403 ForbiddenNoPermission issue, not a service outage400 Bad RequestYesCould indicate API issues404 Not FoundYesCould indicate API changesThis prevents false positives. A rate limit doesn't mean Stripe is down - it means you're sending too many requests.

---

Custom Failure Classification
-----------------------------

[](#custom-failure-classification)

The default behavior works well for most APIs, but some services deviate from HTTP standards. For example, Stripe returns `500` for idempotency errors that are actually client-side issues — not outages.

You can override the failure classification logic per service by setting the `failure_classifier` option in your service config:

```
// config/fuse.php

'services' => [
    'stripe' => [
        'threshold' => 50,
        'timeout' => 30,
        'min_requests' => 5,
        'failure_classifier' => \App\Fuse\StripeFailureClassifier::class,
    ],
],
```

### Extending the Default Classifier

[](#extending-the-default-classifier)

The easiest approach is to extend `DefaultFailureClassifier` and override specific cases:

```
namespace App\Fuse;

use GuzzleHttp\Exception\ServerException;
use Harris21\Fuse\Classifiers\DefaultFailureClassifier;
use Throwable;

class StripeFailureClassifier extends DefaultFailureClassifier
{
    public function shouldCount(Throwable $e): bool
    {
        // Stripe returns 500 for idempotency errors — not a real outage
        if ($e instanceof ServerException) {
            $body = (string) $e->getResponse()?->getBody();

            if (str_contains($body, 'idempotency')) {
                return false;
            }
        }

        return parent::shouldCount($e);
    }
}
```

### Implementing the Interface from Scratch

[](#implementing-the-interface-from-scratch)

For full control, implement `FailureClassifier` directly:

```
namespace App\Fuse;

use Harris21\Fuse\Contracts\FailureClassifier;
use Throwable;

class CustomFailureClassifier implements FailureClassifier
{
    public function shouldCount(Throwable $e): bool
    {
        // Your classification logic
    }
}
```

When no `failure_classifier` is configured, Fuse uses `DefaultFailureClassifier` which preserves the behavior described in the table above.

---

Events
------

[](#events)

Fuse dispatches Laravel events on every state transition:

```
use Harris21\Fuse\Events\CircuitBreakerOpened;
use Harris21\Fuse\Events\CircuitBreakerHalfOpen;
use Harris21\Fuse\Events\CircuitBreakerClosed;
```

### Listening to Events

[](#listening-to-events)

```
// app/Listeners/AlertOnCircuitOpen.php

class AlertOnCircuitOpen
{
    public function handle(CircuitBreakerOpened $event): void
    {
        Log::critical("Circuit breaker opened for {$event->service}", [
            'failure_rate' => $event->failureRate,
            'attempts' => $event->attempts,
            'failures' => $event->failures,
        ]);

        // Send Slack notification, page on-call, etc.
    }
}
```

### Event Properties

[](#event-properties)

**CircuitBreakerOpened:**

- `$service` — The service name (e.g., "stripe")
- `$failureRate` — Current failure percentage
- `$attempts` — Total requests in the window
- `$failures` — Failed requests in the window

**CircuitBreakerHalfOpen:**

- `$service` — The service name

**CircuitBreakerClosed:**

- `$service` — The service name

---

Status Page
-----------

[](#status-page)

Fuse includes a real-time monitoring dashboard that shows the state of all your circuit breakers.

 [![Fuse Status Page](art/status-page.png)](art/status-page.png)

### Enable the Status Page

[](#enable-the-status-page)

Add to your `.env`:

```
FUSE_STATUS_PAGE_ENABLED=true
```

The status page is available at `/fuse` (configurable via `FUSE_STATUS_PAGE_PREFIX`).

### Authorization

[](#authorization)

Access is controlled by a `viewFuse` gate. By default, only the `local` environment is allowed. Override it in your `AppServiceProvider`:

```
use Illuminate\Support\Facades\Gate;

Gate::define('viewFuse', function ($user = null) {
    return $user?->isAdmin();
});
```

### Configuration

[](#configuration-1)

```
// config/fuse.php

'status_page' => [
    'enabled' => env('FUSE_STATUS_PAGE_ENABLED', false),
    'prefix' => env('FUSE_STATUS_PAGE_PREFIX', 'fuse'),
    'middleware' => [],          // Custom middleware (replaces default)
    'polling_interval' => 2,    // Frontend refresh interval in seconds
],
```

### What It Shows

[](#what-it-shows)

- **Circuit state** for each configured service (CLOSED, OPEN, HALF-OPEN)
- **State history** with timestamped transitions
- **Live stats** — attempts, failures, failure rate per window
- **Recovery info** — when the circuit opened and when it will test recovery
- **Auto-refresh** — polls the backend every 2 seconds (configurable)

---

Artisan Commands
----------------

[](#artisan-commands)

Fuse includes CLI commands for inspecting and manually controlling circuit breakers.

### Check circuit status

[](#check-circuit-status)

```
php artisan fuse:status           # all services
php artisan fuse:status stripe    # single service
```

Outputs a table with the current state, failure rate, request counts, and threshold for each circuit.

### Reset a circuit

[](#reset-a-circuit)

```
php artisan fuse:reset            # all services
php artisan fuse:reset stripe     # single service
```

Resets the circuit to CLOSED state and clears all stats for the current window.

### Manually open a circuit

[](#manually-open-a-circuit)

```
php artisan fuse:open stripe
```

Forces the circuit OPEN immediately. Useful when you know a service is down and want to protect your queue before failures accumulate. The circuit will recover automatically after the configured `timeout`.

### Manually close a circuit

[](#manually-close-a-circuit)

```
php artisan fuse:close stripe
```

Forces the circuit CLOSED immediately. Useful when a service has recovered but the circuit hasn't timed out yet.

---

Fallback Strategies
-------------------

[](#fallback-strategies)

When the circuit opens, your application needs a plan. Here are common strategies:

**Return cached data** — Show last known prices, cached shipping rates, or stale product info. Slightly stale data beats an error page.

**Use a fallback service** — Switch to a backup payment provider, or show "payment pending" and queue it for later.

**Queue for later** — Fuse already does this with `release()`. For synchronous requests, dispatch a job to retry when the circuit closes.

**Graceful degradation** — Hide the feature entirely. Can't load recommendations? Don't show that section. The page still works.

---

Direct Usage
------------

[](#direct-usage)

Use the circuit breaker directly outside of jobs:

```
use Harris21\Fuse\CircuitBreaker;

$breaker = new CircuitBreaker('stripe');

if (!$breaker->isOpen()) {
    try {
        $result = Stripe::charges()->create([...]);
        $breaker->recordSuccess();
        return $result;
    } catch (Exception $e) {
        $breaker->recordFailure($e);
        throw $e;
    }
} else {
    // Circuit is open - use fallback
    return $this->fallbackResponse();
}
```

### Check Circuit State

[](#check-circuit-state)

```
$breaker = new CircuitBreaker('stripe');

$breaker->isClosed();    // Normal operations
$breaker->isOpen();      // Protected, failing fast
$breaker->isHalfOpen();  // Testing recovery

$breaker->getStats();    // Get full statistics
$breaker->reset();       // Manually reset to closed
```

---

Requirements
------------

[](#requirements)

- PHP 8.3+
- Laravel 11+
- Redis recommended for production, file cache may have race conditions during recovery probing

---

Credits
-------

[](#credits)

Built by [Harris Raftopoulos](https://x.com/harrisrafto) for [Laracon India 2026](https://laracon.in).

YouTube: [@harrisrafto](https://youtube.com/@harrisrafto)

Based on the circuit breaker pattern from Michael Nygard's *Release It!* and popularized by Martin Fowler.

---

License
-------

[](#license)

MIT

###  Health Score

51

—

FairBetter than 96% of packages

Maintenance84

Actively maintained with recent releases

Popularity46

Moderate usage in the ecosystem

Community17

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 53.1% 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 ~3 days

Total

9

Last Release

80d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0fcd0f4186fd1784c24293005e0c1468bb229f8a07f6b502b72fb8f034a284b9?d=identicon)[harris21](/maintainers/harris21)

---

Top Contributors

[![harris21](https://avatars.githubusercontent.com/u/1542015?v=4)](https://github.com/harris21 "harris21 (26 commits)")[![Button99](https://avatars.githubusercontent.com/u/56029580?v=4)](https://github.com/Button99 "Button99 (15 commits)")[![Copilot](https://avatars.githubusercontent.com/in/1143301?v=4)](https://github.com/Copilot "Copilot (4 commits)")[![geangontijo](https://avatars.githubusercontent.com/u/64979293?v=4)](https://github.com/geangontijo "geangontijo (2 commits)")[![lloricode](https://avatars.githubusercontent.com/u/8251344?v=4)](https://github.com/lloricode "lloricode (1 commits)")[![superbiche](https://avatars.githubusercontent.com/u/2478146?v=4)](https://github.com/superbiche "superbiche (1 commits)")

---

Tags

circuit-breakerlaravelqueuesresiliencelaravelqueuejobscircuit breakerfuseresilience

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/harris21-laravel-fuse/health.svg)

```
[![Health](https://phpackages.com/badges/harris21-laravel-fuse/health.svg)](https://phpackages.com/packages/harris21-laravel-fuse)
```

###  Alternatives

[croustibat/filament-jobs-monitor

Background Jobs monitoring like Horizon for all drivers for FilamentPHP

254255.2k6](/packages/croustibat-filament-jobs-monitor)[mateffy/laravel-job-progress

Track and show progress of your background jobs (for progress bar UIs etc.)

451.2k](/packages/mateffy-laravel-job-progress)

PHPackages © 2026

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