Skip to content

CacheWarmerAggregate handle deprecations logs #27421

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

Conversation

ScullWM
Copy link
Contributor

@ScullWM ScullWM commented May 30, 2018

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #27387
License MIT

Actually the Web Debug Toolbar warning you about the deprecation messages thrown during the container built (#21502).
Cache warmup can throw deprecated message without any persistance, it may cause issue like #27387
This PR reproduce the same job for the cache warmer, and so on, handle deprecated messages during the warmup of Twig, Translator, Validator, Security and all kernel.cache_warmer services.

Here are the point that may be improvable in this PR:

  1. Actually I've "duplicate" the callable used in the set_error_handler of the Kernel.
    IMHO I think that Kernel and CacheWarmerAggregate have differents jobs and a trait may be a good solution to share this error handler setter without duplicating the code, but I'm a little bit lost about the repercussion of adding a Trait in the Kernel.

  2. I've think about extending the CacheWarmerAggregate into a DeprecatedLogHandlingCacheWarmerAggregate to add the debug and containerClass argument, and declare it as the cache_warmer service only in debug mode (by declaring it in the DebugBundle/Resources/config/services.xml).

@@ -32,6 +32,8 @@

<service id="cache_warmer" class="Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate" public="true">
<argument type="tagged" tag="kernel.cache_warmer" />
<argument>%kernel.debug%</argument>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

CacheWarmerAggregate is annoted as final, so it shouldn't be BCBreak

@ScullWM ScullWM force-pushed the cachewarmer_deprecated_handler branch 2 times, most recently from 61eb2e1 to ed92377 Compare May 30, 2018 19:31
@nicolas-grekas nicolas-grekas added this to the next milestone May 31, 2018
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

thanks for working on this, it's been missing a lot!

{
$this->warmers = $warmers;
$this->debug = $debug;
$this->containerPathPrefix = $containerPathPrefix;
Copy link
Member

Choose a reason for hiding this comment

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

I'd suggest to pass the path of the deprecation file instead, not only the prefix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

$containerPathPrefix became $deprecationLogsFilepath with the right argument.


$warmer->warmUp($cacheDir);
file_put_contents($this->containerPathPrefix.'Deprecations.log', serialize(array_values($collectedLogs)));
restore_error_handler();
Copy link
Member

Choose a reason for hiding this comment

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

this call should be the 1st in the "if" so that we're sure it's always restored correctly

@ScullWM ScullWM force-pushed the cachewarmer_deprecated_handler branch from ed92377 to 1da0b71 Compare May 31, 2018 11:35
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

LGTM, with one minor comment.


$warmer->warmUp($cacheDir);
if (file_exists($file = $this->deprecationLogsFilepath)) {
Copy link
Member

Choose a reason for hiding this comment

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

no need for the $file tmp var now

@ScullWM ScullWM force-pushed the cachewarmer_deprecated_handler branch from 1da0b71 to 3e473ee Compare May 31, 2018 21:15
}
} finally {
if ($this->debug && true !== $previousHandler) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've also prepare a test scenario with a cacheWarmer who throw deprecations but here I'm directly in conflict with #25413 (fixing #24767). It can't pass in this condition during the test and it's right.

Here was my test code (CacheWarmerAggregateTest):

    public function testWarmupWithDeprecationLogs()
    {
        $cacheDir = self::$cacheDir;
        $warmer = $this->getCacheWarmerMock();
        $warmer
            ->expects($this->once())
            ->method('warmUp')
            ->with($cacheDir)
            ->will($this->returnValue($this->throwDeprecation()));
        ;

        $deprecationLogsFilepath = self::$cacheDir.'/Deprecations.log';

        $aggregate = new CacheWarmerAggregate(array($warmer), true, $deprecationLogsFilepath);
        $aggregate->warmUp($cacheDir);

        $this->assertTrue(file_exists($deprecationLogsFilepath));
        $this->assertCount(1, unserialize(file_get_contents($deprecationLogsFilepath)));
    }

    protected function throwDeprecation()
    {
        @trigger_error('Deprecation Message', E_USER_DEPRECATED);
    }

Copy link
Member

@nicolas-grekas nicolas-grekas May 31, 2018

Choose a reason for hiding this comment

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

Unsetting the env var temporarily should be doable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've try many solution, and even temporarily removing the defined constant (runkit_constant_remove), deprecations have a different behavior during phpunit test. Still looking to find a solution and test it.

Copy link
Member

Choose a reason for hiding this comment

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

ok, let's forget about it :):

@ScullWM ScullWM force-pushed the cachewarmer_deprecated_handler branch 2 times, most recently from 4d499a7 to 1cbe807 Compare May 31, 2018 22:14
@ScullWM
Copy link
Contributor Author

ScullWM commented Jun 1, 2018

Show rendering on symfony-demo:
capture d ecran 2018-06-01 a 13 41 06

capture d ecran 2018-06-01 a 13 39 30

@ScullWM ScullWM force-pushed the cachewarmer_deprecated_handler branch from 1cbe807 to f6c3851 Compare June 1, 2018 14:32
continue;
if ($this->debug) {
$collectedLogs = array();
$previousHandler = defined('PHPUNIT_COMPOSER_INSTALL');
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you're talking about the definition of PHPUNIT_COMPOSER_INSTALL it's related to #25413

@ScullWM ScullWM force-pushed the cachewarmer_deprecated_handler branch from f6c3851 to f03b8bb Compare June 5, 2018 22:13
@nicolas-grekas
Copy link
Member

Thank you @ScullWM.

@nicolas-grekas nicolas-grekas merged commit f03b8bb into symfony:master Jun 19, 2018
nicolas-grekas added a commit that referenced this pull request Jun 19, 2018
This PR was merged into the 4.2-dev branch.

Discussion
----------

CacheWarmerAggregate handle deprecations logs

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #27387
| License       | MIT

Actually the Web Debug Toolbar warning you about the deprecation messages thrown during the container built (#21502).
Cache warmup can throw deprecated message without any persistance, it may cause issue like #27387
This PR reproduce the same job for the cache warmer, and so on, handle deprecated messages during the warmup of Twig, Translator, Validator, Security and all `kernel.cache_warmer` services.

Here are the point that may be improvable in this PR:

1.  Actually I've "duplicate" the callable used in the `set_error_handler` of the Kernel.
IMHO I think that Kernel and CacheWarmerAggregate have differents jobs and a trait may be a good solution to share this error handler setter without duplicating the code, but I'm a little bit lost about the repercussion of adding a Trait in the Kernel.

2. I've think about extending the `CacheWarmerAggregate` into a `DeprecatedLogHandlingCacheWarmerAggregate` to add the debug and containerClass argument, and declare it as the `cache_warmer` service only in debug mode (by declaring it in the DebugBundle/Resources/config/services.xml).

Commits
-------

f03b8bb CacheWarmerAggregate handle deprecations logs
@stof
Copy link
Member

stof commented Jun 19, 2018

What about having separate files for the warmer deprecations and the container deprecations ? It would avoid having to read the existing file to merge deprecations with the existing ones (which might cause issues if you run cache:warmup as the cache warmer could run multiple times for a single run of the container building, duplicating warmer deprecations)

@nicolas-grekas nicolas-grekas modified the milestones: next, 4.2 Nov 1, 2018
@fabpot fabpot mentioned this pull request Nov 3, 2018
@fabpot fabpot mentioned this pull request Nov 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants