Skip to content
This repository has been archived by the owner on Oct 29, 2020. It is now read-only.

Allow providers to be loaded lazy #113

Closed

Conversation

chregu
Copy link

@chregu chregu commented Jun 15, 2017

Currently, providers are not loaded lazy and there's no easy way (or I didn't find out) to let them load lazy. The problem is, that if you inject such a provider into a class in symfony, there is no easy way to catch an exception happening during constructing the class. And at least in the redis provider, an exception happens simply when the redis server is not reachable (which for a cache shouldn't lead in an not easy catchable exception). When lazy loading the provider, we can handle this case with a common try/catch construct in the get/set calls

@mikeSimonson
Copy link
Contributor

@chregu Can you add a test for this?

@chregu
Copy link
Author

chregu commented Jun 15, 2017

Tests added

{
$container = $this->compileContainer('lazy');

$providers = array(
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you know about the phpunit data provider ?
It would allow you to get rid of the foreach.

Copy link
Contributor

Choose a reason for hiding this comment

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

And if the first test fail you still see that the other pass.

Copy link
Author

Choose a reason for hiding this comment

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

The other tests in the same class do it the exact same way.

@stof
Copy link
Member

stof commented Jun 15, 2017

@chregu making the service lazy still does not give you any guarantee that the instantiation of the service will happen inside your try/catch

@chregu
Copy link
Author

chregu commented Jun 16, 2017

Maybe. But for me it's better than "doesn't happen inside a try/catch block at all".

@ChristianRiesen
Copy link

Having the option to lazy load these is very helpful. If chregu wouldn't have made the PR I would have. Tried the branch and works really well for me.

@alcaeus
Copy link
Member

alcaeus commented Nov 28, 2017

TBH, I believe this is the wrong approach to solve the problem described by @chregu. If you want to avoid exceptions e.g. if the redis server is unavailable, you can write a wrapper adapter that simply reverts to NOP for set/get calls and behaves as if there was no cache present (with all the problems go along with it). Deferring the exception to the set/get stage is a bad idea IMO. What do you think @stof @mikeSimonson?

@dbu
Copy link
Member

dbu commented Dec 18, 2017

isn't the root problem that a cache service should never ever throw exceptions in production mode? if redis is gone, the adapter should simply report it did not find the cached information. on construction it should also never explode. i guess during testing / development we want to know when things fail, they could be misconfigured or whatever. but during production, logging is fine but exceptions is not good. maybe inject the kernel.debug option as a flag whether errors should lead to exceptions?

@Tobion
Copy link

Tobion commented May 28, 2019

@chregu I believe this issue can be closed given that the bundle will be deprecated (#156) and our projects switched to Symfony cache some time ago so that the caches do not throw exceptions on failures anymore.

@alcaeus
Copy link
Member

alcaeus commented May 29, 2019

I agree - no point pursuing this any further.

@alcaeus alcaeus closed this May 29, 2019
@alcaeus alcaeus self-assigned this May 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants