Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow cache store used by console scheduling integration to be overridden #942

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
jnbeaver marked this conversation as resolved.
Show resolved Hide resolved

/**
* @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);
}
}