/
QueueHealth.php
80 lines (66 loc) · 1.73 KB
/
QueueHealth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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;
}
}