Problem/Motivation

For now, creating batches, using storage service, looks like \Drupal::service('batch.storage')->create($batch);. For instance, if batch defined as progressive, then it'll be created via the same construction inside of batch_process() function. Afterwards, when batch is finished and _batch_finished() executed, an entry will be deleted from storage by \Drupal::service('batch.storage')->delete($batch['id']);. Everything fine at the moment. My proposal is: take care about the storage and trigger the cleanup() method of the service inside of system_cron().

In addition to above, there could be a case when _batch_queue() function will try to construct wrong class. To resolve this, I propose to add an interface which will identify batch-related queues.

Proposed resolution

Add execution of \Drupal::service('batch.storage')->cleanup() to system_cron().

Remaining tasks

None.

User interface changes

None.

API changes

Add \Drupal\Core\Queue\BatchQueueInterface which must be implemented by every custom batch queue.

Data model changes

None.

Issue fork drupal-2803061

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

BR0kEN created an issue. See original summary.

br0ken’s picture

Assigned: br0ken » Unassigned
Status: Active » Needs review
StatusFileSize
new2.41 KB

Status: Needs review » Needs work

The last submitted patch, 2: core-cleanup_batches-2803061-2.patch, failed testing.

br0ken’s picture

Status: Needs work » Needs review
StatusFileSize
new2.41 KB
new459 bytes

Fixed typo in service name.

br0ken’s picture

StatusFileSize
new14.92 KB
new11.84 KB

Changes:

  1. Added BatchQueueInterface interface for batch queues
  2. Restrict batch queue definition to implement BatchQueueInterface interface
  3. Prevent possible fails when queue was wrongly implemented
br0ken’s picture

