Skip to content
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

feat: Add command for sentry cron monitoring #36

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
58 changes: 58 additions & 0 deletions Classes/Command/SentryCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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="<slug>" <command with args>
* 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;
}
}

}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*"
},
Expand Down
Loading