I made some changes to own services and now I get an error about invalid arguments. When I clear all caches everything works fine. I don't want to clear all caches in my production environment. Which cache bin (and how) do I need to clear? I use drupal console. I tried 'drupal cache:rebuild container' but I get a message 'Cache container is invalid'.
2 Answers
Drupal Console uses Cache::getBins()
for its list of valid cache bins - that method returns all services in the main container tagged with cache.bin
. The container
cache service is part of the bootstrap container, which is managed internally by DrupalKernel
.
I haven't dug deep enough to know for sure, but keeping the management of that bin internal seems to be deliberate, so there's probably no simple (maybe even no desirable) way to hack around it.
Fortunately, a custom plugin for Console is easy to create with drupal generate:command
(there's a tutorial here). Just be sure to inject the kernel
service, and call $this->kernel->invalidateContainer();
in the command 's action callback.
There are no real guarantees about what needs to be invalidated when you change dependencies. The service definition for the compiled container doesn't match up with the PHP class constructors it is referencing, so you are getting errors. So, you need to do a full container rebuild, which will also trigger a cache clear.
The proper way to do this when pushing a change to a new environment is to add a an empty hook_update_N()
to your module defining the service. It can also be done with an empty hook_post_update_NAME
, which is what core tends to do to avoid patch conflicts (among other reasons).
Then, after deployment you can do a drush updb
, which will run all pending updates.
-
I was going to mention something similar (about not knowing what else might need to be invalidated at the same time), but I couldn't think of any categories of things that might be affected. Do you know of any in particular?– Clive ♦Commented Aug 10, 2018 at 20:05
-
@Clive Look at
drupal_flush_all_caches()
and everything it does after invalidating the container. Also consider that a most of what is in YAML ends up in in the K-V and not Cache.– mpdonadio ♦Commented Aug 10, 2018 at 20:27 -
Yeah I was looking there earlier, I didn't think there was anything after the container invalidation that would also need to be done for just a dependency change. But I'll take your word for it. Maybe OP could implement a custom command with just the select things they need to flush (e.g. not Twig/router/etc)– Clive ♦Commented Aug 10, 2018 at 20:43
-
I already used one hook_update_N() function, but not an empty one, one that added a db table. I can try, but does it really make a difference wehther it is empty or not? Commented Aug 11, 2018 at 8:11