Skip to content

feat: Add support for Scheduled Tasks monitoring #659

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 4 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"require": {
"php": "^7.2 | ^8.0",
"illuminate/support": "^6.0 | ^7.0 | ^8.0 | ^9.0 | ^10.0",
"sentry/sentry": "^3.12",
"sentry/sentry": "^3.16",
"sentry/sdk": "^3.3",
"symfony/psr-http-message-bridge": "^1.0 | ^2.0",
"nyholm/psr7": "^1.0"
Expand Down
92 changes: 92 additions & 0 deletions src/Sentry/Laravel/Features/ConsoleIntegration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Sentry\Laravel\Features;

use Illuminate\Console\Scheduling\Event as SchedulingEvent;
use Illuminate\Contracts\Foundation\Application;
use Sentry\CheckIn;
use Sentry\CheckInStatus;
use Sentry\Event as SentryEvent;
use Sentry\SentrySdk;

class ConsoleIntegration extends Feature
{
/**
* @var array<string, CheckIn> The list of checkins that are currently in progress.
*/
private $checkInStore = [];

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

public function setup(): void
{
$startCheckIn = function (string $mutex, string $slug) {
$this->startCheckIn($mutex, $slug);
};
$finishCheckIn = function (string $mutex, CheckInStatus $status) {
$this->finishCheckIn($mutex, $status);
};

SchedulingEvent::macro('sentryMonitor', function (string $monitorSlug) use ($startCheckIn, $finishCheckIn) {
/** @var SchedulingEvent $this */
return $this
->before(function () use ($startCheckIn, $monitorSlug) {
/** @var SchedulingEvent $this */
$startCheckIn($this->mutexName(), $monitorSlug);
})
->onSuccess(function () use ($finishCheckIn) {
/** @var SchedulingEvent $this */
$finishCheckIn($this->mutexName(), CheckInStatus::ok());
})
->onFailure(function () use ($finishCheckIn) {
/** @var SchedulingEvent $this */
$finishCheckIn($this->mutexName(), CheckInStatus::error());
});
});
}

private function startCheckIn(string $mutex, string $slug): void
{
$options = SentrySdk::getCurrentHub()->getClient()->getOptions();

$checkIn = new CheckIn(
$slug,
CheckInStatus::inProgress(),
null,
$options->getEnvironment(),
$options->getRelease()
);

$this->checkInStore[$mutex] = $checkIn;

$this->sendCheckIn($checkIn);
}

private function finishCheckIn(string $mutex, CheckInStatus $status): void
{
$checkIn = $this->checkInStore[$mutex] ?? null;

// This should never happen (because we should always start before we finish), but better safe than sorry
if ($checkIn === null) {
return;
}

// We don't need to keep the checkin in memory anymore since we finished
unset($this->checkInStore[$mutex]);

$checkIn->setStatus($status);

$this->sendCheckIn($checkIn);
}

private function sendCheckIn(CheckIn $checkIn): void
{
$event = SentryEvent::createCheckIn();
$event->setCheckIn($checkIn);

SentrySdk::getCurrentHub()->captureEvent($event);
}
}
1 change: 1 addition & 0 deletions src/Sentry/Laravel/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class ServiceProvider extends BaseServiceProvider
*/
protected const FEATURES = [
Features\CacheIntegration::class,
Features\ConsoleIntegration::class,
Features\LivewirePackageIntegration::class,
];

Expand Down