Skip to content

Commit b3ca0ea

Browse files
committed
Queue summary for ohdear
1 parent 02b4c14 commit b3ca0ea

File tree

4 files changed

+60
-19
lines changed

4 files changed

+60
-19
lines changed

src/Controllers/ToolsController.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,37 @@ class ToolsController extends Controller
1111
{
1212
public function ohdear(QueueSummary $queueSummary): JsonResponse
1313
{
14+
$results = $queueSummary->all();
15+
$treshold = 100;
16+
1417
return response()->json([
1518
'finishedAt' => now()->timestamp,
16-
'checkResults' => $queueSummary->all()->map(function ($queue) {
19+
'checkResults' => $results->map(function ($queue) use ($treshold) {
1720
$name = $queue[0]->queue;
1821
$count = $queue->sum('count');
19-
$status = $count > 100 ? 'warning' : 'ok';
22+
$status = $count > $treshold ? 'warning' : 'ok';
23+
$notificationMessage = 'Queue is fine';
24+
25+
if ($status === 'warning') {
26+
$notificationMessage = "Queue ($name) seems to be filling up";
27+
}
2028

2129
return [
2230
'name' => "Queue $name",
23-
'label' => "queue",
31+
'label' => $name,
2432
'status' => $status,
25-
'notificationMessage' => "Queue ($name) seems to be filling up",
26-
'shortSummary' => "$count jobs",
27-
'meta' => $queue->map(function ($event) {
28-
$eventName = str_replace('\\\\', '\\', (trim($event->event, '"')));
29-
30-
return [
31-
'event' => $eventName,
32-
'count' => $event->count,
33-
];
33+
'notificationMessage' => $notificationMessage,
34+
'shortSummary' => "$count / $treshold jobs",
35+
'meta' => $queue->mapWithKeys(function ($event) {
36+
if (isset($event->event)) {
37+
$eventName = str_replace('\\\\', '\\', (trim($event->event, '"')));
38+
39+
return [
40+
$eventName => $event->count,
41+
];
42+
}
43+
44+
return [];
3445
})
3546
];
3647
}),

src/Services/Queue/DatabaseQueueService.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
use Illuminate\Support\Facades\DB;
77
use Programic\Tools\Contracts\QueueSummary;
88

9-
class DatabaseQueueService implements QueueSummary
9+
class DatabaseQueueService extends QueueService implements QueueSummary
1010
{
1111
public function all(): Collection
1212
{
13-
return DB::table('queue_jobs')
14-
->select(DB::raw('COUNT(0) as count, queue, JSON_EXTRACT(payload, "$.displayName") as event'))
15-
->groupByRaw('queue, JSON_EXTRACT(payload, "$.displayName")')
16-
->get()
17-
->groupBy('queue');
13+
return $this->format(
14+
DB::table('queue_jobs')
15+
->select(DB::raw('COUNT(0) as count, queue, JSON_EXTRACT(payload, "$.displayName") as event'))
16+
->where('available_at', '>', DB::raw('UNIX_TIMESTAMP(NOW())'))
17+
->groupByRaw('queue, JSON_EXTRACT(payload, "$.displayName")')
18+
->get()
19+
->groupBy('queue')
20+
);
1821
}
1922
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Programic\Tools\Services\Queue;
4+
5+
use Illuminate\Support\Collection;
6+
use Illuminate\Support\Facades\DB;
7+
use Programic\Tools\Contracts\QueueSummary;
8+
9+
abstract class QueueService
10+
{
11+
public function format(Collection $items): Collection
12+
{
13+
if ($items->count() > 0) {
14+
return $items;
15+
}
16+
17+
$stdClass = new \StdClass();
18+
$stdClass->count = 0;
19+
$stdClass->queue = 'default';
20+
21+
return collect([
22+
'queue' => collect([
23+
$stdClass,
24+
]),
25+
]);
26+
}
27+
}

src/Services/Queue/RedisQueueService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Illuminate\Support\Facades\DB;
77
use Programic\Tools\Contracts\QueueSummary;
88

9-
class RedisQueueService implements QueueSummary
9+
class RedisQueueService extends QueueService implements QueueSummary
1010
{
1111
public function all(): Collection
1212
{

0 commit comments

Comments
 (0)