Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f98cca
commit 766940d
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters