1

Every time I refresh the page, my cron job update_backup_now_feed seems to have its timer reset. I'm using a plugin called "crontrol" that displays the time until next call, and every time I refresh it resets to 1 hour. The only time the schedule seems to fire off is when I avoid the site for over an hour and then refresh.

Am I doing something wrong with my code below or is this a bug? I'm working on Multisite. I found this question which is similar - and in the comments section of the answer, the user mentioned having the same problems only on multisite.

Why does refreshing the page reset the timer for wp cron events?

Here is all of my cron related code.

// add custom cron interval
add_filter('cron_schedules', 'add_custom_cron_intervals', 10, 1);
function add_custom_cron_intervals($schedules) {
    $schedules['every_two_minutes'] = array(
    'interval'  => 120,
    'display' => 'Every Two Minutes'
    );

    return (array)$schedules; 
}

add_action('init', 'register_update_feed_event');
function register_update_feed_event() {
  if(!wp_next_scheduled('update_now_feed')) {
        wp_schedule_event(time(), 'every_two_minutes', 'update_now_feed');
    }

    if(!wp_next_scheduled('update_backup_now_feed')) {
        wp_schedule_event(time(), 'hourly', 'update_backup_now_feed');
    }
}   

add_action('update_now_feed', 'update_now_feed_function');
function update_now_feed_function() {
    NowFeed::set_instagram_cache();
    NowFeed::set_twitter_cache();
}


add_action('update_backup_now_feed', 'update_backup_now_feed_function');
function update_backup_now_feed_function() {
    NowFeed::set_instagram_backup_cache();
    NowFeed::set_twitter_backup_cache();
}

UDPATE I thought I fixed it, but instead it raises more questions.

For some portion of last night, my code looked like this below. I had removed the wp_next_scheduled check... and it literally created 50 different scheduled events.

//if(!wp_next_scheduled('update_backup_now_feed')) {
    wp_schedule_event(time(), 'hourly', 'update_backup_now_feed');
//}

The crontrol only showed one of them, since they all had the same name. I realized I could keep deleting them, and they didn't go away. I kept clicking and finally, after ~50 clicks they were gone. This definitely helped some of the issues I was having. Don't pull a me and remove that conditional (although I'm not sure how you update cron jobs otherwise.. )

I have now put this code in my WP_Config. define('DISABLE_WP_CRON', true); to see if I could troubleshoot more.

At this point, refreshing the page would NOT call any of my events. However, I went to the custom URL to test cron - /wp-cron.php?doing_wp_cron and when I refresh it, the response from my action sometimes shows. This is the part I don't understand.

Does disabled wp cron and enabling it via real cron cause the 'timers' to stop functioning as expected? If I have an hourly job and I call the cron every 5 minutes, will it also be called every 5 minutes? Why does calling it manually sometimes call the event and sometimes not?

0

Well....I've figured it out.

Manually deleting everything in Crontrol didn't help as much as I thought it had. I went to my wp_options table and deleted the entire cron entry. On refresh now, everything looked more or less how I expected, and all of my questions are answered now.

When disabling WP Cron to enable your servers cron, the timing on wp_scheduled_events is not forgotten. If you call the file and the job still has 30 minutes left before it should be run, it will not be run. Therefore, there is no benefit to calling the cron job more often than your minimum timer. If your shortest timer is hourly, and you've disabled WP_CRON, don't set your cron to less than an hour!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.