Fix/clean uses scheduled date 1107 (wp action-scheduler clean filtering by last_attempt_gmt instead of scheduled_date_gmt)#1348
Open
MagicGeny wants to merge 2 commits into
Conversation
Author
|
Hi @barryhughes! I have created this PR to address the issues with the Since this bugfix relies on the default value fix from my previous task (#1343 / #1346), I have branched this PR from that work to ensure everything functions correctly together. I have also written 4 new automated regression tests to cover these changes and ran them successfully in my local environment. Could you please take a look when you have a moment? I would really appreciate your feedback on whether this approach looks good to you, or if we should merge the previous default-value fix first. Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
wp action-scheduler cleancommand was incorrectly filtering actions by their last modified date (last_attempt_gmt) instead of their scheduled date (scheduled_date_gmt).The
--beforeparameter in the CLI command is documented as referring to the scheduled date — e.g.--before='31 days ago'should delete actions scheduled more than 31 days ago. However, the query was usinglast_attempt_gmt, so any action that had been retried or modified recently would escape cleanup even if it was scheduled far beyond the retention period.Example of the bug:
last_attempt_gmt) → incorrectly kept, even though it's well past the retention period.This meant that over time, a growing backlog of old actions would accumulate in the database if they had ever been retried or re-scheduled after their initial run.
Root Cause
ActionScheduler_QueueCleaner::clean_actions()always queried actions using themodifiedfield (last_attempt_gmt), regardless of who called it. Both the scheduled (cron) cleanup and the WP-CLI command used the samemodified-based filtering.The WP-CLI clean command should use the scheduled date (
scheduled_date_gmt) since the--beforeparameter refers to when the action was scheduled to run, not when it last executed.Changes
1.
classes/ActionScheduler_QueueCleaner.php$date_typeparameter toclean_actions()with two modes:'modified'(default) — filters bylast_attempt_gmt. Used by scheduled cron cleanup.'date'— filters byscheduled_date_gmt. Used by WP-CLI clean command.delete_old_actions()) always forces$date_type = 'modified', preserving backward compatibility and the existing execution budget optimization.2.
classes/WP_CLI/ActionScheduler_WPCLI_Clean_Command.phpclean()method now passes'date'as the 5th argument to$cleaner->clean_actions(), so the CLI correctly filters by scheduled date.3.
classes/data-stores/ActionScheduler_DBStore.phpclaim_idbefore deletion.{$wpdb->actionscheduler_claims}.4.
classes/data-stores/ActionScheduler_DBLogger.phpdefined('WP_CLI') && WP_CLI),clear_deleted_action_logs()now performs a direct DELETE query instead of the batched approach using scheduled actions.5.
classes/data-stores/ActionScheduler_wpCommentLogger.phpclear_deleted_action_logs()method that cleans up bothcommentmetaandcommentsrecords for deleted actions.action_scheduler_deleted_actionandaction_scheduler_canceled_corrupted_actionhooks.Testing Instructions
Automated Tests (4 new regression tests in
ActionScheduler_QueueCleaner_Test.php)last_attempt_gmtto simulate a recent retry:wp action-scheduler clean --before='31 days ago' --batch-size=100Regression Test
Verify that scheduled (cron) cleanup still works as expected:
last_attempt_gmt(within the last hour).wp cron event run action_scheduler_run_actions_cleanup_hook.modifieddate).Related