Skip to content

Commit

Permalink
Add queue worker health check
Browse files Browse the repository at this point in the history
  • Loading branch information
NFarrington committed Mar 5, 2019
1 parent 5f98cca commit 766940d
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
80 changes: 80 additions & 0 deletions app/Console/Commands/QueueHealth.php
@@ -0,0 +1,80 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Contracts\Queue\Queue;

class QueueHealth extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'queue:health';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Get the health of the queue.';

/**
* The cache service.
*
* @var \Illuminate\Contracts\Cache\Repository
*/
protected $cache;

/**
* The queue service.
*
* @var \Illuminate\Contracts\Queue\Queue
*/
protected $queue;

/**
* Create a new command instance.
*
* @param \Illuminate\Contracts\Cache\Repository $cache
* @param \Illuminate\Contracts\Queue\Queue $queue
* @return void
*/
public function __construct(Cache $cache, Queue $queue)
{
parent::__construct();

$this->cache = $cache;
$this->queue = $queue;
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$queueSize = $this->queue->size();
if ($queueSize == 0) {
$this->line('The queue is empty.');

return 0;
}

/** @var \Illuminate\Support\Carbon $lastProcessed */
$lastProcessed = $this->cache->get('queue.job.last-processed');
if (!$lastProcessed || $lastProcessed->diffInSeconds() > 30) {
$this->line("The queue has $queueSize items, and appears to be stale.");

return 1;
}

echo "The queue has $queueSize items, and is processing.";

return 0;
}
}
39 changes: 39 additions & 0 deletions app/Listeners/RecordJobProcessingListener.php
@@ -0,0 +1,39 @@
<?php

namespace App\Listeners;

use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Support\Carbon;

class RecordJobProcessingListener
{
/**
* The cache service.
*
* @var \Illuminate\Contracts\Cache\Repository
*/
protected $cache;

/**
* Create the event listener.
*
* @param \Illuminate\Contracts\Cache\Repository $cache
* @return void
*/
public function __construct(Cache $cache)
{
$this->cache = $cache;
}

/**
* Handle the event.
*
* @param \Illuminate\Queue\Events\JobProcessing $event
* @return void
*/
public function handle(JobProcessing $event)
{
$this->cache->forever('queue.job.last-processed', Carbon::now());
}
}
6 changes: 6 additions & 0 deletions app/Providers/EventServiceProvider.php
Expand Up @@ -7,8 +7,10 @@
use App\Events\PrefixApplicationCreatedEvent;
use App\Listeners\DeleteEmailVerificationListener;
use App\Listeners\NotifyApplicationSubmittedListener;
use App\Listeners\RecordJobProcessingListener;
use App\Listeners\VerifyEmailListener;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Queue\Events\JobProcessing;

class EventServiceProvider extends ServiceProvider
{
Expand All @@ -26,6 +28,10 @@ class EventServiceProvider extends ServiceProvider
DeleteEmailVerificationListener::class,
],

JobProcessing::class => [
RecordJobProcessingListener::class,
],

PrefixApplicationCreatedEvent::class => [
NotifyApplicationSubmittedListener::class,
],
Expand Down
5 changes: 5 additions & 0 deletions docker-compose.yml
Expand Up @@ -34,6 +34,11 @@ services:
volumes:
- app-storage:/var/www/storage
env_file: .env.docker
healthcheck:
test: php artisan queue:health || exit 1
interval: 30s
timeout: 10s
start_period: 15s

volumes:
app-storage:

0 comments on commit 766940d

Please sign in to comment.