8000 [FrameworkBundle] Make cache:clear "atomic" and consistent with cache:warmup by hkdobrev · Pull Request #25117 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Make cache:clear "atomic" and consistent with cache:warmup #25117

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
Dec 31, 2017
Merged
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
68 changes: 31 additions & 37 deletions src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,32 +93,44 @@ protected function execute(InputInterface $input, OutputInterface $output)
$realCacheDir = $this->getContainer()->getParameter('kernel.cache_dir');
}

$fs = $this->filesystem;
$io = new SymfonyStyle($input, $output);

$kernel = $this->getApplication()->getKernel();
$realCacheDir = isset($realCacheDir) ? $realCacheDir : $kernel->getContainer()->getParameter('kernel.cache_dir');
// the old cache dir name must not be longer than the real one to avoid exceeding
// the maximum length of a directory or file path within it (esp. Windows MAX_PATH)
$oldCacheDir = substr($realCacheDir, 0, -1).('~' === substr($realCacheDir, -1) ? '+' : '~');
$fs->remove($oldCacheDir);

if (!is_writable($realCacheDir)) {
throw new \RuntimeException(sprintf('Unable to write in the "%s" directory', $realCacheDir));
}

if ($this->filesystem->exists($oldCacheDir)) {
$this->filesystem->remove($oldCacheDir);
}

$io->comment(sprintf('Clearing the cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
$this->cacheClearer->clear($realCacheDir);

// The current event dispatcher is stale, let's not use it anymore
$this->getApplication()->setDispatcher(new EventDispatcher());

if ($input->getOption('no-warmup')) {
$this->filesystem->rename($realCacheDir, $oldCacheDir);
} else {
$this->warmupCache($input, $output, $realCacheDir, $oldCacheDir);
$containerDir = new \ReflectionObject($kernel->getContainer());
$containerDir = basename(dirname($containerDir->getFileName()));

// the warmup cache dir name must have the same length as the real one
// to avoid the many problems in serialized resources files
$warmupDir = substr($realCacheDir, 0, -1).('_' === substr($realCacheDir, -1) ? '-' : '_');

if ($output->isVerbose() && $fs->exists($warmupDir)) {
$io->comment('Clearing outdated warmup directory...');
}
$fs->remove($warmupDir);
$fs->mkdir($warmupDir);

if (!$input->getOption('no-warmup')) {
if ($output->isVerbose()) {
$io->comment('Warming up cache...');
}
$this->warmup($warmupDir, $realCacheDir, !$input->getOption('no-optional-warmers'));

if ($this->warning) {
@trigger_error($this->warning, E_USER_DEPRECATED);
Expand All @@ -127,12 +139,22 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

$containerDir = $fs->exists($warmupDir.'/'.$containerDir) ? false : $containerDir;

$fs->rename($realCacheDir, $oldCacheDir);
$fs->rename($warmupDir, $realCacheDir);

if ($containerDir) {
$fs->rename($oldCacheDir.'/'.$containerDir, $realCacheDir.'/'.$containerDir);
touch($realCacheDir.'/'.$containerDir.'.legacy');
}

if ($output->isVerbose()) {
$io->comment('Removing old cache directory...');
}

try {
$this->filesystem->remove($oldCacheDir);
$fs->remove($oldCacheDir);
} catch (IOException $e) {
$io->warning($e->getMessage());
}
Expand All @@ -144,34 +166,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$io->success(sprintf('Cache for the "%s" environment (debug=%s) was successfully cleared.', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
}

private function warmupCache(InputInterface $input, OutputInterface $output, $realCacheDir, $oldCacheDir)
{
$io = new SymfonyStyle($input, $output);

// the warmup cache dir name must have the same length than the real one
// to avoid the many problems in serialized resources files
$realCacheDir = realpath($realCacheDir);
$warmupDir = substr($realCacheDir, 0, -1).('_' === substr($realCacheDir, -1) ? '-' : '_');

if ($this->filesystem->exists($warmupDir)) {
if ($output->isVerbose()) {
$io->comment('Clearing outdated warmup directory...');
}
$this->filesystem->remove($warmupDir);
}

if ($output->isVerbose()) {
$io->comment('Warming up cache...');
}
$this->warmup($warmupDir, $realCacheDir, !$input->getOption('no-optional-warmers'));

$this->filesystem->rename($realCacheDir, $oldCacheDir);
if ('\\' === DIRECTORY_SEPARATOR) {
sleep(1); // workaround for Windows PHP rename bug
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been introduced in #8767, but links to a behavior that I'm not able to reproduce on PHP 5.5/7.1
I propose to remove it, since it slows down DX on Windows, and breaks "atomicity".

}
$this->filesystem->rename($warmupDir, $realCacheDir);
}

/**
* @param string $warmupDir
* @param string $realCacheDir
Expand Down
0