Scheduled events disappear from events queue

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');

Topic events plugin-development Wordpress

Category Web


This topic on WP.ORG would help.

Long story short - if there is issue or delay or break in the cron-job, it might get delisted by wp.


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' );
    }
}

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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.