Skip to content

🚑 Crudely, check if your Redis queue worker is running

License

Notifications You must be signed in to change notification settings

f9webltd/laravel-queue-check

Repository files navigation

Latest Stable Version Scrutinizer coverage (GitHub/BitBucket) Scrutinizer code quality (GitHub/Bitbucket) Build Status StyleCI Status License

Laravel Redis Queue Check

A crude and simple Laravel console command to determine if your Redis queue worker is running.

When the command determines that your worker is not running, the event QueueCheckFailed is dispatched.

Behind the scenes, the command ps aux is ran to determine if you expected command is present.

Requirements

PHP >= 7.2, Laravel >= 5.8.

Installation

composer require f9webltd/laravel-queue-check

Publish the configuration file:

vendor:publish --provider="F9Web\QueueCheck\CheckQueueServiceProvider"

Documentation

Configuration Settings

The configuration is extremely basic and allows two expectations to be set.

processes

An integer, the required number of processes that should be present.

expected-output

The target command that should be running.

Some examples:

  • artisan queue:work redis
  • artisan horizon
  • php artisan horizon:work redis --delay=0 --memory=128 --queue=default --sleep=3 --timeout=60 --tries=3 --supervisor=abc:supervisor-1

The specificity is optional.

To determine the output typically ps aux | grep artisan can be ran from the command line. So if the output of which was:

user+ 17981  0.0  0.4 384080 38544 ?        Ss   Jun12   0:31 php /home/user/artisan queue:work redis --queue=live --sleep=3 --tries=3
user+ 18004  0.0  0.4 384080 38548 ?        Ss   Jun12   0:31 php /home/user/artisan queue:work redis --queue=live --sleep=3 --tries=3
user+ 18027  0.0  0.4 384080 38548 ?        Ss   Jun12   0:31 php /home/user/artisan queue:work redis --queue=live --sleep=3 --tries=3

The following configuration values could apply:

  • expected-output = artisan queue:work redis
  • processes = 3

Usage within your application

When desired command or required number of processes is not running, the event QueueCheckFailed os dispatched.

Within your application you can listen for that event.

Define the event listener, in this case QueueFailed.

// App\Providers\EventServiceProvider

protected $listen = [
    \F9Web\QueueCheck\Events\QueueCheckFailed::class => [
        \App\Listeners\QueueFailed::class,
    ],
];

Register the scheduled task f9web:queue-check:

// App\Console\Kernel

protected function schedule(Schedule $schedule) 
{
    $schedule->command('f9web:queue-check')->everyThirtyMinutes();
}

The event returns the console output that caused the command to fail, In the below case, that is simply logged. The listener can of course do anything required, such as sending an email or a Slack notification.

namespace App\Listeners;

use F9Web\QueueCheck\Events\QueueCheckFailed;

class QueueFailed
{
    public function __construct()
    {
        //
    }
    
    public function handle(QueueCheckFailed $event)
    {
        info('Failing', [
            'output' => $event->getOutput(),
        ]);
    }
}

N.B: When running a Redis queue, there is a high chance your application will queue mail. If the queue is down the notification email is queued, the email will never be sent. If sending an email ensure the mail is not queued. If using a "mailable" object the Queueable trait can be omitted.

Contribution

Any ideas are welcome. Feel free to submit any issues or pull requests.

Testing

composer test

Security

If you discover any security related issues, please email rob@f9web.co.uk instead of using the issue tracker.

Credits

License

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