Skip to content

Commit cd4ba85

Browse files
committed
add queue monitor
1 parent b3ca0ea commit cd4ba85

File tree

5 files changed

+141
-2
lines changed

5 files changed

+141
-2
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Programic\Tools\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Programic\Tools\Contracts\AnalyticsClient;
7+
use Programic\Tools\Contracts\QueueSummary;
8+
9+
class SendQueueAnalyticsCommand extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'queue:monitor';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Send queue monitor to 3rd party';
24+
25+
/**
26+
* @var QueueSummary
27+
*/
28+
protected $queueSummary;
29+
30+
/**
31+
* Execute the console command.
32+
*
33+
* @return void
34+
*/
35+
public function handle()
36+
{
37+
$queueSummary = app(QueueSummary::class);
38+
$analyticsClient = app(AnalyticsClient::class);
39+
$results = $queueSummary->all();
40+
41+
$response = $analyticsClient->send($results);
42+
43+
44+
if ($response['status'] === 'OK') {
45+
$this->line('<info>' . $response['message'] . '</info>');
46+
47+
return;
48+
}
49+
50+
$this->line('<error>' . $response['message'] . '</error>');
51+
}
52+
}

src/Contracts/AnalyticsClient.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Programic\Tools\Contracts;
4+
5+
use Illuminate\Support\Collection;
6+
7+
interface AnalyticsClient
8+
{
9+
public function __construct($auth);
10+
11+
public function send(Collection $data): array;
12+
}

src/Services/Analytics/Databox.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Programic\Tools\Services\Analytics;
4+
5+
use Illuminate\Support\Collection;
6+
use Programic\Tools\Contracts\AnalyticsClient;
7+
8+
class Databox implements AnalyticsClient
9+
{
10+
private $token;
11+
12+
public function __construct($token)
13+
{
14+
$this->token = $token;
15+
16+
if (!$token) {
17+
throw new \Exception('Databox token must be set');
18+
}
19+
}
20+
21+
public function send(Collection $data): array
22+
{
23+
$postData = [];
24+
25+
$data->each(function ($queue) use (&$postData) {
26+
$name = $queue[0]->queue;
27+
$count = $queue->sum('count');
28+
29+
foreach ($queue as $item) {
30+
$postData[] = [
31+
"\$queue_{$item->queue}" => $item->count,
32+
'event' => str_replace('\\\\', '\\', (trim($item->event, '"'))),
33+
'date' => now()->format('c'),
34+
];
35+
}
36+
});
37+
38+
$ch = curl_init();
39+
curl_setopt($ch, CURLOPT_URL,'https://push.databox.com');
40+
curl_setopt($ch, CURLOPT_POST, 1);
41+
curl_setopt($ch, CURLOPT_USERPWD, "$this->token:");
42+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['data' => $postData]));
43+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
44+
45+
$headers = [
46+
'Accept: application/vnd.databox.v2+json',
47+
'Content-Type: application/json',
48+
];
49+
50+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
51+
52+
$server_output = curl_exec($ch);
53+
54+
curl_close($ch);
55+
56+
return json_decode($server_output, true);
57+
}
58+
}

src/Services/Queue/DatabaseQueueService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function all(): Collection
1313
return $this->format(
1414
DB::table('queue_jobs')
1515
->select(DB::raw('COUNT(0) as count, queue, JSON_EXTRACT(payload, "$.displayName") as event'))
16-
->where('available_at', '>', DB::raw('UNIX_TIMESTAMP(NOW())'))
16+
// ->where('available_at', '>', DB::raw('UNIX_TIMESTAMP(NOW())'))
1717
->groupByRaw('queue, JSON_EXTRACT(payload, "$.displayName")')
1818
->get()
1919
->groupBy('queue')

src/ToolsServiceProvider.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
namespace Programic\Tools;
44

5+
use Illuminate\Console\Scheduling\Schedule;
56
use Illuminate\Routing\Router;
67
use Illuminate\Support\ServiceProvider;
8+
use Programic\Tools\Console\Commands\SendQueueAnalyticsCommand;
9+
use Programic\Tools\Contracts\AnalyticsClient;
710
use Programic\Tools\Contracts\QueueSummary;
811
use Programic\Tools\Controllers\ToolsController;
912
use Programic\Tools\Middleware\SentryContext;
13+
use Programic\Tools\Services\Analytics\Databox;
1014

1115
class ToolsServiceProvider extends ServiceProvider
1216
{
@@ -36,6 +40,17 @@ public function boot(Router $router)
3640
});
3741

3842
$this->app['router']->get('ohdear-health-check', [ToolsController::class, 'ohdear']);
43+
44+
$databoxToken = config('services.databox.token');
45+
46+
if ($databoxToken) {
47+
$this->app->singleton(AnalyticsClient::class, fn () => new Databox($databoxToken));
48+
49+
$this->app->booted(function () {
50+
$schedule = $this->app->make(Schedule::class);
51+
$schedule->command(SendQueueAnalyticsCommand::class)->everyMinute()->withoutOverlapping();
52+
});
53+
}
3954
}
4055

4156
/**
@@ -45,6 +60,8 @@ public function boot(Router $router)
4560
*/
4661
public function register()
4762
{
48-
// Register magic
63+
$this->commands([
64+
SendQueueAnalyticsCommand::class,
65+
]);
4966
}
5067
}

0 commit comments

Comments
 (0)