4

I'm having an issue where events that I have scheduled work for a while then just stop. After looking at the event queue I see that the scheduled events are no longer in the queue.

If I deactivate then reactivate the plugin the schedule is back and works for a period of time.

I don't know if WordPress has something that removes events if they timeout or anything like that.

Here is my code:

register_activation_hook(__FILE__, 'k2_section_featured_ads_setup');
function k2_section_featured_ads_setup() {
    wp_schedule_event('1395871200', 'minutely', 'k2_section_featured_ads_hook');
}

register_deactivation_hook(__FILE__, 'k2_section_featured_ads_deactivation');
function k2_section_featured_ads_deactivation() {

    $featured_next = wp_next_scheduled('k2_section_featured_ads_hook');
    wp_unschedule_event($featured_next, 'k2_section_featured_ads_hook');
}

add_action('k2_section_featured_ads_hook', 'k2_section_featured_ads_check_time');

function k2_section_featured_ads_check_time() {
    // function here
}

Here is the filter for my extra cron schedules

function k2_cron_add_schedules( $schedules ) {
    $schedules['quarterday'] = array(
        'interval' => 14400,
        'display' => __('Once every 4 hours')
    );
    $schedules['minutely'] = array(
        'interval' => 60,
        'display' => __('Once every 1 minute')
    );
    return $schedules;
}
add_filter('cron_schedules', 'k2_cron_add_schedules');
  • From the Wordpress docs: For some reason there seems to be a problem on some systems where the hook must not contain underscores or uppercase characters. – Tomás Cot Aug 28 '14 at 11:23
  • 'minutely' is not a core-supplied interval. Though unlikely that this is the root of the problem, could you please supply the filter responsible for adding the interval and let us know where it resides? – bosco Aug 30 '14 at 18:46
  • @TomásCot: I cannot seem to find the passage that you are referencing - mind throwing us a link? – bosco Aug 30 '14 at 18:52
  • @boscho, codex.wordpress.org/Function_Reference/… Read the $hook parameter. – Tomás Cot Aug 30 '14 at 20:30
  • 1
    @kel, I think it could be related with the uksort function using the strnatcasecmp comparison function, which can generate problems with strings with underscores – Tomás Cot Sep 5 '14 at 16:08
1

My solution for now is check every hour if it is running and if not reschedule it. I also remove this in deactivate etc.

wp_schedule_event(time(), 'hourly', 'my_restart_schedule_if_failed');

function my_restart_schedule_if_failed() {
    if( !wp_next_scheduled( 'my_scheduled_minute_job' ) ) {
        wp_schedule_event( time(), 'one_minute', 'my_scheduled_minute_job' );
    }
}
  • +1 for the workaround, but still most likely something is broken with some code if this happens – Mark Kaplun Feb 26 '18 at 8:35
0

After pouring all of my points into this question and trying a few suggested options it seems that WordPress can't handle one minute crons. I have changed it to 5 minutes and haven't had any issues since.

A few things that I tried over the course of weeks for testing purposes:

  • Change the hook names to not include underscores [BUSTED]
  • Rewrite the plugin. Was written procedurally originally and now its all OOP [BUSTED]
  • Changed to 5 minute schedule [BUSTED]

EDIT: I originally marked the last as working but after all these months it was unscheduled again. I'm still not sure what's going on with all of this.

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.