Support » Developing with WordPress » Event scheduled with wp_schedule_single_event is executing more than once

  • I am scheduling a single event using wp_schedule_single_event and it is occasionally executing more than once. I have seen it execute up to 8 times.

    This is how I am scheduling the event:
    wp_schedule_single_event(time(), $hook, $args);

    This event can be scheduled very often, but the $args will be different every time because I am including a unique ID.

    I am using WordPress version 5.3.2 in Azure.

    Does anyone know why this is happening and how I can ensure the event is only executed once?

Viewing 11 replies - 1 through 11 (of 11 total)
  • Note that scheduling an event to occur within 10 minutes of an existing event with the same action hook will be ignored unless you pass unique $args values for each scheduled event.

    Use wp_next_scheduled() to prevent duplicate events.

    Use wp_schedule_event() to schedule a recurring event.
    https://developer.wordpress.org/reference/functions/wp_schedule_single_event/

    Hi,

    Check for the next execution before scheduling an event, if exits clear the current schedule and add new,

    if (wp_next_scheduled ( $hook, $args )) {
       wp_clear_scheduled_hook( $hook );
     }
    wp_schedule_single_event(time(), $hook, $args);
    
    Thread Starter nkearns

    (@nkearns)

    I have tried using wp_next_scheduled, but it did not solve my issue. I think this is because the $args will differ each time, so it isn’t actually already scheduled.

    Moderator bcworkz

    (@bcworkz)

    Why schedule an event for time() (now)? Why not just do the function directly? I’d advise scheduling events at least 10 min. into the future.

    Thread Starter nkearns

    (@nkearns)

    The function that’s called in the event calls an API that can take several seconds to respond, so I am scheduling the event so that user does not have to wait for that response time.

    I am calling wp_schedule_single_event whenever a particular form is submitted on the site, so it is possible for 2 (or more) forms to be submitted at the same time. I include the unique id of the entry in $args, so it should be unique and allow it to be scheduled.

    Would scheduling these events at least 10 minutes solve this duplicate issue? Or is that just a general best practice?

    $args = array( '123' );
    //value timestamp or false
    if ( ! ($var = wp_next_scheduled( 'myevent', $args ) ) ) {
    //value true or false
        unset( $var );
        $var = wp_schedule_event( time(), 'daily', 'myevent', $args );
    }
    if( is_int( $var ) ) {
    $test = new DateTime( '@'. $var );
    $output = $test->format( "Y-m-d H:i:s" );
    }

    Valid values for the recurrence are ‘hourly’, ‘daily’, and ‘twicedaily’. These can be extended using the ‘cron_schedules’ filter in wp_get_schedules(). https://developer.wordpress.org/reference/functions/wp_schedule_event/#comment-933
    if a scheduled event exists, you don’t create an event even using a unique id in $ args ( good php code ).
    , unique id you have to create it only when it is necessary

    • This reply was modified 2 years, 5 months ago by autotutorial.
    • This reply was modified 2 years, 5 months ago by autotutorial.
    • This reply was modified 2 years, 5 months ago by James Huff.
    Moderator bcworkz

    (@bcworkz)

    Without knowing the real reason for duplicate calls, it’s hard to say if scheduling in 10 minutes would help. I would say that scheduling more than 10 minutes in advance would more likely allow a wp_next_scheduled() check to work as expected. By scheduling immediately, I think you are encountering a race condition which prevents wp_next_scheduled() from working as expected.

    In any case, I think it’s worth a try. It’s not like doing so is a huge effort 🙂

    Thread Starter nkearns

    (@nkearns)

    @bcworkz I do not want duplicate calls. What I am seeing is that I schedule using wp_schedule_single_event and the event is being called multiple times. It should only be called once for the hook and args passed.

    I do call wp_schedule_single_event multiple times within 10 minutes with the same hook, but always different args, so they are not duplicate.

    @nkearns if $ args is different it is called several times, do you want the timestamp code $ hook and maybe update the timestamp or $ args for the 10 minute period?

    Thread Starter nkearns

    (@nkearns)

    @autotutorial I am going to try to explain my issue again..

    A form is submitted on the site, which has a unique $entryId.
    I schedule the event like so:
    $args = array($formData, $formId, $entryId);
    wp_schedule_single_event(time(), $hook, array($formData, $formId, $args));

    The issue I have is that $hook is occasionally being executed more than once for this same set of $args.

    It is only scheduled once, so why is it being executed more than once?

    add_filter( 'cron_schedules', 'cron_add_tenminutes' );
     
     function cron_add_tenminutes( $schedules ) {
     	// Adds once 10 minutes to the existing schedules.
     	$schedules['10min'] = array(
     		'interval' => 10 * MINUTE_IN_SECONDS,
     		'display' => __( 'Every 10 minutes' )
     	);
     	return $schedules;
     }
    
    function test() {
    $args_1 = array( '123' );
    //$var return timestamp , timestamp update or false
      if ( ( ! ( $var = wp_next_scheduled( 'myevent', $args_1 ) ) ) || ( ( ( $var + ( 10 * MINUTE_IN_SECONDS ) ) < time() ) && wp_clear_scheduled_hook( 'myevent', $args_1 ) >= 0 ) ) {
        wp_schedule_event( time(), '10min', 'myevent', $args_1 );
      }
    return $var;
    }
    
    $args_1 = array( '123' );
    function do_this_tenminutes( $args_1 ) {
        // do something every hour
    }
    add_action( 'myevent', 'do_this_tenminutes', 10, 1 );
    test();

    if $args is different WordPress creates a scheduled event for each iteration my code shows a single manual event with $args (‘123’) for 10 minutes.
    This code delete all hook myevent for this $args.
    wp_clear_scheduled_hook( 'myevent', array ( '123' ) );

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Event scheduled with wp_schedule_single_event is executing more than once’ is closed to new replies.