WordPress.org

Make WordPress Core

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#25557 closed defect (bug) (invalid)

wp_next_scheduled May Return Wrong Timestamp

Reported by: model13 Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.6.1
Component: Cron API Keywords:
Focuses: Cc:

Description

The wp_next_scheduled function is written with the assumption that the combination of $hook and $args will provide a unique identifier within an array of cron jobs.

In practice some plugins are scheduling multiple cron jobs with the same hook and args (usually an empty array). The function returns the timestamp of the first match, thus returning the wrong timestamp.

(Will attempt to attach an image of sample output comparing the actual timestamp for three cron jobs to the time returned by wp_next_scheduled.)

This may be regarded as an error on the part of plugin developers. However, as there didn't seem to be a previous report on this issue, it seemed to be worth noting.

Attachments (1)

error-in-action.jpg (243.1 KB) - added by model13 8 years ago.
Screen Shot of Cron Jobs

Download all attachments as: .zip

Change History (5)

@model13
8 years ago

Screen Shot of Cron Jobs

#1 @leewillis77
8 years ago

wp_net_scheduled is documented as returning "the time the scheduled event will next occur (unix timestamp)." [My Bold]. So it seems this is doing exactly as intended. The next time that hook will be fired with those arguments is 2013-08-02 18:19:15. I'm not sure what else you'd expect it to return?

#2 @nacin
8 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Indeed, the function is called wp_next_scheduled(). wp_get_schedule() would give you all scheduled occurrences for a single hook + arguments pairing.

#3 @model13
8 years ago

Wish this hadn't been closed quite so quickly. I was trying to note that in a couple of cases the list of cron jobs retrieved by _get_cron_array() in this function was not in chronological order, and so the first match was not necessarily the next-scheduled event (I know the screen shot provided doesn't illustrate this). Possibly it's an anomaly caused by a plugin; I've been trying to replicate it to further illustrate the point.

#4 @leewillis77
8 years ago

Cron entries are sorted when they're inserted (See http://core.trac.wordpress.org/browser/trunk/src/wp-includes/cron.php#L80). If cron items are coming back in the wrong order, it's likely that whatever is adding them is doing them manually rather than using wp_schedule_event(). If you manager to reproduce this with entries added via the standard API, you can of course re-open this.

Note: See TracTickets for help on using tickets.