Followup #2999721: [META] Deprecate the legacy include files before Drupal 9

Problem/Motivation

Kill includes

Proposed resolution

Convert drupal_flush_all_caches function into a Clearer service.
Services with the cache_clearer tag and implementing the Drupal\Core\Cache\CacheClearerInterface will have their clearCache() method called when a cache flush is requested.

Remaining tasks

  1. Add new methods
  2. Replace body of the function with new method call
    Replace old function calls with the new method
  3. Create CR
  4. Add @deprecated annotation with message for the deprecated functions
  5. Call @trigger_error function in the deprecated functions
  6. Add the legacy tests

User interface changes

none

API changes

New service Drupal\Core\Cache\ChainCacheClearer::clearCache() deprecates drupal_flush_all_caches.

Data model changes

none

CommentFileSizeAuthor
#109 3014752-nr-bot.txt90 bytesneeds-review-queue-bot
#55 interdiff-3014752-53-55.txt2.2 KBvoleger
#55 3014752-55.patch35.23 KBvoleger
#53 interdiff-3014752-47-53.txt2.18 KBvoleger
#53 3014752-53.patch35.3 KBvoleger
#47 diff_44-47.txt519 bytesdeepak goyal
#47 3014752-47.patch34.26 KBdeepak goyal
#44 interdiff-3014752-41-44.txt513 bytesmrinalini9
#44 3014752-44.patch34.25 KBmrinalini9
#41 interdiff-3014752-39-41.txt951 bytesmrinalini9
#41 3014752-41.patch34.25 KBmrinalini9
#39 3014752-39.patch33.14 KBkishor_kolekar
#36 interdiff-3014752-35-36.txt3.04 KBvoleger
#36 3014752-36.patch33.12 KBvoleger
#35 interdiff_32-35.txt813 bytesswatichouhan012
#35 3014752-35.patch30.38 KBswatichouhan012
#32 interdiff-3014752-31-32.txt654 bytesvoleger
#32 3014752-32.patch30.34 KBvoleger
#31 3014752-31.patch30.58 KBvoleger
#29 interdiff-3014752-26-29.txt858 bytesvoleger
#29 3014752-29.patch31.49 KBvoleger
#26 interdiff-3014752-24-26.txt1.4 KBvoleger
#26 3014752-26.patch30.47 KBvoleger
#24 3014752-24.patch30.46 KBvoleger
#22 interdiff-3014752-18-22.txt6.06 KBvoleger
#22 3014752-22.patch31.08 KBvoleger
#22 3014752-22-JUST-REROLL.patch30.69 KBvoleger
#18 3014752-18.patch30.72 KBandypost
#18 interdiff.txt4.14 KBandypost
#17 3014752-17.patch30.15 KBvoleger
#15 interdiff-3014752-12-15.txt1.17 KBvoleger
#15 3014752-15.patch30.12 KBvoleger
#12 interdiff-3014752-05-12.txt28.74 KBvoleger
#12 3014752-12.patch30.04 KBvoleger
#6 interdiff-3014752-02-05.txt811 bytesgoodboy
#5 3014752-05.patch5.23 KBgoodboy
#5 interdiff-3014752-02-05.patch811 bytesgoodboy
#2 3014752-02.patch4.76 KBvoleger

Issue fork drupal-3014752

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

voleger created an issue. See original summary.

voleger’s picture

Assigned: voleger » Unassigned
Issue summary: View changes
StatusFileSize
new4.76 KB

First iteration. Moved function into a class as the static method. Replaced calls in the core directory. Removed include on the utility.inc file in the rebuild.php script.
Todo:

  1. Create CR
  2. Add @deprecated annotation with message for deprecated drupal_rebuild function
  3. Call @trigger_error function in deprecated drupal_rebuild function
  4. Add legacy test
andypost’s picture

+++ b/core/rebuild.php
@@ -50,7 +50,7 @@
-  drupal_rebuild($autoloader, $request);
+  Cache::rebuild($autoloader, $request);

Not sure that related to cache - it is more like controller

voleger’s picture

Maybe, so why not to convert the rebuild.php script to the controller and keep there all related functionality from script including drupal_rebuild function?
Is it possible?

goodboy’s picture

StatusFileSize
new811 bytes
new5.23 KB

Add deprecated annotation and trigger_error function

goodboy’s picture

StatusFileSize
new811 bytes
andypost’s picture

Btw while you on it - Icm sure it needs to check core commands, probably there is similar command so they could reuse same codebase

andypost’s picture

mile23’s picture

It seems like drupal_flush_all_caches() is doing most of the heavy lifting here.

It doesn't have a 'kill includes' issue, either.

How about we turn drupal_flush_all_caches() and drupal_rebuild() into a class. Maybe call it CacheRebuilder.

It seems like the replacement for drupal_flush_all_caches() would be concrete (rather than static), so we could inject services, but it just rebuilds the container so maybe not.

andypost’s picture

why not to convert the rebuild.php script to the controller

we could inject services

I see no way, rebuild.php was introduced exactly for the case when core can't bootstrap #2097189: Add a rebuild script

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

voleger’s picture

Issue summary: View changes
Issue tags: +Needs change record updates
Parent issue: » #2999721: [META] Deprecate the legacy include files before Drupal 9
StatusFileSize
new30.04 KB
new28.74 KB

I like the idea #9 with the rebuilder class.
Moved the method, deprecated drupal_flush_all_caches() and _drupal_flush_css_js() functions.

voleger’s picture

Status: Needs work » Needs review
goodboy’s picture

I see bins deleted in Rebuilder::rebuild()

   ...
  foreach (Cache::getBins() as $bin) {
     $bin->deleteAll();
  }

  // Disable recording of cached pages.
  \Drupal::service('page_cache_kill_switch')->trigger();

  self::flushAllCaches();
  ...

But Rebuild::flushAllCaches() has bins clearing code already

    foreach (Cache::getBins() as $service_id => $cache_backend) {
      $cache_backend->deleteAll();
    }

And this last bin clearer code has unused $service_id key.

So, it seems bins in Rebuilder::rebuild() were cleared twice. If this is correct then need create non-public member function binsClear() to exclude similar code in both functions. If this is incorrect then need remove bins clear code from Rebuilder::rebuild() and remove unused $service_id key from Rebuild::flushAllCaches().

voleger’s picture

StatusFileSize
new30.12 KB
new1.17 KB

#14 nice catch and sounds reasonable to move that into the protected method

berdir’s picture

Status: Needs review » Needs work

That we are clearing them twice is AFAIK actually a long-standing bug and there is an issue about that, so I wouldn't bother to make a method for that.

  1. +++ b/core/includes/common.inc
    @@ -1183,10 +1123,15 @@ function drupal_flush_all_caches() {
      */
     function _drupal_flush_css_js() {
    -  // The timestamp is converted to base 36 in order to make it more compact.
    -  Drupal::state()->set('system.css_js_query_string', base_convert(REQUEST_TIME, 10, 36));
    +  @trigger_error('_drupal_flush_css_js() is deprecated in drupal:8.8.0. It will be removed from drupal:9.0.0. Use \Drupal\Core\Cache\Rebuilder::flushCssJs() instead. See https://www.drupal.org/node/3014783', E_USER_DEPRECATED);
    +  Rebuilder::flushCssJs();
     }
    

    this was prefixed with an underscore, so it was considered an internal/private function. should we add a public replacement for that, are there any direct calls to it? If not we could make it protected and just keep the old code here, it's a single line anyway.

    Checking below, looks like there are actually are some calls to it. interesting..

  2. +++ b/core/lib/Drupal/Core/Cache/Rebuilder.php
    @@ -0,0 +1,208 @@
    +  /**
    +   * Rebuilds all caches even when Drupal itself does not work.
    +   *
    +   * @param mixed $class_loader
    +   *   The class loader. Normally Composer's ClassLoader, as included by the
    +   *   front controller, but may also be decorated; e.g.,
    +   *   \Symfony\Component\ClassLoader\ApcClassLoader,
    +   *   \Symfony\Component\ClassLoader\WinCacheClassLoader,
    +   *   or \Symfony\Component\ClassLoader\XcacheClassLoader.
    +   * @param \Symfony\Component\HttpFoundation\Request $request
    +   *   The current request.
    +   *
    +   * @see rebuild.php
    +   */
    +  public static function rebuild($class_loader, Request $request) {
    

    Naming...

    I like putting all this stuff into a Rebuilder class.

    But the thing is that flushAllCaches() obviously does so much more than that (to the point that I'm not sure that this should be in the cache component, it merely uses that, maybe Site or Utility instead?). It really is the rebuild part in this whole thing.

    What the current rebuild() method really does is a safe bootstrap and then rebuild.

    What about:

    ::rebuild() (or rebuildAll()?) instead of flushAllCaches()
    ::safeBootstrapAndRebuild() or something like that for the current rebuild()?

  3. +++ b/core/lib/Drupal/Core/Cache/Rebuilder.php
    @@ -0,0 +1,208 @@
    +   * Flushes all persistent caches, resets all variables, rebuilds all data
    +   * structures.
    +   * At times, it is necessary to re-initialize the entire system to account for
    +   * changed or new code. This function:
    +   * - Clears all persistent caches:
    +   *   - The bootstrap cache bin containing base system, module system, and
    +   *     theme system information.
    +   *   - The common 'default' cache bin containing arbitrary caches.
    +   *   - The page cache.
    +   *   - The URL alias path cache.
    +   * - Resets all static variables that have been defined via drupal_static().
    +   * - Clears asset (JS/CSS) file caches.
    +   * - Updates the system with latest information about extensions (modules and
    +   *   themes).
    +   * - Updates the bootstrap flag for modules implementing bootstrap_hooks().
    +   * - Rebuilds the full database schema information (invoking hook_schema()).
    +   * - Rebuilds data structures of all modules (invoking hook_rebuild()). In
    +   *   core this means
    +   *   - blocks, node types, date formats and actions are synchronized with the
    +   *     database
    +   *   - The 'active' status of fields is refreshed.
    +   * - Rebuilds the menu router.
    

    I'm always torn between doing 1:1 conversions to methods and actually improving things.

    I'm pretty sure that list of persistent caches is way outdated, URL alias for example isn't its own cache bin anymore and doesn't seem worth mentioning here. Honestly, I'd just drop that ist and just say that all registered cache bins are cleared?

    The part below rebuild is also outdated, the only thing that still implements that hook is block.module and the only thing that it does is disable blocks that are in invalid regions.

    On the other side, we could maybe mention new things that are happening that are more important, like invalidating twig caches, and that kernel rebuild/service container invalidation?

voleger’s picture

StatusFileSize
new30.15 KB

Rerrolled.
Addressed #16.1 and #16.2

andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new4.14 KB
new30.72 KB

Re-roll + fix for \Drupal\system\Controller\DbUpdateController::triggerBatch()

voleger’s picture

Status: Needs review » Reviewed & tested by the community

Looks good +1 for RTBC

voleger’s picture

voleger’s picture

Assigned: Unassigned » voleger
Status: Reviewed & tested by the community » Needs work

NW regarding #16

voleger’s picture

Assigned: voleger » Unassigned
Status: Needs work » Needs review
StatusFileSize
new30.69 KB
new31.08 KB
new6.06 KB

Added legacy test, addressed #16.3, and simplified the ::safeBootstrapAndRebuild to ::safeBootstrap.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

voleger’s picture

Version: 8.9.x-dev » 9.1.x-dev
StatusFileSize
new30.46 KB

Reroll against 9.0.x
Also, there was the usage of the deprecated prepageLegacyRequest method, so it was replaced with appropriate replacement.

Status: Needs review » Needs work

The last submitted patch, 24: 3014752-24.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

voleger’s picture

Status: Needs work » Needs review
StatusFileSize
new30.47 KB
new1.4 KB

Missed updates in the deprecation messages.

Status: Needs review » Needs work

The last submitted patch, 26: 3014752-26.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

voleger’s picture

StatusFileSize
new31.49 KB
new858 bytes

New required replacement detected)

voleger’s picture

Status: Needs work » Needs review
voleger’s picture

StatusFileSize
new30.58 KB

Reroll.
Took into account changes from #3119373: Configuration synchronisation that both enables & configures a module fails and drupal_flush_all_caches()

The 9.1.x branch is opened, so reviews are welcome.

voleger’s picture

StatusFileSize
new30.34 KB
new654 bytes

Fixed reroll issue

The last submitted patch, 31: 3014752-31.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Status: Needs review » Needs work

The last submitted patch, 32: 3014752-32.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

swatichouhan012’s picture

Status: Needs work » Needs review
StatusFileSize
new30.38 KB
new813 bytes

Remove ununsed use statements from patch and coding fixes.

voleger’s picture

StatusFileSize
new33.12 KB
new3.04 KB

Accidentally removed the body of the method. Restoring.

daffie’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

Patch no longer applies. Needs a reroll.

kishor_kolekar’s picture

Assigned: Unassigned » kishor_kolekar
kishor_kolekar’s picture

Assigned: kishor_kolekar » Unassigned
Status: Needs work » Needs review
StatusFileSize
new33.14 KB

Re-roll patch #36.
please review

Status: Needs review » Needs work

The last submitted patch, 39: 3014752-39.patch, failed testing. View results

mrinalini9’s picture

Status: Needs work » Needs review
StatusFileSize
new34.25 KB
new951 bytes

Fixing test case failure issues in #39, please review.

Status: Needs review » Needs work

The last submitted patch, 41: 3014752-41.patch, failed testing. View results

mrinalini9’s picture

Assigned: Unassigned » mrinalini9
mrinalini9’s picture

Assigned: mrinalini9 » Unassigned
Status: Needs work » Needs review
StatusFileSize
new34.25 KB
new513 bytes

Fixed test case failure in #41, please review.

voleger’s picture

Status: Needs review » Needs work

+++ b/core/tests/Drupal/KernelTests/Core/DrupalKernel/RebuildLegacyTest.php
@@ -15,7 +15,7 @@
- protected function setUp() {
+ protected function setUp(): void

Missed { in method definition

protected function setUp(): void {

deepak goyal’s picture

Assigned: Unassigned » deepak goyal
deepak goyal’s picture

StatusFileSize
new34.26 KB
new519 bytes

Hi @voleger
Updated patch please take a look.

deepak goyal’s picture

Assigned: deepak goyal » Unassigned
Status: Needs work » Needs review
voleger’s picture

+1 for RTBC. But still needs additional review, as I worked on the patches

andypost’s picture

Status: Needs review » Needs work

The function needs be kept, NW for that

+++ b/core/includes/common.inc
@@ -512,89 +512,14 @@ function show(&$element) {
-function _drupal_flush_css_js() {
-  // The timestamp is converted to base 36 in order to make it more compact.
-  Drupal::state()->set('system.css_js_query_string', base_convert(REQUEST_TIME, 10, 36));

+++ b/core/lib/Drupal/Core/Cache/Rebuilder.php
@@ -0,0 +1,201 @@
+  public static function flushCssJs() {
+    // The timestamp is converted to base 36 in order to make it more compact.
+    \Drupal::state()->set('system.css_js_query_string', base_convert(REQUEST_TIME, 10, 36));

This function is used in contrib http://grep.xnddx.ru/search?text=_drupal_flush_css_js

SO should be deprecated to not break it

voleger’s picture

But it's an internal function. It has not to be used by definition. https://www.drupal.org/core/d8-bc-policy#underscore

kostyashupenko’s picture

Issue tags: -Needs reroll
voleger’s picture

Status: Needs work » Needs review
StatusFileSize
new35.3 KB
new2.18 KB

Addressed #50

andypost’s picture

Status: Needs review » Needs work

@voleger Thank you, it looks mostly ready, just few places needs some work

  1. +++ b/core/lib/Drupal/Core/Cache/Rebuilder.php
    @@ -0,0 +1,201 @@
    +   * @param mixed $class_loader
    +   *   The class loader. Normally Composer's ClassLoader, as included by the
    +   *   front controller, but may also be decorated; e.g.,
    +   *   \Symfony\Component\ClassLoader\ApcClassLoader,
    +   *   \Symfony\Component\ClassLoader\WinCacheClassLoader,
    +   *   or \Symfony\Component\ClassLoader\XcacheClassLoader.
    

    There's no such classloaders in 9.x https://www.drupal.org/node/3116384

    Looks we should \Composer\Autoload\ClassLoader as \Drupal\Core\DrupalKernel::__construct() defines

  2. +++ b/core/lib/Drupal/Core/Cache/Rebuilder.php
    @@ -0,0 +1,201 @@
    +   * @todo Try to clear page/JS/CSS caches last, so cached pages can still be
    +   *   served during this possibly long-running operation. (Conflict on
    +   *   bootstrap cache though.)
    +   * @todo Add a global lock to ensure that caches are not primed in concurrent
    +   *   requests.
    

    both todo needs issues and links

  3. +++ b/core/lib/Drupal/Core/Cache/Rebuilder.php
    @@ -0,0 +1,201 @@
    +  public static function rebuildAll() {
    +    $module_handler = \Drupal::moduleHandler();
    +    // Flush all persistent caches.
    +    // This is executed based on old/previously known information, which is
    +    // sufficient, since new extensions cannot have any primed caches yet.
    +    $module_handler->invokeAll('cache_flush');
    

    no reason for the variable as it used only once and redefined later

  4. +++ b/core/lib/Drupal/Core/Cache/Rebuilder.php
    @@ -0,0 +1,201 @@
    +  public static function flushCssJs() {
    +    // The timestamp is converted to base 36 in order to make it more compact.
    +    \Drupal::state()->set('system.css_js_query_string', base_convert(REQUEST_TIME, 10, 36));
    

    not sure REQUEST_TIME should be used in new code

voleger’s picture

Status: Needs work » Needs review
StatusFileSize
new35.23 KB
new2.2 KB

Thanks for the review @andypost
Addressed #54:
1. Done. Used the same definitions from DrupalKernel.
2. Filled #3160185: [META] Improve Cache rebuild process. Added the link for the todo items.
3. Done
4. Done

voleger’s picture

#55 is ready for another round of the review

voleger’s picture

Title: Convert drupal_rebuild function into Cache static method » Convert drupal_rebuild(), drupal_flush_all_caches() and _drupal_flush_css_js() functions into cache Rebuilder class
Issue summary: View changes

Updated CR, IS and title to reflect actual scope of the task.

andypost’s picture

It still needs to update summary, and then it looks ready for commiter's eyes

voleger’s picture

Feel free to update IS, if I miss something.

andypost’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +@deprecated

Looks good to go! Thank you @voleger

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/lib/Drupal/Core/Cache/Rebuilder.php
    @@ -0,0 +1,198 @@
    +  /**
    +   * Rebuilds all caches even when Drupal itself does not work.
    +   *
    +   * @param mixed $class_loader
    +   *   The class loader. Normally \Composer\Autoload\ClassLoader, as included by
    +   *   the front controller, but may also be decorated.
    +   * @param \Symfony\Component\HttpFoundation\Request $request
    +   *   The current request.
    +   *
    +   * @see rebuild.php
    +   */
    +  public static function safeBootstrap($class_loader, Request $request) {
    

    I'm not convinced this should be here. The only place that utility.inc is loaded from is from rebuild.php. This code is rescue code and for me does not belong in a class that is autoloadable by regular Drupal. If we want to remove it from utlity.inc then I think we should consider moving it to rebuild.php. There are usages in contrib but one has to wonder why drupal_flush_all_caches() was not the correct thing for them to use - http://grep.xnddx.ru/search?text=drupal_rebuild%28&filename=

  2. +++ b/core/lib/Drupal/Core/Cache/Rebuilder.php
    @@ -0,0 +1,198 @@
    +  /**
    +   * Changes the dummy query string added to all CSS and JavaScript files.
    +   *
    +   * Changing the dummy query string appended to CSS and JavaScript files forces
    +   * all browsers to reload fresh files.
    +   *
    +   * @internal
    +   */
    +  public static function flushCssJs() {
    +    // The timestamp is converted to base 36 in order to make it more compact.
    +    \Drupal::state()->set('system.css_js_query_string', base_convert(\Drupal::time()->getRequestTime(), 10, 36));
    +  }
    
    +++ b/core/modules/system/src/Controller/DbUpdateController.php
    @@ -206,7 +207,7 @@ public function handle($op, Request $request) {
    +    Rebuilder::flushCssJs();
    

    We're changing something that is internal - ie. marked with an _ to a public static function. But there is a tonne of usage in contrib - http://grep.xnddx.ru/search?text=_drupal_flush_css_js&filename= . I think we should be asking ourselves if this is the right place to put this. Or is there a core service that already interacts with this state that should be responsible for doing this. Or should we be adding a little service to do this.

voleger’s picture

Added service for managing query string for css and js files. In some cases the state service injected in classes, where new service used, become redundant.

Moved drupal_rebuild function into rebuild.php file.

voleger’s picture

Status: Needs work » Needs review

Need some feedback for the last changes

andypost’s picture

Sadly merge requests provide no link in mail to check interdiff)

voleger’s picture

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

daffie’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

The MR needs to be rebased to 9.2.x.

daffie’s picture

All the deprecations need to be changed from 9.1.0 to 9.2.0.

voleger’s picture

Status: Needs work » Needs review

Addressed all review threads and #69 comment

daffie’s picture

Status: Needs review » Needs work

At lot of testbot failures because of: "$state parameter is deprecated since drupal:9.2.0 and removed from drupal:10.0.0" and "$query_string parameter is added since drupal:9.2.0 and is required from drupal:10.0.0".

voleger’s picture

spokje’s picture

Assigned: Unassigned » spokje
spokje’s picture

Assigned: spokje » Unassigned

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

voleger’s picture

Status: Needs work » Needs review

Rebased against 9.3.x branch

daffie’s picture

Status: Needs review » Needs work
Issue tags: -Needs reroll

The testbot is not happy.

sharique’s picture

@voleger I think you need to rebase your branch.
I ran the tests and following is the results

Tests: 7258, Assertions: 14538, Errors: 5, Failures: 3, Warnings: 1, Skipped: 3, Incomplete: 1

Much less than that of the testbot.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

andypost’s picture

I think deprecation of drupal_rebuild() could use separate issue as it used only in front controller and few places in contrib http://grep.xnddx.ru/search?text=drupal_rebuild

then we can get rid of core/includes/utility.inc in D10

yogeshmpawar made their first commit to this issue’s fork.

yogeshmpawar’s picture

Status: Needs work » Needs review
alexpott’s picture

I think this issue could do with coming in 2 parts. One that deals with the new CSS / JS query string service and converting usages of _drupal_flush_css_js() and anything that accesses the 'system.css_js_query_string' state directly. And another that deals with drupal_flush_all_caches() and drupal_rebuild().

alexpott’s picture

Status: Needs review » Needs work

Also the change here needs to be made in light of changes to drupal_flush_all_caches(). It some ways it might be best to start a fresh - or at least copying the entire current contents of drupal_flush_all_caches() again - it now accepts a $kernel argument. And that needs to be copied over.

ravi.shankar made their first commit to this issue’s fork.

ravi.shankar’s picture

Fixed drupal CS issue of #81, still needs work for comments #83, #84.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

andypost’s picture

Version: 9.5.x-dev » 10.1.x-dev

as a feature

kim.pepper’s picture

kim.pepper’s picture

Title: Convert drupal_rebuild(), drupal_flush_all_caches() and _drupal_flush_css_js() functions into cache Rebuilder class » [PP-1] Convert drupal_rebuild(), drupal_flush_all_caches() and _drupal_flush_css_js() functions into cache Rebuilder class
Status: Needs work » Postponed

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

donquixote’s picture

Why not introduce events for this?

One event for "container rebuilt".
Another event for "flush all caches". This could be triggered by "container rebuilt" so subscribers only need to subscribe to one of them, not both.

In DrupalKernel::initializeContainer(), we add this:

     // If there is no container and no cached container definition, build a new
     // one from scratch.
     if (!isset($container) && !isset($container_definition)) {
       $container = $this->compileContainer();
 
       // Only dump the container if dumping is allowed. This is useful for
       // KernelTestBase, which never wants to use the real container, but always
       // the container builder.
       if ($this->allowDumping) {
         $dumper = new $this->phpArrayDumperClass($container);
         $container_definition = $dumper->getArray();
       }
+
+      /** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $event_dispatcher */
+      $event_dispatcher = $container->get('event_dispatcher');
+      $event_dispatcher->dispatch(new ResetEvent(), self::EVENT_CONTAINER_REBUILT);
     }

Or maybe there is an even better place where this event should be fired.

----

Yes, I saw CONTAINER_INITIALIZE_SUBREQUEST_FINISHED, but I am not sure I understand what it does.
It looks like it has a different purpose, and does not fire on cache rebuild exclusively.

kim.pepper’s picture

Title: [PP-1] Convert drupal_rebuild(), drupal_flush_all_caches() and _drupal_flush_css_js() functions into cache Rebuilder class » Convert drupal_rebuild(), drupal_flush_all_caches() and _drupal_flush_css_js() functions into cache Rebuilder class
Status: Postponed » Active
donquixote’s picture

New issue to explore the events idea: #3375976: Use events to flush all caches on container rebuild
I don't think it is mutually exclusive with this one, but let's see where it goes.

kim.pepper’s picture

Status: Active » Needs review

I took the approach of tagged services. I think it works quite nicely. Would be interested in some early feedback before I go much further.

donquixote’s picture

I took the approach of tagged services. I think it works quite nicely. Would be interested in some early feedback before I go much further.

Interesting. This is a valid alternative to events.

Can we make a good argument why cache tags + interface is preferable over events?
I don't have a clear opinion one way or the other, but I am curious, and I think it is good if we can describe why we do it this way.

Currently, event subscribers are more verbose, but I think we should move towards using the #[AsEventSubscriber] attribute from symfony.
Service tags + interface means that the service always needs to implement the interface, and the method always needs to have the same name.

I think in isolation it is hard to determine which is better.

donquixote’s picture

Btw, at some point we could change drupal_rebuild() and DrupalKernel so that we call cache rebuild from the kernel instead of from drupal_rebuild(). But at that point we can also question other parts of the process.

smustgrave’s picture

Status: Needs review » Needs work

Seems a number of failures in MR 4430

kim.pepper’s picture

Hoping for some more feedback before going much further.

smustgrave’s picture

Status: Needs work » Needs review

Gotcha will put back to review then.

smustgrave’s picture

Have posted in #contribute to try and get some eyes but no luck so far.

kim.pepper’s picture

This needs an IS and CR update since #3358336: Deprecate _drupal_flush_css_js() and create a new AssetQueryString cache service went in. Might also be worth adding the options discussed here: i.e. tagged services vs events.

kim.pepper’s picture

catch’s picture

For me on tagged services vs. events I like the simplicity of https://git.drupalcode.org/project/drupal/-/merge_requests/4430/diffs#c8...

I also went for tagged services in #3257725: Add a cache prewarm API.

Part of this is a long-standing dislike for the Symfony Event API though, for here specifically my main reason is the Event class. There's no state to pass around here, there's no need to be able to stop propagation etc. we just want to run a method with no arguments and no return value.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new90 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

kim.pepper’s picture

Status: Needs work » Needs review

Rebased on 11.x

kim.pepper’s picture

Sorry @catch Do you mind pasting the code you are referring to in #108? I think the link got broken in the rebase.

catch’s picture

diff --git a/core/lib/Drupal/Core/Asset/AssetQueryString.php b/core/lib/Drupal/Core/Asset/AssetQueryString.php
index fa0d6c20805b4a423b99bca10247a2144a53a2b3..b49fa322369bad5b4ee5daea31fbe2925f70bdea 100644
--- a/core/lib/Drupal/Core/Asset/AssetQueryString.php
+++ b/core/lib/Drupal/Core/Asset/AssetQueryString.php
@@ -5,6 +5,7 @@
 namespace Drupal\Core\Asset;
 
 use Drupal\Component\Datetime\TimeInterface;
+use Drupal\Core\Cache\CacheClearerInterface;
 use Drupal\Core\State\StateInterface;
 
 /**
@@ -13,7 +14,7 @@
  * The string changes on every update or full cache flush, forcing browsers to
  * load a new copy of the files, as the URL changed.
  */
-class AssetQueryString implements AssetQueryStringInterface {
+class AssetQueryString implements AssetQueryStringInterface, CacheClearerInterface {
 
   /**
    * The key used for state.
@@ -48,4 +49,11 @@ public function get(): string {
     return $this->state->get(self::STATE_KEY, '0');
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function clearCache(): void {
+    $this->reset();
+  }
+
 }

This is what I meant by the simplicity of tagged services, it very much suits having neither context to pass in nor anything to return.

smustgrave’s picture

Status: Needs review » Needs work

Thanks @catch. Moving to NW for that and issue tags.

catch’s picture

That's not a needs work issue, it's supporting the current implementation over using an event.

New symfony events with attribute subscription and no event class are a bit better than how core currently uses events, but don't think we fully support that yet.

kim.pepper’s picture

Status: Needs work » Needs review

Merge in 11.x

smustgrave’s picture

Status: Needs review » Needs work

This something that should be aiming for 10.2

smustgrave’s picture

Status: Needs work » Needs review

Didn't mean to change status.

kim.pepper’s picture

I'm not sure whether we should wait for #3257725: Add a cache prewarm API ? Is there any overlap?

catch’s picture

Doesn't need to be postponed on #3257725: Add a cache prewarm API IMO, any overlap won't be too hard to deal with.

voleger’s picture

Issue summary: View changes

Removed mention of _drupal_flush_css_js() function as it is already deprecated.

voleger’s picture

Title: Convert drupal_rebuild(), drupal_flush_all_caches() and _drupal_flush_css_js() functions into cache Rebuilder class » Convert drupal_rebuild() and drupal_flush_all_caches() functions into cache Rebuilder class
smustgrave’s picture

Status: Needs review » Needs work

Seems to have test failures.

sorlov made their first commit to this issue’s fork.

gapple changed the visibility of the branch 3014752-convert-functions-into-cache-rebuilder-class to hidden.

gapple changed the visibility of the branch 3014752-convert-functions-into-cache-rebuilder-class to active.

gapple’s picture

MR 4430 has significantly different code, but no explanation for the changes.
I've rebased MR 9 from 9.4.x to 11.x

I skipped over too many comments and missed context.

gapple changed the visibility of the branch 3014752-convert-functions-into-cache-rebuilder-class to hidden.

gapple changed the visibility of the branch 3014752-cache-rebuilder to hidden.

gapple’s picture

Title: Convert drupal_rebuild() and drupal_flush_all_caches() functions into cache Rebuilder class » Convert drupal_flush_all_caches() function to a Cache Clearer service
Issue summary: View changes
Issue tags: -Needs issue summary update

I was confused by the title & summary still referencing drupal_rebuild, which is no longer present in the latest MR (and potentially covered by prewarming in the future instead?).

Updated title & summary to reflect current changes only applying drupal_flush_all_caches

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

voleger changed the visibility of the branch 3014752-deprecate-flush-all-caches to hidden.

voleger changed the visibility of the branch 11.x to active.

voleger changed the visibility of the branch 3014752-deprecate-flush-all-caches to active.

voleger changed the visibility of the branch 11.x to hidden.