Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
bug #26670 [FrameworkBundle] Remove double cache generation by detect…
…ing fresh kernels on cache:clear (nicolas-grekas)

This PR was merged into the 4.1-dev branch.

Discussion
----------

[FrameworkBundle] Remove double cache generation by detecting fresh kernels on cache:clear

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

The `cache:clear` command currently runs non-optional cache warmers twice or more.
It also clears + warms up just created caches (when var/cache/dev doens't exist yet, eg on 1st `composer install`.)

This PR fixes both behaviors. To run only what's needed once.
Best reviewed [ignoring whitespaces](https://github.com/symfony/symfony/pull/26670/files?w=1).

Commits
-------

9d8eac2 [FrameworkBundle] Remove double cache generation by detecting fresh kernels on cache:clear
  • Loading branch information
fabpot committed Mar 28, 2018
2 parents 820b728 + 9d8eac2 commit e3f964f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 30 deletions.
67 changes: 42 additions & 25 deletions src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php
Expand Up @@ -93,8 +93,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
// The current event dispatcher is stale, let's not use it anymore
$this->getApplication()->setDispatcher(new EventDispatcher());

$containerDir = new \ReflectionObject($kernel->getContainer());
$containerDir = basename(dirname($containerDir->getFileName()));
$containerFile = (new \ReflectionObject($kernel->getContainer()))->getFileName();
$containerDir = basename(dirname($containerFile));

// the warmup cache dir name must have the same length as the real one
// to avoid the many problems in serialized resources files
Expand All @@ -104,34 +104,50 @@ protected function execute(InputInterface $input, OutputInterface $output)
$io->comment('Clearing outdated warmup directory...');
}
$fs->remove($warmupDir);
$fs->mkdir($warmupDir);

if (!$input->getOption('no-warmup')) {
if ($_SERVER['REQUEST_TIME'] <= filemtime($containerFile) && filemtime($containerFile) <= time()) {
if ($output->isVerbose()) {
$io->comment('Warming up cache...');
$io->comment('Cache is fresh.');
}
if (!$input->getOption('no-warmup') && !$input->getOption('no-optional-warmers')) {
if ($output->isVerbose()) {
$io->comment('Warming up optional cache...');
}
$warmer = $kernel->getContainer()->get('cache_warmer');
// non optional warmers already ran during container compilation
$warmer->enableOnlyOptionalWarmers();
$warmer->warmUp($realCacheDir);
}
} else {
$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'));
}
$this->warmup($warmupDir, $realCacheDir, !$input->getOption('no-optional-warmers'));
}

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

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

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

if ($output->isVerbose()) {
$io->comment('Removing old cache directory...');
}
if ($containerDir) {
$fs->rename($oldCacheDir.'/'.$containerDir, $realCacheDir.'/'.$containerDir);
touch($realCacheDir.'/'.$containerDir.'.legacy');
}

try {
$fs->remove($oldCacheDir);
} catch (IOException $e) {
if ($output->isVerbose()) {
$io->warning($e->getMessage());
$io->comment('Removing old cache directory...');
}

try {
$fs->remove($oldCacheDir);
} catch (IOException $e) {
if ($output->isVerbose()) {
$io->warning($e->getMessage());
}
}
}

Expand All @@ -152,11 +168,12 @@ private function warmup(string $warmupDir, string $realCacheDir, bool $enableOpt
$kernel->reboot($warmupDir);

// warmup temporary dir
$warmer = $kernel->getContainer()->get('cache_warmer');
if ($enableOptionalWarmers) {
$warmer->enableOptionalWarmers();
$warmer = $kernel->getContainer()->get('cache_warmer');
// non optional warmers already ran during container compilation
$warmer->enableOnlyOptionalWarmers();
$warmer->warmUp($warmupDir);
}
$warmer->warmUp($warmupDir);

// fix references to cached files with the real cache directory name
$search = array($warmupDir, str_replace('\\', '\\\\', $warmupDir));
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Bundle/FrameworkBundle/composer.json
Expand Up @@ -19,10 +19,10 @@
"php": "^7.1.3",
"ext-xml": "*",
"symfony/cache": "~3.4|~4.0",
"symfony/dependency-injection": "^3.4.3|^4.0.3",
"symfony/dependency-injection": "^4.1",
"symfony/config": "~3.4|~4.0",
"symfony/event-dispatcher": "~3.4|~4.0",
"symfony/http-foundation": "~3.4|~4.0",
"symfony/event-dispatcher": "^4.1",
"symfony/http-foundation": "^4.1",
"symfony/http-kernel": "^4.1",
"symfony/polyfill-mbstring": "~1.0",
"symfony/filesystem": "~3.4|~4.0",
Expand All @@ -44,11 +44,11 @@
"symfony/process": "~3.4|~4.0",
"symfony/security-core": "~3.4|~4.0",
"symfony/security-csrf": "~3.4|~4.0",
"symfony/serializer": "~3.4|~4.0",
"symfony/serializer": "^4.1",
"symfony/stopwatch": "~3.4|~4.0",
"symfony/translation": "~3.4|~4.0",
"symfony/templating": "~3.4|~4.0",
"symfony/validator": "~4.1",
"symfony/validator": "^4.1",
"symfony/var-dumper": "~3.4|~4.0",
"symfony/workflow": "~3.4|~4.0",
"symfony/yaml": "~3.4|~4.0",
Expand Down
Expand Up @@ -22,6 +22,7 @@ class CacheWarmerAggregate implements CacheWarmerInterface
{
private $warmers;
private $optionalsEnabled = false;
private $onlyOptionalsEnabled = false;

public function __construct(iterable $warmers = array())
{
Expand All @@ -33,6 +34,11 @@ public function enableOptionalWarmers()
$this->optionalsEnabled = true;
}

public function enableOnlyOptionalWarmers()
{
$this->onlyOptionalsEnabled = $this->optionalsEnabled = true;
}

/**
* Warms up the cache.
*
Expand All @@ -44,6 +50,9 @@ public function warmUp($cacheDir)
if (!$this->optionalsEnabled && $warmer->isOptional()) {
continue;
}
if ($this->onlyOptionalsEnabled && !$warmer->isOptional()) {
continue;
}

$warmer->warmUp($cacheDir);
}
Expand Down

0 comments on commit e3f964f

Please sign in to comment.