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

3 s
3

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

Leave a Reply

Your email address will not be published. Required fields are marked *