1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Laravel Paket. |
5
|
|
|
* |
6
|
|
|
* (c) Anton Komarev <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Cog\Laravel\Paket\Http\Controllers\Api\Statuses; |
15
|
|
|
|
16
|
|
|
use Cog\Laravel\Paket\Support\Queue\Events\QueueHasBeenPinged; |
17
|
|
|
use Cog\Laravel\Paket\Support\Queue\Listeners\QueuePingListener; |
18
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
19
|
|
|
use Illuminate\Support\Facades\File; |
20
|
|
|
use Laravel\Horizon\Contracts\MasterSupervisorRepository; |
21
|
|
|
use Ramsey\Uuid\Uuid; |
22
|
|
|
|
23
|
|
|
final class CollectAction |
24
|
|
|
{ |
25
|
|
|
public function __invoke() |
26
|
|
|
{ |
27
|
|
|
return [ |
28
|
|
|
'queue' => $this->resolveQueueStatus(), |
29
|
|
|
]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
private function resolveQueueStatus(): array |
33
|
|
|
{ |
34
|
|
|
if (interface_exists(MasterSupervisorRepository::class)) { |
35
|
|
|
$horizonQueueStatus = $this->resolveHorizonQueueStatus(); |
36
|
|
|
if ($horizonQueueStatus !== 'inactive') { |
37
|
|
|
return [ |
38
|
|
|
'handler' => 'Horizon', |
39
|
|
|
'status' => $horizonQueueStatus, |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return [ |
45
|
|
|
'handler' => 'Illuminate', |
46
|
|
|
'status' => $this->resolveIlluminateQueueStatus(), |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function resolveHorizonQueueStatus(): string |
51
|
|
|
{ |
52
|
|
|
if (!$masters = app(MasterSupervisorRepository::class)->all()) { |
53
|
|
|
return 'inactive'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return collect($masters)->contains(function ($master) { |
57
|
|
|
return $master->status === 'paused'; |
58
|
|
|
}) ? 'paused' : 'running'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function resolveIlluminateQueueStatus(): string |
62
|
|
|
{ |
63
|
|
|
$uuid = Uuid::uuid4()->toString(); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$dispatcher = app(Dispatcher::class); |
66
|
|
|
$dispatcher->listen(QueueHasBeenPinged::class, QueuePingListener::class); |
67
|
|
|
$dispatcher->dispatch(new QueueHasBeenPinged($uuid)); |
68
|
|
|
sleep(2); |
69
|
|
|
|
70
|
|
|
$storedUuid = File::get(storage_path('paket/queue-ping.log')); |
71
|
|
|
|
72
|
|
|
return $uuid === $storedUuid ? 'running' : 'inactive'; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.