Opened 19 months ago

Last modified 13 months ago

#18997 reopened feature request

Adding wp_unschedule_hook function

Reported by: arena Owned by: arena
Priority: normal Milestone: Future Release
Component: Cron Version: 3.3
Severity: normal Keywords: has-patch needs-unit-tests
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 (3)

#18997.2.patch (929 bytes) - added by arena 17 months ago.
#18997.patch (929 bytes) - added by arena 17 months ago.
#cron.patch (4.2 KB) - added by arena 17 months ago.

Download all attachments as: .zip

Change History (22)

Isn't that what wp_clear_scheduled_hook() already does?

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.

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

  • 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

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

thank you

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

comment:7 follow-up: ↓ 8   arena17 months ago

Done !

comment:8 in reply to: ↑ 7   Viper007Bond17 months 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(). :)

arena17 months ago

arena17 months ago

Done !

  • Milestone changed from Awaiting Review to 3.4

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: ↓ 13   arena17 months 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   arena17 months 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

arena17 months ago

  • Milestone changed from 3.4 to Future Release

A good idea, but too late for 3.4.

comment:15 follow-up: ↓ 16   arena14 months ago

not fair, nacin opened 5 months ago ...

comment:16 in reply to: ↑ 15   nacin14 months 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.

  • 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 14 months ago by nacin (previous) (diff)
  • Resolution set to maybelater
  • Status changed from new to closed
  • 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. :)

Note: See TracTickets for help on using tickets.