[wp-trac] [WordPress Trac] #42660: Single event scheduler
WordPress Trac
noreply at wordpress.org
Fri Nov 24 07:15:18 UTC 2017
#42660: Single event scheduler
-------------------------------+----------------------
Reporter: skarabeq | Owner:
Type: defect (bug) | Status: closed
Priority: normal | Milestone:
Component: Cron API | Version: 4.9
Severity: normal | Resolution: invalid
Keywords: reporter-feedback | Focuses:
-------------------------------+----------------------
Changes (by dd32):
* status: new => closed
* resolution: => invalid
* severity: major => normal
* milestone: Awaiting Review =>
Comment:
Replying to [comment:10 skarabeq]:
> @ocean90 with this code:
>
> {{{#!php
> <?php
> if ( ! wp_next_scheduled( 'single_action' ) ) {
> wp_schedule_single_event( strtotime( '2017-11-22 11:00' ),
'single_action' );
> }
> }}}
>
> isn't works like as expect. Still triggered event on every refresh on
website (doesn't matter where I'm)
I think what is happening here is:
- Pageload 1: See's there's no `single_action` scheduled, so queues it up.
Later in the pageload it sees there's crons that need running, so it
spawns the cron process.
- Cron pageload runs in background. removes cron entry.
- Pagelaod 2: The cron run has already processed the cron and removed its
entry from the database, so `wp_next_scheduled( 'single_action' )` is once
again `false`, so it queues it up again to run
- Cron pageload runs in the background again
rinse repeat.
In your case, you should -conditionally- call `wp_schedule_single_event()`
when a certain circumstance is true (For example, `if ( isset( $_POST
['schedule-the-cron'] ) ) { wp_schedule_single_event( time(),
'single_action' ); }`).
This is a hastily written, and insecure, but here's an example plugin
which shows an implementation where the cron only fires once, in response
to a user action triggering it to run:
https://gist.github.com/dd32/d1091e4f6316dba126814b5d3ce559bb
I hope this helps set you on the right direction.
I'm marking this ticket as invalid as there's nothing which can be used to
point to a core bug, discussion can still occur while the ticket is
closed, please do not re-open the ticket unless you have information which
can be used to reproduce a bug in core.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/42660#comment:12>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list