Skip to content
Open
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
4 changes: 4 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
use Illuminate\Support\Facades\Route;

Route::prefix('api')->namespace('Api')->name('paket.api.')->group(function () {
Route::get('statuses')
->uses('Statuses\CollectAction')
->name('statuses.collect');

Route::get('repositories')
->uses('Repositories\CollectAction')
->name('repositories.collect');
Expand Down
86 changes: 86 additions & 0 deletions src/Http/Controllers/Api/Statuses/CollectAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/*
* This file is part of Laravel Paket.
*
* (c) Anton Komarev <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Cog\Laravel\Paket\Http\Controllers\Api\Statuses;

use Cog\Laravel\Paket\Support\Queue\Events\QueueHasBeenPinged;
use Cog\Laravel\Paket\Support\Queue\Listeners\QueuePingListener;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Facades\File;
use Laravel\Horizon\Contracts\MasterSupervisorRepository;
use Ramsey\Uuid\Uuid;

final class CollectAction
{
public function __invoke()
{
return [
'queue' => $this->resolveQueueStatus(),
];
}

private function resolveQueueStatus(): array
{
$queueConnection = config('queue.default');

if ($queueConnection === 'sync') {
return [
'handler' => 'Illuminate',
'connection' => $queueConnection,
'status' => 'inactive',
];
}

if (interface_exists(MasterSupervisorRepository::class)) {
$horizonQueueStatus = $this->resolveHorizonQueueStatus();
if ($horizonQueueStatus !== 'inactive') {
return [
'handler' => 'Horizon',
'connection' => $queueConnection,
'status' => $horizonQueueStatus,
];
}
}

return [
'handler' => 'Illuminate',
'connection' => $queueConnection,
'status' => $this->resolveIlluminateQueueStatus(),
];
}

private function resolveHorizonQueueStatus(): string
{
if (!$masters = app(MasterSupervisorRepository::class)->all()) {
return 'inactive';
}

return collect($masters)->contains(function ($master) {
return $master->status === 'paused';
}) ? 'paused' : 'running';
}

private function resolveIlluminateQueueStatus(): string
{
$uuid = Uuid::uuid4()->toString();

$dispatcher = app(Dispatcher::class);
$dispatcher->listen(QueueHasBeenPinged::class, QueuePingListener::class);
$dispatcher->dispatch(new QueueHasBeenPinged($uuid));
sleep(2);

$storedUuid = File::get(storage_path('paket/queue-ping.log'));

return $uuid === $storedUuid ? 'running' : 'inactive';
}
}
29 changes: 29 additions & 0 deletions src/Support/Queue/Events/QueueHasBeenPinged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of Laravel Paket.
*
* (c) Anton Komarev <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Cog\Laravel\Paket\Support\Queue\Events;

final class QueueHasBeenPinged
{
private $id;

public function __construct(string $id)
{
$this->id = $id;
}

public function getId(): string
{
return $this->id;
}
}
26 changes: 26 additions & 0 deletions src/Support/Queue/Listeners/QueuePingListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of Laravel Paket.
*
* (c) Anton Komarev <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Cog\Laravel\Paket\Support\Queue\Listeners;

use Cog\Laravel\Paket\Support\Queue\Events\QueueHasBeenPinged;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\File;

final class QueuePingListener implements ShouldQueue
{
public function handle(QueueHasBeenPinged $event): void
{
File::put(storage_path('paket/queue-ping.log'), $event->getId());
}
}