Ensure your Laravel applications keep a normal pulse

Overview

Laravel Defibrillator

Ensure your Laravel applications keep a normal rhythm

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Laravel Defibrillator helps you ensure that aspects of your application that should be running at a regular interval are doing so.

Installation

You can install the package via composer:

composer require dyrynda/laravel-defibrillator

You can publish the config file with:

php artisan vendor:publish --provider="Dyrynda\Defibrillator\DefibrillatorServiceProvider" --tag="laravel-defibrillator-config"

Usage

When an abnormal heartbeat rhythm is detected, you can defibrillate your heart to get back to normalcy.



if ($this->hasAbnormalRhythm()) {
  $this->defibrillate();

  return;
}

What on Earth?

Consider a scheduled task that communicates with your application users on a regular interval.

This scheduled task queues notifications to your users based on some condition within your application.

In a normal situation, there is a handful of notifications to go out, and they are dispatched with in a few seconds.

But an application error causes your application to spiral out of control.

Queued notifiations back up, your database is not being updated to flag notifications as having been sent, your error tracker floods with exceptions.

And then your scheduled task runs again.

Suddenly your queue has tens of thousands of pending jobs in it and you're stuck in a cycle that you can't keep up with.

Enter Laravel Defibrillator

The Laravel Defibrillator helps you keep track of individual components of your application that are expected to be called on a regular interval.

On each execution, you call the defibrillate() method to update a cache value, setting an acceptable rhythm.

For example;



// app/Console/Kernel.php
$schedule->job(NotifyUsers::class)->everyMinute();

// app/Jobs/NotifyUsers.php
public function handle()
{
  if ($this->hasAbnormalRhythm()) {
    $this->defibrillate();

    return;
  }

  // Regular processing
  $this->defibrillate();
}

By default, calling defibrillate() will put an item into your configured cache with a Carbon instance 90 seconds into the future. The cache key is the basename of the calling class. i.e. the cache key for the App\Jobs\NotifyUsers class will be NotifyUsers. You can override the heart() method in your class if you wish to have more control over this.

Within the realm of normal operation, your scheduled task will execute every 60 seconds and push the cached value another 90 seconds.

However, should your database become overwhelmed, or your queues full of backlogged email notifications, and your scheduled task misses an execution and the cached value is in the past, instead of adding further strain to the database or pushing more notifications on to the queue, the Defibrillator will instead push the cache value out another 90 seconds.

In doing so, you give your database a chance to keep up with the queue backlog without manual intervention.

You can even prevent lagging notifications from being sent should the hearbeat enter an abnormal rhythm between when the scheduled task ran and the notification is trying to be sent:


// app/Notifications/CustomerNotification.php
public function shouldSend(): bool
{
  return Cache::get('NotifyUsers')?->isFuture() ?? false;
}

How will I know this is happening?

It is beyond the scope of this package, however, you might consider conditionally dispatching a notification if you breach a threshold in abnormal rhythm.

heart()}:skipped") === 3); return; } // Regular processing $this->defibrillate(); + + Cache::forget("{$this->heart()}:skipped"); } ">
hasAbnormalRhythm()) {
    $this->defibrillate();
+
+   CardiacEvent::dispatchIf(Cache::increment("{$this->heart()}:skipped") === 3);

    return;
  }

  // Regular processing
  $this->defibrillate();
+
+ Cache::forget("{$this->heart()}:skipped");
}

This way, if you have 3 consecutive defibrillations, you can dispatch an email, SMS, Slack, whatever notification to get on the case!

Alternatively, you might consider a scheduled task monitoring solution such as thenping.me.

Configuration

You can define the default interval of a normal heartbeat by setting the defibrillator.interval config value or override it per-class by adding an interval method where you are using the Defibrillator trait:



use Dyrynda\Defibrillator\Defibrillator;

class Artisan
{
  use Defibrillator;

  public function interval(): int
  {
    return 30;
  }
}

Support development

If you would like to support the on going maintenance and development of this package, please consider sponsoring me on GitHub.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

You might also like...
1Pilot.io, a universal dashboard to effortlessly manage all your Laravel applications
1Pilot.io, a universal dashboard to effortlessly manage all your Laravel applications

