Skip to content

Instantly share code, notes, and snippets.

@kathangeorg
Last active July 6, 2020 22:00
Show Gist options
  • Save kathangeorg/8717a4ec0af651da59362b07cd7041f8 to your computer and use it in GitHub Desktop.
Save kathangeorg/8717a4ec0af651da59362b07cd7041f8 to your computer and use it in GitHub Desktop.
Override for staticfilecache BoostQueueCommand.php
<?php
/*
* typo3conf/AdditionalConfiguration.php
*/
// ... all the other stuff ...
if (isset($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['staticfilecache'])) {
$oExtensionConfig = TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(SFC\Staticfilecache\Service\ConfigurationService::class);
$oExtensionConfig->override('boostMode', '1');
$oExtensionConfig->override('renameTablesToOtherPrefix', '1');
$oExtensionConfig->override('overrideCacheDirectory', 'fileadmin/user_upload/tx_staticfilecache/');
}
<?php
/**
* typo3conf/ext/kitt3n_custom/Classes/Command/BoostQueueCommand.php
* BoostQueueRunCommand.
*/
declare(strict_types=1);
namespace KITT3N\Kitt3nCustom\Command;
use SFC\Staticfilecache\Domain\Repository\QueueRepository;
use SFC\Staticfilecache\Service\CacheService;
use SFC\Staticfilecache\Service\ConfigurationService;
use SFC\Staticfilecache\Service\QueueService;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* BoostQueueRunCommand.
*/
class BoostQueueCommand extends \SFC\Staticfilecache\Command\BoostQueueCommand
{
/**
* Configures the current command.
*/
protected function configure()
{
parent::configure();
$this->setDescription('Run (work on) the cache boost queue. Call this task every 5 minutes.')
->addOption('limit-items', null, InputOption::VALUE_REQUIRED, 'Limit the items that are crawled. 0 => all', 500)
->addOption('stop-processing-after', null, InputOption::VALUE_REQUIRED, 'Stop crawling new items after N seconds since scheduler task started. 0 => infinite', 240)
->addOption('avoid-cleanup', null, InputOption::VALUE_NONE, 'Avoid the cleanup of the queue items');
}
/**
* Executes the current command.
*
* This method is not abstract because you can use this class
* as a concrete class. In this case, instead of defining the
* execute() method, you set the code to execute by passing
* a Closure to the setCode() method.
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int|null null or 0 if everything went fine, or an error code
*
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
*
* @see setCode()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
/* @var CacheService $cache */
$cache = GeneralUtility::makeInstance(CacheService::class)->get();
/* @var ConfigurationService $configuration */
$configuration = GeneralUtility::makeInstance(ConfigurationService::class);
$initialBoostMode = $configuration->get('boostMode');
switch (true) {
case $initialBoostMode === '1':
/*
* Cache entries are removed from filesystem and db only if boostMode is 0
* when using $cache->remove($runEntry['cache_url']) in foreach ($rows as $runEntry) { ... } below
*/
$configuration->override('boostMode', '0');
$queueRepository = GeneralUtility::makeInstance(QueueRepository::class);
$queueService = GeneralUtility::makeInstance(QueueService::class);
$io = new SymfonyStyle($input, $output);
$startTime = \time();
$stopProcessingAfter = (int)$input->getOption('stop-processing-after');
$limit = (int)$input->getOption('limit-items');
$limit = $limit > 0 ? $limit : 5000;
$rows = $queueRepository->findOpen($limit);
$io->progressStart(\count($rows));
foreach ($rows as $runEntry) {
if ($stopProcessingAfter > 0 && \time() >= $startTime + $stopProcessingAfter) {
$io->note('Skip after "stopProcessingAfter" time.');
break;
}
/*
* Remove cache entries from filesystem and db
*/
if ($cache->has($runEntry['cache_url'])) {
$cache->remove($runEntry['cache_url']);
}
/*
* Generate cache entries in filesystem and db
*/
$queueService->runSingleRequest($runEntry);
$io->progressAdvance();
}
$io->progressFinish();
$io->success(\count($rows) . ' items are done (perhaps not all are processed).');
if (!(bool)$input->getOption('avoid-cleanup')) {
parent::cleanupQueue($io);
}
// Reset boostMode
$configuration->override('boostMode', $initialBoostMode);
break;
default:
parent::execute($input, $output);
}
return 0;
}
}
<?php
/*
* typo3conf/ext/kitt3n_custom/Configuration/Commands.php
*/
use KITT3N\Kitt3nCustom\Command\BoostQueueCommand;
return [
'kitt3n_custom:staticfilecache:boostQueue' => [
'class' => BoostQueueCommand::class
],
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment