Skip to content

Commit

Permalink
Allow cache store used by console scheduling integration to be overri…
Browse files Browse the repository at this point in the history
…dden (#942)
  • Loading branch information
jnbeaver authored Sep 12, 2024
1 parent 14e828d commit 40fd30e
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/Sentry/Laravel/Features/ConsoleSchedulingIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Console\Application as ConsoleApplication;
use Illuminate\Console\Scheduling\Event as SchedulingEvent;
use Illuminate\Contracts\Cache\Factory as Cache;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Support\Str;
use RuntimeException;
use Sentry\CheckIn;
Expand All @@ -17,6 +18,11 @@

class ConsoleSchedulingIntegration extends Feature
{
/**
* @var string|null
*/
private $cacheStore = null;

/**
* @var array<string, CheckIn> The list of checkins that are currently in progress.
*/
Expand Down Expand Up @@ -109,6 +115,11 @@ public function onBootInactive(): void
$this->shouldHandleCheckIn = false;
}

public function useCacheStore(?string $name): void
{
$this->cacheStore = $name;
}

private function startCheckIn(
?string $slug,
SchedulingEvent $scheduled,
Expand Down Expand Up @@ -148,7 +159,7 @@ private function startCheckIn(
$this->checkInStore[$cacheKey] = $checkIn;

if ($scheduled->runInBackground) {
$this->resolveCache()->store()->put($cacheKey, $checkIn->getId(), $scheduled->expiresAt * 60);
$this->resolveCache()->put($cacheKey, $checkIn->getId(), $scheduled->expiresAt * 60);
}

$this->sendCheckIn($checkIn);
Expand All @@ -169,7 +180,7 @@ private function finishCheckIn(?string $slug, SchedulingEvent $scheduled, CheckI
$checkIn = $this->checkInStore[$cacheKey] ?? null;

if ($checkIn === null && $scheduled->runInBackground) {
$checkInId = $this->resolveCache()->store()->get($cacheKey);
$checkInId = $this->resolveCache()->get($cacheKey);

if ($checkInId !== null) {
$checkIn = $this->createCheckIn($checkInSlug, $status, $checkInId);
Expand All @@ -185,7 +196,7 @@ private function finishCheckIn(?string $slug, SchedulingEvent $scheduled, CheckI
unset($this->checkInStore[$mutex]);

if ($scheduled->runInBackground) {
$this->resolveCache()->store()->forget($cacheKey);
$this->resolveCache()->forget($cacheKey);
}

$checkIn->setStatus($status);
Expand Down Expand Up @@ -237,8 +248,8 @@ private function makeSlugForScheduled(SchedulingEvent $scheduled): string
return "scheduled_{$generatedSlug}";
}

private function resolveCache(): Cache
private function resolveCache(): Repository
{
return $this->container()->make(Cache::class);
return $this->container()->make(Cache::class)->store($this->cacheStore);
}
}

0 comments on commit 40fd30e

Please sign in to comment.