Skip to content

[FrameworkBundle] Command cache:pool:clear warns and fails when one of the pools fails to clear #39910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$clearer->clear($kernel->getContainer()->getParameter('kernel.cache_dir'));
}

$failure = false;
foreach ($pools as $id => $pool) {
$io->comment(sprintf('Clearing cache pool: <info>%s</info>', $id));

if ($pool instanceof CacheItemPoolInterface) {
$pool->clear();
if (!$pool->clear()) {
$io->warning(sprintf('Cache pool "%s" could not be cleared.', $pool));
$failure = true;
}
} else {
$this->poolClearer->clearPool($id);
if (false === $this->poolClearer->clearPool($id)) {
$io->warning(sprintf('Cache pool "%s" could not be cleared.', $pool));
$failure = true;
}
}
}

if ($failure) {
return 1;
}

$io->success('Cache was successfully cleared.');

return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

use Symfony\Bundle\FrameworkBundle\Command\CachePoolClearCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Finder\SplFileInfo;

/**
* @group functional
Expand Down Expand Up @@ -73,6 +75,35 @@ public function testClearUnexistingPool()
->execute(['pools' => ['unknown_pool']], ['decorated' => false]);
}

public function testClearFailed()
{
$tester = $this->createCommandTester();
/** @var FilesystemAdapter $pool */
$pool = static::$container->get('cache.public_pool');
$item = $pool->getItem('foo');
$item->set('baz');
$pool->save($item);
$r = new \ReflectionObject($pool);
$p = $r->getProperty('directory');
$p->setAccessible(true);
$poolDir = $p->getValue($pool);

/** @var SplFileInfo $entry */
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($poolDir)) as $entry) {
// converts files into dir to make adapter fail
if ($entry->isFile()) {
unlink($entry->getPathname());
mkdir($entry->getPathname());
}
}

$tester->execute(['pools' => ['cache.public_pool']]);

$this->assertSame(1, $tester->getStatusCode(), 'cache:pool:clear exits with 1 in case of error');
$this->assertStringNotContainsString('[OK] Cache was successfully cleared.', $tester->getDisplay());
$this->assertStringContainsString('[WARNING] Cache pool "cache.public_pool" could not be cleared.', $tester->getDisplay());
}

private function createCommandTester()
{
$application = new Application(static::$kernel);
Expand Down