Changes how we cancel pending events. We now use clear all and also h…#296
Conversation
…ook in on cancel to fully remove them
WalkthroughAdds duplicate-event prevention to the post event processing flow. In class Process_Local_Post_Event, a private static method ensure_single_event(int $post_id) unschedules existing actions for a given post and a public static method handle_hard_delete(int $action_id) conditionally deletes scheduler actions. Calls to ensure_single_event were added where events are queued. Tests were updated and new tests added to verify duplicate removal and end-to-end queue cleanup. 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Fix all issues with AI agents
In `@src/Event/Process_Local_Post_Event.php`:
- Around line 140-148: The add_action/remove_action pairing in
ensure_single_event currently risks leaving the handle_hard_delete hook
registered if as_unschedule_all_actions throws; wrap the unschedule call in a
try/finally inside ensure_single_event so that add_action(self::class,
'handle_hard_delete') is always removed in the finally block via remove_action,
referencing ensure_single_event, as_unschedule_all_actions, add_action,
remove_action and the handle_hard_delete method to guarantee cleanup even on
exception.
In `@tests/Event/Test_Process_Local_Post_Event.php`:
- Line 381: There is a duplicated assignment in the test where $actions is
assigned twice; update the statement that calls $this->wpdb->get_results (the
line using $actions = $this->wpdb->get_results(...)) to remove the extra
duplicate assignment so it reads as a single assignment to $actions and
continues to fetch pending actions via $this->wpdb->get_results.
- Around line 119-138: The wpdb->insert call in the test uses 14 columns but
only supplies 12 format specifiers, causing a mismatch; update the format array
passed to $this->wpdb->insert (the third argument) to include a format specifier
for each column in the values array so counts match (refer to the insert call
that uses Process_Local_Post_Event::HANDLE and fields like action_id, hook,
status, scheduled_date_gmt, scheduled_date_local, priority, args, schedule,
group_id, attempts, last_attempt_gmt, last_attempt_local, claim_id,
extended_args) — ensure types align (e.g. %d for integers, %s for strings/null)
and the format array length equals the values array length.
🧹 Nitpick comments (1)
tests/Event/Test_Process_Local_Post_Event.php (1)
80-81: Remove unused$eventvariable.The static analysis tool flagged this: the
$eventvariable is instantiated but never used since the test now callsProcess_Local_Post_Event::add_to_queuestatically.♻️ Proposed fix
- // Create the event. - $event = new Process_Local_Post_Event(); - Process_Local_Post_Event::add_to_queue( $post_id );
…ook in on cancel to fully remove them
Changes proposed in this Pull Request
This pull request introduces significant improvements to how duplicate scheduled actions are handled for processing local posts. The main goal is to ensure that only one event per post ID exists in the action queue at any time, preventing database flooding and improving reliability. It also adds comprehensive tests to verify this behavior.
Duplicate event prevention and queue management:
ensure_single_eventtoProcess_Local_Post_Eventthat unschedules all existing actions for a given post ID before scheduling a new one, ensuring only one event per post is queued at any time. This method is now called in bothadd_to_queue_with_delayand the event handler itself. [1] [2]handle_hard_deleteto forcefully delete canceled actions, and ensured it is only temporarily hooked to avoid interfering with unrelated events.Testing and validation:
Test_Process_Local_Post_Eventto:These changes address issues with duplicate scheduled actions and ensure robust, predictable event processing.
Testing instructions
Mentions #294
Summary by CodeRabbit
Bug Fixes
Tests