add logging to cron

This commit is contained in:
Jürgen Mummert
2026-01-09 09:40:08 +01:00
parent e9f06f7cc9
commit d2c9263755
+48 -37
View File
@@ -3,55 +3,52 @@
namespace MummertMedia\ContaoMeilisearchBundle\Cron; namespace MummertMedia\ContaoMeilisearchBundle\Cron;
use Contao\CoreBundle\Framework\ContaoFramework; use Contao\CoreBundle\Framework\ContaoFramework;
use MummertMedia\ContaoMeilisearchBundle\Service\MeilisearchIndexService;
use Symfony\Component\Process\Process; use Symfony\Component\Process\Process;
class MeilisearchIndexCron class MeilisearchIndexCron
{ {
private string $logFile;
public function __construct( public function __construct(
private readonly MeilisearchIndexService $indexService,
private readonly ContaoFramework $framework, private readonly ContaoFramework $framework,
private readonly string $projectDir, private readonly string $projectDir,
) {} ) {
$this->logFile = $this->projectDir . '/var/logs/meilisearch_bundle.log';
}
public function __invoke(): void public function __invoke(): void
{ {
$start = microtime(true);
error_log('[MeilisearchCron] === START ===');
// Contao initialisieren
$this->framework->initialize(); $this->framework->initialize();
error_log('[MeilisearchCron] Contao framework initialized');
// 1) Contao Crawl $this->log('=== CRON START ===');
$this->runConsole(
'contao:crawl', // 1) Cleanup
'Contao crawl' $this->runStep(
'meilisearch:files:cleanup',
'meilisearch:files:cleanup'
); );
// 2) Cleanup (24h Grace) // 2) Contao Crawl
$this->runConsole( $this->runStep(
'meilisearch:files:cleanup', 'contao:crawl',
'Meilisearch files cleanup' 'contao:crawl'
); );
// 3) Meilisearch Index // 3) Meilisearch Index
try { $this->runStep(
error_log('[MeilisearchCron] Meilisearch index started'); 'meilisearch:index',
$this->indexService->run(); 'meilisearch:index'
error_log('[MeilisearchCron] Meilisearch index finished'); );
} catch (\Throwable $e) {
error_log('[MeilisearchCron] ERROR during Meilisearch index: ' . $e->getMessage());
}
$duration = round(microtime(true) - $start, 2); $this->log('=== CRON END ===');
error_log('[MeilisearchCron] === END (duration: ' . $duration . 's) ===');
} }
private function runConsole(string $command, string $label): void /**
* Führt einen Console-Command aus und loggt sauber Start/Ende
*/
private function runStep(string $command, string $label): void
{ {
error_log('[MeilisearchCron] ' . $label . ' started'); $this->log($label . ' gestartet');
$process = new Process([ $process = new Process([
PHP_BINARY, PHP_BINARY,
@@ -63,16 +60,30 @@ class MeilisearchIndexCron
$process->run(); $process->run();
if (!$process->isSuccessful()) { if (!$process->isSuccessful()) {
error_log( $this->log(
'[MeilisearchCron] ERROR in ' . $label . ': ' . $label . ' FEHLGESCHLAGEN',
$process->getErrorOutput() $process->getErrorOutput() ?: 'Unbekannter Fehler'
); );
} else {
$output = trim($process->getOutput()); // ❌ Abbruch Folgeschritte NICHT ausführen
if ($output !== '') { return;
error_log('[MeilisearchCron] ' . $label . ' output: ' . $output);
}
error_log('[MeilisearchCron] ' . $label . ' finished');
} }
$this->log($label . ' erfolgreich beendet');
}
/**
* Schreibt eine Logzeile mit Zeitstempel
*/
private function log(string $message, string $details = ''): void
{
$line = sprintf(
"[%s] %s%s\n",
date('Y-m-d H:i:s'),
$message,
$details ? ' | ' . trim($details) : ''
);
file_put_contents($this->logFile, $line, FILE_APPEND);
} }
} }