Website · Free Trial · Pricing · Documentation · API · Feedback · Support [You] What are you, strange being? [1Pilot] Greetings, traveller. I am 1Pilo

Test your Laravel applications with phpspec

phpspec Laravel Extension phpspec extension for testing Laravel applications. Versions Depending on the version of Laravel and/or Phpspec you're using

Jumpstart your web development journey with the HALT Stack Starter Kit, a one-command solution for creating dynamic, scalable, and clean web applications.

Welcome to the HALT Stack Starter Kit! This kit is designed to help you kickstart your web development projects using the HALT Stack, a powerful combi

Nebula is a minimalistic and easy to use administration tool for Laravel applications, made with Laravel, Alpine.js, and Tailwind CSS.

Nebula Nebula is a minimalistic and easy to use administration tool for Laravel applications, made with Laravel, Alpine.js, and Tailwind CSS. Nebula m

⛽ Set of utilities to test Laravel applications powered by Octane.

⛽ Octane Testbench Set of utilities to test Laravel applications powered by Octane. Install Via Composer: composer require --dev cerbero/octane-testbe

A simple `make:view` command for Laravel applications.

A simple make:view command for Laravel applications. Quickly generate a new Blade view from the console using artisan make:view. Installation You can

Laravel package integrating PHP Flasher into Livewire applications
Laravel package integrating PHP Flasher into Livewire applications

A powerful and flexible flash notifications system for PHP, Laravel, Symfony 👀 PHP Flasher helps you to add flash notifications to your PHP projects.

A premade, easy to use local development setup to be used for authoring Laravel applications

Laravel Drydock This project is a premade, easy to use local development setup to be used for authoring Laravel applications. The deliverables of this

An in-app database management UI for Laravel applications. ⚡️

:package_description This repo can be used to scaffold a Laravel package. Follow these steps to get started: Press the "Use template" button at the to

Releases(0.2.0)
Owner
Michael Dyrynda
Michael Dyrynda
A package to keep track of your pages & understand your audience

A clean way to track your pages & understand your user's behavior Installation You can install the package via composer: composer require coderflexx/l

Coderflex 178 Jan 4, 2023
A package to keep track of outgoing emails in your Laravel application.

Keep track of outgoing emails and associate sent emails with Eloquent models This package helps you to keep track of outgoing emails in your Laravel a

Stefan Zweifel 108 Nov 1, 2022
Laravel Seeable - Keep track of the date and time a user was last seen.

Laravel Seeable This package makes it easy to keep track of the date and time a user was last seen. Installation Install this package. composer requir

Zep Fietje 29 Dec 26, 2022
🧑‍🔬 The missing assertions for your views in your Laravel applications.

Laravel View Assertions The missing assertions for your views in your Laravel applications. Installation You'll have to follow a couple of simple step

Sven Luijten 4 Dec 21, 2022
Laravel Boilerplate provides a very flexible and extensible way of building your custom Laravel applications.

Laravel Boilerplate Project Laravel Boilerplate provides a very flexible and extensible way of building your custom Laravel applications. Table of Con

Labs64 848 Dec 28, 2022
This package lets you add uuid as primary key in your laravel applications

laravel-model-uuid A Laravel package to add uuid to models Table of contents Installation Configuration Model Uuid Publishing files / configurations I

salman zafar 10 May 17, 2022
Quickly identify controller methods with no route in your Laravel applications.

Orphan Controller Quickly identify controller methods with no route in your Laravel applications. Installation You can install the package via Compose

Ryan Chandler 16 Feb 18, 2022
Run patches migration style in your Laravel applications.

This package generates patch files in the same fashion Laravel generates migrations. Each file is timestamped with an up and a down method and is asso

Anthony Rappa 44 Sep 9, 2022
Zarinpal is a laravel package to easily use zarinpal.com payment services in your applications

پکیج اتصال به درگاه پرداخت زرین پال zarinpal.com برای اتصال به درگاه پرداخت اینترنتی زرین پال و استفاده از api های آن می توانید از این پکیج استفاده کن

Rahmat Waisi 4 Jan 26, 2022
Integrate Astrel to your Laravel applications.

✨ Astrel Laravel Integrate Astrel to your Laravel applications. Astrel is a remote config orchestration application that enables you to change anythin

Sustainable Hustle 3 Jan 9, 2022