WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 10 months ago

#18997 reopened feature request

Adding wp_unschedule_hook function

Reported by: arena Owned by: arena
Milestone: Future Release Priority: normal
Severity: normal Version: 3.3
Component: Cron API Keywords: has-patch needs-testing
Focuses: Cc:

Description

Unschedule all previously scheduled cron job for a hook.

Can be usefull for plugins when deactivating to clean up the cron queue

The $hook parameter is required, so that the events can be identified.

@param string $hook Action hook, the execution of which will be unscheduled.

Attachments (5)

#18997.2.patch (929 bytes) - added by arena 3 years ago.
#18997.patch (929 bytes) - added by arena 3 years ago.
#cron.patch (4.2 KB) - added by arena 3 years ago.
18997.patch (905 bytes) - added by mordauk 11 months ago.
18997-tests.patch (1.1 KB) - added by mordauk 11 months ago.

Download all attachments as: .zip

Change History (28)

comment:1 @SergeyBiryukov4 years ago

Isn't that what wp_clear_scheduled_hook() already does?

comment:2 @dd324 years ago

Isn't that what wp_clear_scheduled_hook() already does?

That clears by hook + args, It's common place to hook multiple "instances" of a job with varying parameters (ie. Post ID to process) to a single hook, there's no API to clear all events for that hook at present. I've seen this requested before, it might be on trac, or elsewhere, I'm not sure.

comment:3 @SergeyBiryukov4 years ago

That makes sense, I had a feeling I must have missed something.

comment:4 @arena4 years ago

  • Keywords dev-feedback removed

dd32 is right :

"there's no API to clear all events for a hook at present"

i coded it myself for a plugin but think it ought to be in the API

comment:5 @arena4 years ago

just giving a bump to this ticket to have it included in 3.4

thank you

comment:6 @Viper007Bond4 years ago

Patch seems to be okay although it will need to be cleaned up to meet WordPress coding standards.

comment:7 follow-up: @arena4 years ago

Done !

comment:8 in reply to: ↑ 7 @Viper007Bond3 years ago

Replying to arena:

Done !

Just one more minor nitpick:

if ( empty( $crons[$timestamp] ) )
	unset( $crons[$timestamp] );

We do if's like that (line break in there) and with some extra spaces inside of the empty(). :)

@arena3 years ago

@arena3 years ago

comment:9 @arena3 years ago

Done !

comment:10 @SergeyBiryukov3 years ago

  • Milestone changed from Awaiting Review to 3.4

comment:11 @nacin3 years ago

Perhaps too clever, or slower, but instead of if ( empty ) unset();, we could also hold off until before we call _set_cron_array() and then do $cron = array_filter( $cron ); That will clear out empty values.

comment:12 follow-up: @arena3 years ago

Nacin, your point is interesting.

May be this should be done once for all in

_set_cron_array()

simplifying all other functions calling _set_cron_array()

comment:13 in reply to: ↑ 12 @arena3 years ago

Replying to arena:

Nacin, your point is interesting.

May be this should be done once for all in

_set_cron_array()

simplifying all other functions calling _set_cron_array()

since nacin tip, i reviewed the cron api and made some code changes :

  • to have _get_cron_array closer to _set_cron_array when possible
  • generalization of test empty( $crons ) after _get_cron_array()
  • added a '$action' argument in function _set_cron_array()

patch is named #cron

@arena3 years ago

comment:14 @nacin3 years ago

  • Milestone changed from 3.4 to Future Release

A good idea, but too late for 3.4.

comment:15 follow-up: @arena3 years ago

not fair, nacin opened 5 months ago ...

comment:16 in reply to: ↑ 15 @nacin3 years ago

Replying to arena:

not fair, nacin opened 5 months ago ...

Cron had a few changes already in 3.4, specifically microtime locking. This however digs a bit deeper into the API and deserves a more thorough review. If we end up setting or getting the cron array out of order, we risk a bug or race condition. I think the patch will be great for 3.5.

comment:17 @nacin3 years ago

  • Keywords needs-unit-tests added

I am fine with reviving #18997.patch for 3.4, but we will first need unit tests: http://unit-tests.trac.wordpress.org/browser/wp-testcase/test_cron.php.

Last edited 3 years ago by nacin (previous) (diff)

comment:18 @arena3 years ago

  • Resolution set to maybelater
  • Status changed from new to closed

comment:19 @Viper007Bond3 years ago

  • Resolution maybelater deleted
  • Status changed from closed to reopened

Please don't close valid tickets. Even if you no longer desire the feature, I'm sure others do. Thanks. :)

@mordauk11 months ago

comment:20 @mordauk11 months ago

18997.patch is a refreshed patch that improves some formatting, adds brackets, and uses the src directory structure.

@mordauk11 months ago

comment:21 @mordauk11 months ago

  • Keywords needs-unit-tests removed

18997-tests.patch introduces the test_unschedule_hook() test.

comment:22 @mordauk11 months ago

  • Keywords needs-testing added

comment:23 @jpswade10 months ago

Had a problem today where I accidentally scheduled the function that adds the schedule.

Although I spotted and fixed the error, it was impossible to remove the hook without this function.

http://wordpress.stackexchange.com/questions/39681/delete-all-scheduled-events-with-a-particular-hook

This case here outlines the same issue.

Note: See TracTickets for help on using tickets.