From 1f1313ed58b733891bd0647de1355f1b7a4d9869 Mon Sep 17 00:00:00 2001 From: Lars Lauger Date: Thu, 29 Aug 2024 09:43:39 +0200 Subject: [PATCH] feat: Add command for sentry cron monitoring --- Classes/Command/SentryCommandController.php | 58 +++++++++++++++++++++ composer.json | 1 + 2 files changed, 59 insertions(+) diff --git a/Classes/Command/SentryCommandController.php b/Classes/Command/SentryCommandController.php index 5405953..a0056e9 100644 --- a/Classes/Command/SentryCommandController.php +++ b/Classes/Command/SentryCommandController.php @@ -7,6 +7,10 @@ use Neos\Flow\Cli\CommandController; use Netlogix\Sentry\Exception\Test; use Netlogix\Sentry\Scope\ScopeProvider; +use Sentry\CheckInStatus; +use Symfony\Component\Process\Process; +use Throwable; +use function Sentry\captureCheckIn; /** * @Flow\Scope("singleton") @@ -59,4 +63,58 @@ public function showScopeCommand(): void \Neos\Flow\var_dump($this->scopeProvider->collectUser()); } + /** + * Run cron with sentry check-in + * + * Usage: ./flow sentry:runcron --slug="" + * Example: ./flow sentry:runcron --slug="foo" cache:collectgarbage "Flow_Mvc_Routing_Route" + * Example: ./flow sentry:runcron --slug="foo" cache:collectgarbage --cache-identifier="Flow_Mvc_Routing_Route" + * + * @param string $slug + * @return void + * @throws Throwable + */ + public function runCronCommand(string $slug): void + { + $args = $_SERVER['argv']; + + // unset sentry:runcron + unset($args[1]); + + $args = array_filter($args, fn (string $arg) => $arg !== '--slug=' . $slug && $arg !== $slug); + $id = captureCheckIn($slug, CheckInStatus::inProgress()); + $checkIn = fn (CheckInStatus $status) => captureCheckIn($slug, $status, null, null, $id); + + try { + $process = new Process($args); + $process->start(); + $lastCheckIn = time(); + + foreach ($process as $type => $data) { + // check in in 1 minute intervals + // FIXME: this may lead to a monitoring timeout if the child process doesn't output anything for a long time + if (time() - $lastCheckIn > 60) { + $checkIn(CheckInStatus::inProgress()); + $lastCheckIn = time(); + } + + if ($process::OUT === $type) { + fputs(STDOUT, $data); + } else { + fputs(STDERR, $data); + } + } + + if ($process->getExitCode() !== 0) { + $checkIn(CheckInStatus::error()); + return; + } + + $checkIn(CheckInStatus::ok()); + } catch (Throwable $e) { + $checkIn(CheckInStatus::error()); + throw $e; + } + } + } diff --git a/composer.json b/composer.json index 2d07a1a..b5493a1 100644 --- a/composer.json +++ b/composer.json @@ -7,6 +7,7 @@ "php": "^7.4 || ^8.0", "neos/flow": "^7.3.6 || ^8.0.4", "sentry/sdk": "^3.1", + "symfony/process": "^5.4 || ^6.0 || ^7.0", "ext-openssl": "*", "ext-json": "*" },