Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Feb 13, 2024
1 parent 801ec40 commit dbdaff8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
20 changes: 5 additions & 15 deletions src/Commands/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,26 @@ public function handle(
): 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)
? $store->lock('laravel:pulse:check', 5)
: 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 IsolatedBeat($now));
}

$event->dispatch(new SharedBeat($lastSnapshotAt, $interval));
$event->dispatch(new SharedBeat($now));

$pulse->ingest();

Sleep::for(1)->second();
}
}
}
1 change: 0 additions & 1 deletion src/Events/IsolatedBeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class IsolatedBeat
*/
public function __construct(
public CarbonImmutable $time,
public CarbonInterval $interval,
) {
//
}
Expand Down
1 change: 0 additions & 1 deletion src/Events/SharedBeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class SharedBeat
*/
public function __construct(
public CarbonImmutable $time,
public CarbonInterval $interval,
) {
//
}
Expand Down
26 changes: 25 additions & 1 deletion src/Recorders/Servers.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Str;
use Laravel\Pulse\Events\SharedBeat;
use Laravel\Pulse\Pulse;
use Laravel\Pulse\Support\CacheStoreResolver;
use RuntimeException;

/**
Expand All @@ -25,6 +26,7 @@ class Servers
*/
public function __construct(
protected Pulse $pulse,
protected CacheStoreResolver $cache,
protected Repository $config
) {
//
Expand All @@ -35,7 +37,7 @@ public function __construct(
*/
public function record(SharedBeat $event): void
{
if ($event->time->second % 15 !== 0) {
if (! $this->readyToRecord($event)) {
return;
}

Expand Down Expand Up @@ -81,5 +83,27 @@ public function record(SharedBeat $event): void
])
->all(),
], flags: JSON_THROW_ON_ERROR), $event->time);

$this->cache->store()->forever($this->key(), $event->time->timestamp);
}

/**
* Determine if the recorder is ready to take another snapshot.
*/
protected function readyToRecord(SharedBeat $event): bool
{
return with($this->cache->store()->get($this->key()), function ($lastChecked) use ($event) {
return $lastChecked === null || $lastChecked <= ($event->time->timestamp - 15);
});
}

/**
* The last checked at cache key.
*/
protected function key(): string
{
$slug = Str::slug($this->config->get('pulse.recorders.'.self::class.'.server_name'));

return "laravel:pulse:recorders:servers:{$slug}:last_snapshot_at";
}
}

0 comments on commit dbdaff8

Please sign in to comment.