Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/Commands/QueueMonitorCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Doppar\Queue\Commands;

use Phaseolies\Console\Schedule\Command;
use Doppar\Queue\Models\QueueJob;
use Doppar\Queue\Models\FailedJob;

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

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

/**
* Execute the console command.
*
* @return int
*/
protected function handle(): int
{
$queues = QueueJob::groupBy('queue')->pluck('queue');

// Create table for queue statistics
$table = $this->createTable();
$table->setHeaders(['Queue', 'Pending', 'Processing']);

foreach ($queues ?? [] as $queue) {
$pending = QueueJob::where('queue', $queue)
->whereNull('reserved_at')
->count();

$processing = QueueJob::where('queue', $queue)
->whereNotNull('reserved_at')
->count();

$table->addRow([
$queue,
$pending,
$processing,
]);
}

// Render queue table
$this->newLine();
$this->info("Queue Statistics");
$table->render();

// Failed jobs table
$failedCount = FailedJob::count();

$failedTable = $this->createTable();
$failedTable->setHeaders(['Metric', 'Value']);
$failedTable->addRow(['Failed Jobs', $failedCount]);

$this->info("\nFailed Jobs Summary");
$failedTable->render();

return Command::SUCCESS;
}
}
4 changes: 3 additions & 1 deletion src/QueueServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Doppar\Queue\Commands\QueueFlushCommand;
use Doppar\Queue\Commands\QueueFailedCommand;
use Doppar\Queue\Commands\MakeJobCommand;
use Doppar\Queue\Commands\QueueMonitorCommand;

class QueueServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -40,7 +41,8 @@ public function boot(): void
QueueRetryCommand::class,
MakeJobCommand::class,
QueueFlushCommand::class,
QueueFailedCommand::class
QueueFailedCommand::class,
QueueMonitorCommand::class
]);
}
}