This commit is contained in:
Jürgen Mummert
2026-01-09 22:01:16 +01:00
parent 56d806c579
commit 99ef883da5
7 changed files with 138 additions and 144 deletions
+20 -14
View File
@@ -3,7 +3,7 @@
namespace MummertMedia\ContaoMeilisearchBundle\Command;
use Contao\CoreBundle\Framework\ContaoFramework;
use Doctrine\DBAL\Connection;
use Contao\Database;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
@@ -13,7 +13,6 @@ class MeilisearchFilesCleanupCommand extends Command
{
public function __construct(
private readonly ContaoFramework $framework,
private readonly Connection $connection,
) {
parent::__construct();
}
@@ -22,13 +21,13 @@ class MeilisearchFilesCleanupCommand extends Command
{
$this
->setName('meilisearch:files:cleanup')
->setDescription('Remove stale indexed files from tl_search_files')
->setDescription('Remove stale indexed files (PDF, DOCX, XLSX, PPTX) from tl_search_pdf')
->addOption(
'grace',
null,
InputOption::VALUE_OPTIONAL,
'Grace period in seconds (files newer than now-grace are kept)',
86400
86400 // 24 Stunden
)
->addOption(
'dry-run',
@@ -50,10 +49,10 @@ class MeilisearchFilesCleanupCommand extends Command
$cutoff = time() - $grace;
if ($dryRun) {
$count = $this->connection->fetchOne(
'SELECT COUNT(*) FROM tl_search_files WHERE last_seen < ?',
[$cutoff]
);
$count = Database::getInstance()
->prepare('SELECT COUNT(*) AS cnt FROM tl_search_pdf WHERE last_seen < ?')
->execute($cutoff)
->cnt;
$message = sprintf(
'[DRY-RUN] %d stale file(s) would be removed (last_seen < %s)',
@@ -64,14 +63,14 @@ class MeilisearchFilesCleanupCommand extends Command
$output->writeln('<comment>' . $message . '</comment>');
$this->log($message);
$this->log('Cleaner stopped (dry-run)');
$this->log('Cleaner successfully stopped');
return Command::SUCCESS;
}
$affected = $this->connection->executeStatement(
'DELETE FROM tl_search_files WHERE last_seen < ?',
[$cutoff]
);
$affected = Database::getInstance()
->prepare('DELETE FROM tl_search_pdf WHERE last_seen < ?')
->execute($cutoff)
->affectedRows;
$message = sprintf(
'Removed %d stale file(s) (last_seen < %s)',
@@ -93,8 +92,15 @@ class MeilisearchFilesCleanupCommand extends Command
}
}
/**
* Einheitliches Logging mit Zeitstempel
*/
private function log(string $message): void
{
error_log(sprintf('[%s] %s', date('Y-m-d H:i:s'), $message));
error_log(sprintf(
'[%s] %s',
date('Y-m-d H:i:s'),
$message
));
}
}