1

I have a plugin that sets a scheduled event and runs fine for myself and all users except one. On his server the scheduled event is mysteriously removed somehow after working fine for hours/days. I need some ideas on how to find out how this is happening.

When activated, the plugin creates the event (and only removes it on plugin deactivation)

wp_schedule_event( time(), 'aps_schedule', 'aps_auto_post_hook' );

and then setting up cron_schedules, I added some debug code:

add_filter('cron_schedules', 'aps_set_next_schedule', 99);
function aps_set_next_schedule($schedules) { 

    // debug section:
    aps_write_log( "hjakhrw3 - setting cron schedule 'aps_schedule'" ); 
    $scheduledtime = wp_next_scheduled('aps_auto_post_hook');
    if ($scheduledtime)
        aps_write_log( "hjakhrw3 - 'aps_auto_post_hook' is set" ); 
    else
        aps_write_log( "hjakhrw3 - 'aps_auto_post_hook' is NOT set." ); 

    $timesecs = aps_time_seconds(get_option('aps_next'),get_option('aps_next_time'));
    $schedules['aps_schedule'] = array(
        'interval' => $timesecs, 'display' => 'APS Scheduler Check'
    );
return $schedules;
}

Now, after many many hours of running successfully and doing it's thing, suddenly:

2014-12-20 04:05:37  - hjakhrw3 - setting cron schedule 'aps_schedule'
2014-12-20 04:05:37  - hjakhrw3 - 'aps_auto_post_hook' is set
2014-12-20 04:05:56  - hjakhrw3 - setting cron schedule 'aps_schedule'
2014-12-20 04:05:56  - hjakhrw3 - 'aps_auto_post_hook' is set
2014-12-20 04:07:03  - hjakhrw3 - setting cron schedule 'aps_schedule'
2014-12-20 04:07:03  - hjakhrw3 - 'aps_auto_post_hook' is NOT set. 
2014-12-20 04:08:31  - hjakhrw3 - setting cron schedule 'aps_schedule'
2014-12-20 04:08:31  - hjakhrw3 - 'aps_auto_post_hook' is NOT set. 

So between 04:05:56 and 04:07:03, something has removed the scheduled event 'aps_auto_post_hook' and it wasn't the plugin, since it only removes it upon deactivation. I suspect other code/plugin - but how can I find out?? I'm at a loss how the event is no longer scheduled without the plugin being deactivated...

P.S. in case someone asks, my deactivation code, though it isn't called in this case:

register_deactivation_hook( __FILE__, 'aps_deactivation' );
function aps_deactivation() {
    wp_clear_scheduled_hook('aps_auto_post_hook');
}
2
  • Doesn't really answer why, but I had a similar problem happen to me and I never found the cause of what was clearing my scheduled tasks. In the end I ended up hooking into the wp action and re-scheduled the task if wp_next_scheduled() returned false. Dec 20 '14 at 15:52
  • Thanks I also added a hook to check for the event and re-schedule if not found. Still waiting on feedback from user to see if this triggers and solves the issue.
    – johnh10
    Dec 23 '14 at 23:22
1

Couple things I'd try:

  • I've seen weird behavior with plugins in general when WP isn't given enough memory. You might want to up PHP and WordPress' memory limit and see if you still get that weird behavior.

  • WP Cron only runs on a page load so between 4:05 and 4:07 (or whenever you're getting the weird behavior), check your server's access and error logs and see what else might be hitting it in the mean time or if WP is failing somehow.

  • You might alo want to try a plugin like WP Crontrol and see what else might be firing during that time.

1
  • Thanks for the ideas. Have crawled through the logs, combed through other plugins looking for anything that might clear the scheduled event but have yet to find anything. Also using 'FFF Cron Manager' but other events 'seem' fine. Still looking...
    – johnh10
    Dec 23 '14 at 23:23

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.