The MU forums have moved to WordPress.org

wp_schedule_event happens multiple times (wp cron) (4 posts)

  1. mdgross
    Member
    Posted 15 years ago #

    wp_schedule_event is occurring frequently between scheduled intervals. I have it set to record its execution time every ten minutes.

    In mu-plugins/zzz_cron.php:

    add_filter('cron_schedules', 'more_reccurences');
    add_action( 'happens_every_ten_minutes', 'do_this_every_ten_minutes' );
    
    function more_reccurences() {
    	return array('tenmin' => array('interval' => 600, 'display' => 'Every Ten Minutes'));
    }
    
    if (!wp_next_scheduled('happens_every_ten_minutes')) {
    	wp_schedule_event( time(), 'tenmin', 'happens_every_ten_minutes' );
    }
    
    function do_this_every_ten_minutes() {
    	$times = get_site_option('zzz-every_ten_minutes', array());
    	$times[] = date('g:i:s A');
    	update_site_option('zzz-every_ten_minutes', $times);
    }

    On a template page:

    foreach(get_site_option('zzz-every_ten_minutes') as $execution_time) {
    	echo $execution_time . '<br />';
    }

    The result of recording execution time at ten minute intervals is:

    8:34:56 PM
    8:34:59 PM
    8:35:04 PM
    8:35:12 PM
    8:35:17 PM
    8:35:28 PM
    8:35:33 PM
    8:35:39 PM
    8:35:47 PM
    8:36:06 PM
    8:36:07 PM
    8:36:27 PM
    8:36:48 PM

    As you can see the event is happening much more frequently than every ten minutes... what is going on here?

  2. SteveAtty
    Member
    Posted 15 years ago #

    I suspect its down to the way you're checking to see if its already got a scheduled event and then adding another if you haven't.

    I've only played with Cron as part of a plugin where I set it on initial activation and put a clear in the de-activate.

  3. mdgross
    Member
    Posted 15 years ago #

    @SteveAtty: It checks if the event is already scheduled or not before running wp_schedule_event. I have a plug-in that shows all wp-cron scheduled events and I can assure you that it's only being added once.

  4. SteveAtty
    Member
    Posted 15 years ago #

    Hmm, then I have no idea.

About this Topic

  • Started 15 years ago by mdgross
  • Latest reply from SteveAtty