/
CheckCommand.php
79 lines (63 loc) · 2.03 KB
/
CheckCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace Laravel\Pulse\Commands;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterval;
use Illuminate\Console\Command;
use Illuminate\Contracts\Cache\LockProvider;
use Illuminate\Events\Dispatcher;
use Illuminate\Support\Sleep;
use Laravel\Pulse\Events\IsolatedBeat;
use Laravel\Pulse\Events\SharedBeat;
use Laravel\Pulse\Pulse;
use Laravel\Pulse\Support\CacheStoreResolver;
use Symfony\Component\Console\Attribute\AsCommand;
/**
* @internal
*/
#[AsCommand(name: 'pulse:check')]
class CheckCommand extends Command
{
/**
* The command's signature.
*
* @var string
*/
public $signature = 'pulse:check';
/**
* The command's description.
*
* @var string
*/
public $description = 'Take a snapshot of the current server\'s pulse';
/**
* Handle the command.
*/
public function handle(
Pulse $pulse,
CacheStoreResolver $cache,
Dispatcher $event,
): int {
$lastRestart = $cache->store()->get('laravel:pulse:restart');
$interval = CarbonInterval::seconds(5);
$lastSnapshotAt = CarbonImmutable::now()->floorSeconds((int) $interval->totalSeconds);
$lock = ($store = $cache->store()->getStore()) instanceof LockProvider
? $store->lock('laravel:pulse:check', (int) $interval->totalSeconds)
: null;
while (true) {
$now = CarbonImmutable::now();
if ($now->subSeconds((int) $interval->totalSeconds)->lessThan($lastSnapshotAt)) {
Sleep::for(500)->milliseconds();
continue;
}
if ($lastRestart !== $cache->store()->get('laravel:pulse:restart')) {
return self::SUCCESS;
}
$lastSnapshotAt = $now->floorSeconds((int) $interval->totalSeconds);
if ($lock?->get()) {
$event->dispatch(new IsolatedBeat($lastSnapshotAt, $interval));
}
$event->dispatch(new SharedBeat($lastSnapshotAt, $interval));
$pulse->ingest();
}
}
}