humanmade / Cavalcade

A better wp-cron. Horizontally scalable, works perfectly with multisite.
https://engineering.hmn.md/projects/cavalcade/
Other
528 stars 46 forks source link

Cannot unschedule more than 100 events with a given hook #125

Open johnbillion opened 1 year ago

johnbillion commented 1 year ago

Deleting all events with a given hook via wp cron event unschedule <hook> or wp_unschedule_hook( <hook> ) will only delete a maximum of 100 events. If there are more than 100 events with that hook then the remainder will not be deleted and the command will need to be run multiple times.

This problem was identified on a site with a runaway process that had scheduled thousands of events with the same hook name but different parameters. Unscheduling the events only unscheduled 100 at a time.

This is caused by the \HM\Cavalcade\Plugin\Connector\pre_clear_scheduled_hook function that's hooked into pre_clear_scheduled_hook which queries for a maximum of 100 jobs.

The fix would be to either repeat this query in a loop until there are no more matches, or to remove the limit on the query, although the latter risks exhausting memory if there's an exceptionally high number of jobs with that hook.