Change Delete Command

This commit is contained in:
Jürgen Mummert
2026-01-05 10:25:58 +01:00
parent b684267541
commit d9b8646835
@@ -11,8 +11,6 @@ use Symfony\Component\Console\Output\OutputInterface;
class MeilisearchFilesCleanupCommand extends Command class MeilisearchFilesCleanupCommand extends Command
{ {
protected static $defaultName = 'meilisearch:files:cleanup';
public function __construct( public function __construct(
private readonly ContaoFramework $framework, private readonly ContaoFramework $framework,
) { ) {
@@ -22,13 +20,14 @@ class MeilisearchFilesCleanupCommand extends Command
protected function configure(): void protected function configure(): void
{ {
$this $this
->setDescription('Removes stale indexed files (PDF, DOCX, XLSX, PPTX) from tl_search_pdf') ->setName('meilisearch:files:cleanup')
->setDescription('Remove stale indexed files (PDF, DOCX, XLSX, PPTX) from tl_search_pdf')
->addOption( ->addOption(
'grace', 'grace',
null, null,
InputOption::VALUE_OPTIONAL, InputOption::VALUE_OPTIONAL,
'Grace period in seconds (files newer than now-grace are kept)', 'Grace period in seconds (files newer than now-grace are kept)',
3600 // 1 Stunde Default 3600
) )
->addOption( ->addOption(
'dry-run', 'dry-run',
@@ -40,11 +39,12 @@ class MeilisearchFilesCleanupCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output): int protected function execute(InputInterface $input, OutputInterface $output): int
{ {
// wichtig für Contao 4.13 & 5.x
$this->framework->initialize(); $this->framework->initialize();
$grace = max(0, (int) $input->getOption('grace')); $grace = max(0, (int) $input->getOption('grace'));
$dryRun = (bool) $input->getOption('dry-run'); $dryRun = (bool) $input->getOption('dry-run');
$cutoff = time() - $grace; $cutoff = time() - $grace;
if ($dryRun) { if ($dryRun) {
$count = Database::getInstance() $count = Database::getInstance()
@@ -53,7 +53,7 @@ class MeilisearchFilesCleanupCommand extends Command
->cnt; ->cnt;
$output->writeln(sprintf( $output->writeln(sprintf(
'[DRY-RUN] %d stale file(s) would be removed (last_seen < %s)', '<comment>[DRY-RUN]</comment> %d stale file(s) would be removed (last_seen < %s)',
$count, $count,
date('Y-m-d H:i:s', $cutoff) date('Y-m-d H:i:s', $cutoff)
)); ));
@@ -67,7 +67,7 @@ class MeilisearchFilesCleanupCommand extends Command
->affectedRows; ->affectedRows;
$output->writeln(sprintf( $output->writeln(sprintf(
'Removed %d stale file(s) from tl_search_pdf (last_seen < %s)', '<info>Removed %d stale file(s)</info> (last_seen < %s)',
$affected, $affected,
date('Y-m-d H:i:s', $cutoff) date('Y-m-d H:i:s', $cutoff)
)); ));