Skip to content

Always register the console scheduling macro #900

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

Merged
merged 1 commit into from
Jun 4, 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
59 changes: 28 additions & 31 deletions src/Sentry/Laravel/Features/ConsoleSchedulingIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Console\Application as ConsoleApplication;
use Illuminate\Console\Scheduling\Event as SchedulingEvent;
use Illuminate\Contracts\Cache\Factory as Cache;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Str;
use RuntimeException;
use Sentry\CheckIn;
Expand All @@ -23,25 +22,10 @@ class ConsoleSchedulingIntegration extends Feature
*/
private $checkInStore = [];

/**
* @var Cache The cache repository.
*/
private $cache;
private $shouldHandleCheckIn = false;

public function register(): void
{
$this->onBootInactive();
}

public function isApplicable(): bool
{
return $this->container()->make(Application::class)->runningInConsole();
}

public function onBoot(Cache $cache): void
{
$this->cache = $cache;

$startCheckIn = function (
?string $slug,
SchedulingEvent $scheduled,
Expand Down Expand Up @@ -110,19 +94,19 @@ public function onBoot(Cache $cache): void
});
}

public function isApplicable(): bool
{
return true;
}

public function onBoot(): void
{
$this->shouldHandleCheckIn = true;
}

public function onBootInactive(): void
{
// This is an exact copy of the macro above, but without doing anything so that even when no DSN is configured the user can still use the macro
SchedulingEvent::macro('sentryMonitor', function (
?string $monitorSlug = null,
?int $checkInMargin = null,
?int $maxRuntime = null,
bool $updateMonitorConfig = true,
?int $failureIssueThreshold = null,
?int $recoveryThreshold = null
) {
return $this;
});
$this->shouldHandleCheckIn = false;
}

private function startCheckIn(
Expand All @@ -134,6 +118,10 @@ private function startCheckIn(
?int $failureIssueThreshold,
?int $recoveryThreshold
): void {
if (!$this->shouldHandleCheckIn) {
return;
}

$checkInSlug = $slug ?? $this->makeSlugForScheduled($scheduled);

$checkIn = $this->createCheckIn($checkInSlug, CheckInStatus::inProgress());
Expand All @@ -160,14 +148,18 @@ private function startCheckIn(
$this->checkInStore[$cacheKey] = $checkIn;

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

$this->sendCheckIn($checkIn);
}

private function finishCheckIn(?string $slug, SchedulingEvent $scheduled, CheckInStatus $status): void
{
if (!$this->shouldHandleCheckIn) {
return;
}

$mutex = $scheduled->mutexName();

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

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

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

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

$checkIn->setStatus($status);
Expand Down Expand Up @@ -244,4 +236,9 @@ private function makeSlugForScheduled(SchedulingEvent $scheduled): string

return "scheduled_{$generatedSlug}";
}

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