Skip to content

Commit 23b0746

Browse files
committed
feat: Add support for Cron Monitoring
1 parent a90dc6a commit 23b0746

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"require": {
2424
"php": "^7.2 | ^8.0",
2525
"illuminate/support": "^6.0 | ^7.0 | ^8.0 | ^9.0 | ^10.0",
26-
"sentry/sentry": "^3.12",
26+
"sentry/sentry": "^3.16",
2727
"sentry/sdk": "^3.3",
2828
"symfony/psr-http-message-bridge": "^1.0 | ^2.0",
2929
"nyholm/psr7": "^1.0"

src/Sentry/Laravel/ServiceProvider.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
use Illuminate\Contracts\Container\BindingResolutionException;
66
use Illuminate\Contracts\Events\Dispatcher;
77
use Illuminate\Contracts\Http\Kernel as HttpKernelInterface;
8+
use Illuminate\Console\Scheduling\Event as LaravelEvent;
89
use Illuminate\Foundation\Application as Laravel;
910
use Illuminate\Foundation\Http\Kernel as HttpKernel;
1011
use Illuminate\Log\LogManager;
1112
use RuntimeException;
13+
use Sentry\CheckIn;
14+
use Sentry\CheckInStatus;
1215
use Sentry\ClientBuilder;
1316
use Sentry\ClientBuilderInterface;
17+
use Sentry\Event as SentryEvent;
1418
use Sentry\Integration as SdkIntegration;
1519
use Sentry\Laravel\Console\PublishCommand;
1620
use Sentry\Laravel\Console\TestCommand;
@@ -59,6 +63,7 @@ public function boot(): void
5963
$this->bindEvents();
6064

6165
$this->setupFeatures();
66+
$this->setupCronMonitoring();
6267

6368
if ($this->app->bound(HttpKernelInterface::class)) {
6469
/** @var \Illuminate\Foundation\Http\Kernel $httpKernel */
@@ -143,6 +148,45 @@ protected function setupFeatures(): void
143148
}
144149
}
145150

151+
/**
152+
* Setup Cron Monitoring.
153+
*/
154+
private function setupCronMonitoring(): void
155+
{
156+
LaravelEvent::macro('sentryMonitor', function (string $monitorSlug) {
157+
$hub = SentrySdk::getCurrentHub();
158+
$checkIn = new CheckIn(
159+
$monitorSlug,
160+
CheckInStatus::inProgress()
161+
);
162+
163+
/** @var \Illuminate\Console\Scheduling\Event $this */
164+
return $this
165+
->before(function () use ($hub, $checkIn) {
166+
$event = SentryEvent::createCheckIn();
167+
$event->setCheckIn($checkIn);
168+
169+
$hub->captureEvent($event);
170+
})
171+
->onSuccess(function () use ($hub, $checkIn) {
172+
$checkIn->setStatus(CheckInStatus::ok());
173+
174+
$event = SentryEvent::createCheckIn();
175+
$event->setCheckIn($checkIn);
176+
177+
$hub->captureEvent($event);
178+
})
179+
->onFailure(function () use ($hub, $checkIn) {
180+
$checkIn->setStatus(CheckInStatus::error());
181+
182+
$event = SentryEvent::createCheckIn();
183+
$event->setCheckIn($checkIn);
184+
185+
$hub->captureEvent($event);
186+
});
187+
});
188+
}
189+
146190
/**
147191
* Register the artisan commands.
148192
*/

0 commit comments

Comments
 (0)