0

Final Class Yiisoft\Yii\Sentry\SentryCronMonitor

InheritanceYiisoft\Yii\Sentry\SentryCronMonitor

Sends Sentry cron monitor check-ins for console commands configured in the cron-monitoring parameter.

The check-in is sent with in_progress status when a command starts and with either ok or error status, derived from the exit code, when it terminates. The final status is sent on {@see \Symfony\Component\Console\Event\ConsoleTerminateEvent} only, so an error listener that resets the exit code to 0 results in an ok check-in.

See also https://docs.sentry.io/platforms/php/crons/.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Sentry\State\HubInterface $hub, array $monitoring = [] )
$hub \Sentry\State\HubInterface
$monitoring array

Map of console command name to a monitor slug or monitor configuration.

                public function __construct(
    private readonly HubInterface $hub,
    private readonly array $monitoring = [],
) {}

            
handleCommand() public method

public void handleCommand ( \Symfony\Component\Console\Event\ConsoleCommandEvent $event )
$event \Symfony\Component\Console\Event\ConsoleCommandEvent

                public function handleCommand(ConsoleCommandEvent $event): void
{
    $command = $event->getCommand();
    assert($command !== null);
    $commandName = $command->getName();
    if ($commandName === null || !array_key_exists($commandName, $this->monitoring)) {
        return;
    }
    $config = $this->monitoring[$commandName];
    if (!is_string($config) && !is_array($config)) {
        throw new InvalidArgumentException(
            sprintf(
                'Sentry monitor configuration for the "%s" console command must be a string or an array, got %s.',
                $commandName,
                get_debug_type($config),
            ),
        );
    }
    $this->slug = $this->validateSlug(is_string($config) ? $config : ($config['slug'] ?? null), $commandName);
    $this->checkInId = $this->hub->captureCheckIn(
        $this->slug,
        CheckInStatus::inProgress(),
        monitorConfig: is_array($config) ? $this->createMonitorConfig($config) : null,
    );
    $this->startedAt = hrtime(true);
    $this->finished = false;
}

            
handleTerminate() public method

public void handleTerminate ( \Symfony\Component\Console\Event\ConsoleTerminateEvent $event )
$event \Symfony\Component\Console\Event\ConsoleTerminateEvent

                public function handleTerminate(ConsoleTerminateEvent $event): void
{
    $this->captureFinalCheckIn(
        $event->getExitCode() === 0 ? CheckInStatus::ok() : CheckInStatus::error(),
    );
}