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.
| Comment | File | Size | Author |
|---|---|---|---|
| #59 | drupal-batch-cleanup-2803061-59.patch | 14.95 KB | alex.bukach |
| #56 | clean_old_or_failed_batches.patch | 619 bytes | nsalves |
| #48 | 2803061-48.patch | 14.6 KB | markus_petrux |
| #46 | reroll_diff_36-46.txt | 3.77 KB | linhnm |
| #46 | 2803061-46.patch | 14.14 KB | linhnm |
Issue fork drupal-2803061
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
Comment #2
br0kenComment #4
br0kenFixed typo in service name.
Comment #5
br0kenChanges:
BatchQueueInterfaceinterface for batch queuesBatchQueueInterfaceinterfaceComment #6
br0kenA set of short explanations of changes:
Moved out of the loop to reduce overhead by method execution which will always return the same result.
Check for queue to prevent fatal errors.
Moved above the loop since result will be the same on every iteration.
Moved above the loop since result will be the same on every iteration.
Check queue for existence before usage to not get fatal error.
Comment #7
br0kenComment #9
br0kenComment #11
br0kenComment #12
br0kenComment #13
br0kenComment #14
br0kenComment #15
br0kenAttention required!
Comment #23
hkirsman commented+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.
Comment #24
jungleTagging "Bug Smash Initiative"
Comment #25
quietone commentedSetting to NW for the reroll.
Comment #26
quietone commentedActually do it this time.
Comment #27
vsujeetkumar commentedRe-roll patch created, Please review.
Comment #30
xavier.massonRe-roll patch #27 with the branch 9.2.x.
Comment #31
vsujeetkumar commentedFixed "Custom Command Fail" issues for 9.3.x.
Comment #32
jofitzRemoved out of date "Needs reroll" tag
Comment #33
quietone commentedDoesn't apply. The following is from a light read of the changes.
This is confusing to me. It is not part of QueueInterface but it extends from QueueInterface.
What are items? Are they keys? Maybe one summary line and then another paragraph to add more description?
Probably should be 'Tests an incorrectly implemented batch queue.
Add another comment here or in the code to explain what 'incorrectly implemented' means.
s/is not implements/does not implement/
$i is not used.
Assertions shouldn't be using the message field. Instead use a command to explain what $found means.
Comment #34
ankithashettyRe-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
$iis being used in the following line: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!
Comment #36
xavier.massonReroll patch from the latest 9.4.x and another one for the 9.3.x.
Comment #39
daften commentedThis 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
Comment #40
bhanu951 commented@daften : Agreed, created a new follow-up issue #3333713: add BatchQueueInterface for adding BatchQueueInterface.
Comment #41
bhanu951 commentedComment #42
bhanu951 commentedComment #44
bhanu951 commentedComment #45
smustgrave commentedThis 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
Comment #46
linhnm commentedRerolled patch from #36 against 9.5.4. No other changes.
Comment #48
markus_petrux commentedPatch rerolled for the 10.2.x branch
Comment #49
meanderix commentedHere's another consequence of this bug. We discovered that we have 16000+ old batch jobs left in the queue table.
_drush_batch_worker()invokesDrupal\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:checkandlocale:upgradetakes 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.Comment #50
bhanu951 commentedRebased against head and moved changes to OOPS hooks.
Comment #51
smustgrave commentedMR and patch seem very different, was some stuff missed?
Comment #52
bhanu951 commented@smustgrave issue is rescoped check #40, patch rerolls are not correct. MR 3248 is relevant.
Comment #53
bhanu951 commentedComment #54
smustgrave commentedThen the needs tests tag still seems relevant hear so moving back to NW thanks
Comment #56
nsalves commentedLeaving here a simpler patch to cleanup batches on system_cron()
Comment #58
alex.bukach commentedRe-rolled #48 against 11.3.
Comment #59
alex.bukach commentedFixed the patch.