Skip to content

Commit dbdaff8

Browse files
committed
wip
1 parent 801ec40 commit dbdaff8

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

src/Commands/CheckCommand.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,26 @@ public function handle(
4444
): int {
4545
$lastRestart = $cache->store()->get('laravel:pulse:restart');
4646

47-
$interval = CarbonInterval::seconds(5);
48-
49-
$lastSnapshotAt = CarbonImmutable::now()->floorSeconds((int) $interval->totalSeconds);
50-
5147
$lock = ($store = $cache->store()->getStore()) instanceof LockProvider
52-
? $store->lock('laravel:pulse:check', (int) $interval->totalSeconds)
48+
? $store->lock('laravel:pulse:check', 5)
5349
: null;
5450

5551
while (true) {
5652
$now = CarbonImmutable::now();
5753

58-
if ($now->subSeconds((int) $interval->totalSeconds)->lessThan($lastSnapshotAt)) {
59-
Sleep::for(500)->milliseconds();
60-
61-
continue;
62-
}
63-
6454
if ($lastRestart !== $cache->store()->get('laravel:pulse:restart')) {
6555
return self::SUCCESS;
6656
}
6757

68-
$lastSnapshotAt = $now->floorSeconds((int) $interval->totalSeconds);
69-
7058
if ($lock?->get()) {
71-
$event->dispatch(new IsolatedBeat($lastSnapshotAt, $interval));
59+
$event->dispatch(new IsolatedBeat($now));
7260
}
7361

74-
$event->dispatch(new SharedBeat($lastSnapshotAt, $interval));
62+
$event->dispatch(new SharedBeat($now));
7563

7664
$pulse->ingest();
65+
66+
Sleep::for(1)->second();
7767
}
7868
}
7969
}

src/Events/IsolatedBeat.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class IsolatedBeat
1212
*/
1313
public function __construct(
1414
public CarbonImmutable $time,
15-
public CarbonInterval $interval,
1615
) {
1716
//
1817
}

src/Events/SharedBeat.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class SharedBeat
1212
*/
1313
public function __construct(
1414
public CarbonImmutable $time,
15-
public CarbonInterval $interval,
1615
) {
1716
//
1817
}

src/Recorders/Servers.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\Str;
77
use Laravel\Pulse\Events\SharedBeat;
88
use Laravel\Pulse\Pulse;
9+
use Laravel\Pulse\Support\CacheStoreResolver;
910
use RuntimeException;
1011

1112
/**
@@ -25,6 +26,7 @@ class Servers
2526
*/
2627
public function __construct(
2728
protected Pulse $pulse,
29+
protected CacheStoreResolver $cache,
2830
protected Repository $config
2931
) {
3032
//
@@ -35,7 +37,7 @@ public function __construct(
3537
*/
3638
public function record(SharedBeat $event): void
3739
{
38-
if ($event->time->second % 15 !== 0) {
40+
if (! $this->readyToRecord($event)) {
3941
return;
4042
}
4143

@@ -81,5 +83,27 @@ public function record(SharedBeat $event): void
8183
])
8284
->all(),
8385
], flags: JSON_THROW_ON_ERROR), $event->time);
86+
87+
$this->cache->store()->forever($this->key(), $event->time->timestamp);
88+
}
89+
90+
/**
91+
* Determine if the recorder is ready to take another snapshot.
92+
*/
93+
protected function readyToRecord(SharedBeat $event): bool
94+
{
95+
return with($this->cache->store()->get($this->key()), function ($lastChecked) use ($event) {
96+
return $lastChecked === null || $lastChecked <= ($event->time->timestamp - 15);
97+
});
98+
}
99+
100+
/**
101+
* The last checked at cache key.
102+
*/
103+
protected function key(): string
104+
{
105+
$slug = Str::slug($this->config->get('pulse.recorders.'.self::class.'.server_name'));
106+
107+
return "laravel:pulse:recorders:servers:{$slug}:last_snapshot_at";
84108
}
85109
}

0 commit comments

Comments
 (0)