A set of short explanations of changes:

  1. +++ b/core/includes/batch.inc
    @@ -225,13 +225,14 @@
    +  $drupal_root = \Drupal::root();
    

    Moved out of the loop to reduce overhead by method execution which will always return the same result.

  2. +++ b/core/includes/batch.inc
    @@ -239,7 +240,7 @@
    +    if ($queue && $item = $queue->claimItem()) {
    

    Check for queue to prevent fatal errors.

  3. +++ b/core/includes/batch.inc
    @@ -399,6 +400,8 @@
    +  $drupal_root = \Drupal::root();
    

    Moved above the loop since result will be the same on every iteration.

  4. +++ b/core/includes/batch.inc
    @@ -399,6 +400,8 @@
    +  $date_formatter = \Drupal::service('date.formatter');
    

    Moved above the loop since result will be the same on every iteration.

  5. +++ b/core/includes/form.inc
    @@ -906,14 +909,15 @@ function _batch_populate_queue(&$batch, $set_id) {
    +    if ($queue = _batch_queue($batch_set)) {
    

    Check queue for existence before usage to not get fatal error.

br0ken’s picture

Issue summary: View changes

Status: Needs review » Needs work

The last submitted patch, 5: core-cleanup_batches-2803061-5.patch, failed testing.

br0ken’s picture

Status: Needs work » Needs review
StatusFileSize
new18.39 KB
new3.8 KB

Status: Needs review » Needs work

The last submitted patch, 9: core-cleanup_batches-2803061-9.patch, failed testing.

br0ken’s picture

Status: Needs work » Needs review
StatusFileSize
new18.49 KB
new4.29 KB
br0ken’s picture

Issue tags: +Dublin2016
br0ken’s picture

Title: Cleanup of batches is never performed » Cleanup of batches is never performed, add BatchQueueInterface
br0ken’s picture

Issue tags: -Dublin2016
br0ken’s picture

Attention required!

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

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

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

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.

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.

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

hkirsman’s picture

+1, I just found batches from 2017 in my db.

I also think that there should be warning for every batch job running in browser not to close tab/window. I mean it's same as upgrading your phone or computer and it also says not to shut down the device before it's finished. I think it's even possible to alert user with js and block closing the window until user agrees. Sometimes you might forget a long batch job and press update button on your computer which wants to then close browser.

jungle’s picture

Issue tags: +Bug Smash Initiative

Tagging "Bug Smash Initiative"

quietone’s picture

Issue tags: +Needs reroll

Setting to NW for the reroll.

quietone’s picture

Status: Needs review » Needs work

Actually do it this time.

vsujeetkumar’s picture

Status: Needs work » Needs review
StatusFileSize
new14.66 KB

Re-roll patch created, Please review.

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.

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.

xavier.masson’s picture

StatusFileSize
new14.73 KB
new4.73 KB

Re-roll patch #27 with the branch 9.2.x.

vsujeetkumar’s picture

StatusFileSize
new14.74 KB
new1.24 KB

Fixed "Custom Command Fail" issues for 9.3.x.

jofitz’s picture

Issue tags: -Needs reroll

Removed out of date "Needs reroll" tag

quietone’s picture

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

Doesn't apply. The following is from a light read of the changes.

  1. +++ b/core/lib/Drupal/Core/Queue/BatchQueueInterface.php
    @@ -0,0 +1,18 @@
    + * Batch API queue which is not part of QueueInterface.
    

    This is confusing to me. It is not part of QueueInterface but it extends from QueueInterface.

  2. +++ b/core/modules/system/tests/modules/error_service_test/src/Logger/TestLog.php
    @@ -11,8 +11,16 @@
    +   * An array of arrays with three items: "level", "context" and "message".
    

    What are items? Are they keys? Maybe one summary line and then another paragraph to add more description?

  3. +++ b/core/modules/system/tests/src/Functional/Batch/PageTest.php
    @@ -87,4 +90,38 @@ public function testBatchProgressMessages() {
    +   * Tests that batch queue was wrongly implemented.
    

    Probably should be 'Tests an incorrectly implemented batch queue.

    Add another comment here or in the code to explain what 'incorrectly implemented' means.

  4. +++ b/core/modules/system/tests/src/Functional/Batch/PageTest.php
    @@ -87,4 +90,38 @@ public function testBatchProgressMessages() {
    +    $message = sprintf('Batch queue "%s" is not implements "%s" interface.', DatabaseQueue::class, BatchQueueInterface::class);
    

    s/is not implements/does not implement/

  5. +++ b/core/modules/system/tests/src/Functional/Batch/PageTest.php
    @@ -87,4 +90,38 @@ public function testBatchProgressMessages() {
    +    foreach ($logger::$entries as $i => $entry) {
    

    $i is not used.

  6. +++ b/core/modules/system/tests/src/Functional/Batch/PageTest.php
    @@ -87,4 +90,38 @@ public function testBatchProgressMessages() {
    +    $this->assertTrue($found, sprintf('Log entry "%s" about wrong queue implementation was found.', $message));
    

    Assertions shouldn't be using the message field. Instead use a command to explain what $found means.

ankithashetty’s picture

Issue tags: -Needs reroll
StatusFileSize
new14.76 KB
new1.93 KB

Re-rolled the patch in #31 and addressed the changes specified in #33.4 and a part of #33.3.

I think the change mentioned in #33.5 is not needed as $i is being used in the following line:

+++ b/core/modules/system/tests/src/Functional/Batch/PageTest.php
@@ -87,4 +90,38 @@ public function testBatchProgressMessages() {
+        unset($logger::$entries[$i]);

Retaining status as "Needs work" to address the rest of the changes i.e., #33.1, #33.2, #33.3 (second part of it) and #33.6.

Thank you!

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.

xavier.masson’s picture

StatusFileSize
new14.18 KB
new3.3 KB
new14.47 KB
new3.14 KB

Reroll patch from the latest 9.4.x and another one for the 9.3.x.

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.

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

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

daften’s picture

This issue mixes 2 things, can we split up the BatchQueueInterface to a separate issue, since that's a feature request. And use this issue to simply fix the cleanup that was forgotten in #1998250: Move batch to pluggable storage?

Link to the commit where the cleanup was forgotten: https://git.drupalcode.org/project/drupal/-/commit/a2f15f28ebca3e589df6d...

If I'm right, we just need to add \Drupal::service('batch.storage')->cleanup(); where the db_delete query was removed

bhanu951’s picture

@daften : Agreed, created a new follow-up issue #3333713: add BatchQueueInterface for adding BatchQueueInterface.

bhanu951’s picture

Title: Cleanup of batches is never performed, add BatchQueueInterface » Cleanup of batches is never performed, add batch cleanup after execution of batch
Related issues: +#3333713: add BatchQueueInterface
bhanu951’s picture

Assigned: Unassigned » bhanu951

bhanu951’s picture

Assigned: bhanu951 » Unassigned
Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs tests

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

This could use test coverage

linhnm’s picture

StatusFileSize
new14.14 KB
new3.77 KB

Rerolled patch from #36 against 9.5.4. No other changes.

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.

markus_petrux’s picture

StatusFileSize
new14.6 KB

Patch rerolled for the 10.2.x branch

meanderix’s picture

Here's another consequence of this bug. We discovered that we have 16000+ old batch jobs left in the queue table. _drush_batch_worker() invokes Drupal\Core\Queue\DataBaseQueue::claimItem(), which generates a query similar to this:

SELECT * FROM queue q WHERE name='drupal_batch:123301:0' ORDER BY item_id ASC LIMIT 0, 1;

This query now takes about 6 seconds to execute!

Now, during an upgrade, drush commands such as locale:check and locale:upgrade takes forever to execute because of this (they repeatedly add new batch jobs). Even enabling a new module will have this effect, since it will invoke the locale module to look for translations.

bhanu951’s picture

Status: Needs work » Needs review

Rebased against head and moved changes to OOPS hooks.

smustgrave’s picture

Status: Needs review » Needs work

MR and patch seem very different, was some stuff missed?

bhanu951’s picture

Status: Needs work » Needs review

@smustgrave issue is rescoped check #40, patch rerolls are not correct. MR 3248 is relevant.

bhanu951’s picture

Issue summary: View changes
smustgrave’s picture

Status: Needs review » Needs work

Then the needs tests tag still seems relevant hear so moving back to NW thanks

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

nsalves’s picture

StatusFileSize
new619 bytes

Leaving here a simpler patch to cleanup batches on system_cron()

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.

alex.bukach’s picture

StatusFileSize
new14.41 KB

Re-rolled #48 against 11.3.

alex.bukach’s picture

StatusFileSize
new14.95 KB

Fixed the patch.