Skip to content

Commit b4f9bf5

Browse files
authored
Merge pull request #8 from doppar/queue
queue:monitor command
2 parents d14ee72 + 7760b45 commit b4f9bf5

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Doppar\Queue\Commands;
4+
5+
use Phaseolies\Console\Schedule\Command;
6+
use Doppar\Queue\Models\QueueJob;
7+
use Doppar\Queue\Models\FailedJob;
8+
9+
class QueueMonitorCommand extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $name = 'queue:monitor';
17+
18+
/**
19+
* The description of the console command.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Monitor queue statistics';
24+
25+
/**
26+
* Execute the console command.
27+
*
28+
* @return int
29+
*/
30+
protected function handle(): int
31+
{
32+
$queues = QueueJob::groupBy('queue')->pluck('queue');
33+
34+
// Create table for queue statistics
35+
$table = $this->createTable();
36+
$table->setHeaders(['Queue', 'Pending', 'Processing']);
37+
38+
foreach ($queues ?? [] as $queue) {
39+
$pending = QueueJob::where('queue', $queue)
40+
->whereNull('reserved_at')
41+
->count();
42+
43+
$processing = QueueJob::where('queue', $queue)
44+
->whereNotNull('reserved_at')
45+
->count();
46+
47+
$table->addRow([
48+
$queue,
49+
$pending,
50+
$processing,
51+
]);
52+
}
53+
54+
// Render queue table
55+
$this->newLine();
56+
$this->info("Queue Statistics");
57+
$table->render();
58+
59+
// Failed jobs table
60+
$failedCount = FailedJob::count();
61+
62+
$failedTable = $this->createTable();
63+
$failedTable->setHeaders(['Metric', 'Value']);
64+
$failedTable->addRow(['Failed Jobs', $failedCount]);
65+
66+
$this->info("\nFailed Jobs Summary");
67+
$failedTable->render();
68+
69+
return Command::SUCCESS;
70+
}
71+
}

src/QueueServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Doppar\Queue\Commands\QueueFlushCommand;
1010
use Doppar\Queue\Commands\QueueFailedCommand;
1111
use Doppar\Queue\Commands\MakeJobCommand;
12+
use Doppar\Queue\Commands\QueueMonitorCommand;
1213

1314
class QueueServiceProvider extends ServiceProvider
1415
{
@@ -40,7 +41,8 @@ public function boot(): void
4041
QueueRetryCommand::class,
4142
MakeJobCommand::class,
4243
QueueFlushCommand::class,
43-
QueueFailedCommand::class
44+
QueueFailedCommand::class,
45+
QueueMonitorCommand::class
4446
]);
4547
}
4648
}

0 commit comments

Comments
 (0)