Skip to content

Commit f6f5248

Browse files
authored
Always register the console scheduling macro (#900)
1 parent cb4311f commit f6f5248

File tree

1 file changed

+28
-31
lines changed

1 file changed

+28
-31
lines changed

Diff for: src/Sentry/Laravel/Features/ConsoleSchedulingIntegration.php

+28-31
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Illuminate\Console\Application as ConsoleApplication;
77
use Illuminate\Console\Scheduling\Event as SchedulingEvent;
88
use Illuminate\Contracts\Cache\Factory as Cache;
9-
use Illuminate\Contracts\Foundation\Application;
109
use Illuminate\Support\Str;
1110
use RuntimeException;
1211
use Sentry\CheckIn;
@@ -23,25 +22,10 @@ class ConsoleSchedulingIntegration extends Feature
2322
*/
2423
private $checkInStore = [];
2524

26-
/**
27-
* @var Cache The cache repository.
28-
*/
29-
private $cache;
25+
private $shouldHandleCheckIn = false;
3026

3127
public function register(): void
3228
{
33-
$this->onBootInactive();
34-
}
35-
36-
public function isApplicable(): bool
37-
{
38-
return $this->container()->make(Application::class)->runningInConsole();
39-
}
40-
41-
public function onBoot(Cache $cache): void
42-
{
43-
$this->cache = $cache;
44-
4529
$startCheckIn = function (
4630
?string $slug,
4731
SchedulingEvent $scheduled,
@@ -110,19 +94,19 @@ public function onBoot(Cache $cache): void
11094
});
11195
}
11296

97+
public function isApplicable(): bool
98+
{
99+
return true;
100+
}
101+
102+
public function onBoot(): void
103+
{
104+
$this->shouldHandleCheckIn = true;
105+
}
106+
113107
public function onBootInactive(): void
114108
{
115-
// 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
116-
SchedulingEvent::macro('sentryMonitor', function (
117-
?string $monitorSlug = null,
118-
?int $checkInMargin = null,
119-
?int $maxRuntime = null,
120-
bool $updateMonitorConfig = true,
121-
?int $failureIssueThreshold = null,
122-
?int $recoveryThreshold = null
123-
) {
124-
return $this;
125-
});
109+
$this->shouldHandleCheckIn = false;
126110
}
127111

128112
private function startCheckIn(
@@ -134,6 +118,10 @@ private function startCheckIn(
134118
?int $failureIssueThreshold,
135119
?int $recoveryThreshold
136120
): void {
121+
if (!$this->shouldHandleCheckIn) {
122+
return;
123+
}
124+
137125
$checkInSlug = $slug ?? $this->makeSlugForScheduled($scheduled);
138126

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

162150
if ($scheduled->runInBackground) {
163-
$this->cache->store()->put($cacheKey, $checkIn->getId(), $scheduled->expiresAt * 60);
151+
$this->resolveCache()->store()->put($cacheKey, $checkIn->getId(), $scheduled->expiresAt * 60);
164152
}
165153

166154
$this->sendCheckIn($checkIn);
167155
}
168156

169157
private function finishCheckIn(?string $slug, SchedulingEvent $scheduled, CheckInStatus $status): void
170158
{
159+
if (!$this->shouldHandleCheckIn) {
160+
return;
161+
}
162+
171163
$mutex = $scheduled->mutexName();
172164

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

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

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

195187
if ($scheduled->runInBackground) {
196-
$this->cache->store()->forget($cacheKey);
188+
$this->resolveCache()->store()->forget($cacheKey);
197189
}
198190

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

245237
return "scheduled_{$generatedSlug}";
246238
}
239+
240+
private function resolveCache(): Cache
241+
{
242+
return $this->container()->make(Cache::class);
243+
}
247244
}

0 commit comments

Comments
 (0)