Add cleanup command for stale indexed files
This commit is contained in:
@@ -0,0 +1,77 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MummertMedia\ContaoMeilisearchBundle\Command;
|
||||||
|
|
||||||
|
use Contao\CoreBundle\Framework\ContaoFramework;
|
||||||
|
use Contao\Database;
|
||||||
|
use Symfony\Component\Console\Command\Command;
|
||||||
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
|
||||||
|
class MeilisearchFilesCleanupCommand extends Command
|
||||||
|
{
|
||||||
|
protected static $defaultName = 'meilisearch:files:cleanup';
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private readonly ContaoFramework $framework,
|
||||||
|
) {
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function configure(): void
|
||||||
|
{
|
||||||
|
$this
|
||||||
|
->setDescription('Removes 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)',
|
||||||
|
3600 // 1 Stunde Default
|
||||||
|
)
|
||||||
|
->addOption(
|
||||||
|
'dry-run',
|
||||||
|
null,
|
||||||
|
InputOption::VALUE_NONE,
|
||||||
|
'Show how many entries would be removed without deleting them'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||||
|
{
|
||||||
|
$this->framework->initialize();
|
||||||
|
|
||||||
|
$grace = max(0, (int) $input->getOption('grace'));
|
||||||
|
$dryRun = (bool) $input->getOption('dry-run');
|
||||||
|
$cutoff = time() - $grace;
|
||||||
|
|
||||||
|
if ($dryRun) {
|
||||||
|
$count = Database::getInstance()
|
||||||
|
->prepare('SELECT COUNT(*) AS cnt FROM tl_search_pdf WHERE last_seen < ?')
|
||||||
|
->execute($cutoff)
|
||||||
|
->cnt;
|
||||||
|
|
||||||
|
$output->writeln(sprintf(
|
||||||
|
'[DRY-RUN] %d stale file(s) would be removed (last_seen < %s)',
|
||||||
|
$count,
|
||||||
|
date('Y-m-d H:i:s', $cutoff)
|
||||||
|
));
|
||||||
|
|
||||||
|
return Command::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
$affected = Database::getInstance()
|
||||||
|
->prepare('DELETE FROM tl_search_pdf WHERE last_seen < ?')
|
||||||
|
->execute($cutoff)
|
||||||
|
->affectedRows;
|
||||||
|
|
||||||
|
$output->writeln(sprintf(
|
||||||
|
'Removed %d stale file(s) from tl_search_pdf (last_seen < %s)',
|
||||||
|
$affected,
|
||||||
|
date('Y-m-d H:i:s', $cutoff)
|
||||||
|
));
|
||||||
|
|
||||||
|
return Command::SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user