• I’m creating my own plugin to schedule a cron job, and it’s working TOO well. It fires every time my site is hit, not when it’s scheduled. I’ve used the Debug Bar plugin with the Cron plugin, and it shows:

    NEXT EVENT:
    2013-11-04 15:35:37
    1383579337
    23 hours ago

    It consistently says that the next event is in the past. Any ideas?

    add_action('admin_menu', 'dhg_cron_menu');
    function dhg_cron_menu()
    {
        add_options_page('NEW WPMS Stats', 'NEW WPMS Stats', 'manage-options', 'dhg-cron', 'dhg_cron_settings');
    }
    
    function dhg_cron_settings()
    {
        if(!wp_next_scheduled('dhg_cron_hook'))
        {
            wp_schedule_event(time(), 'hourly', 'dhg_cron_hook');
        }
    }
    
    add_action('dhg_cron_hook', 'dhg_cron_get_stats');
    function dhg_cron_get_stats()
    {
        log_wpms_stats("testing!");
    }
Viewing 14 replies - 1 through 14 (of 14 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    What does time() give you on your server?

    Thread Starter danhgilmore

    (@danhgilmore)

    Otto – I just had it echo time() to the log file….

    “1383666668”

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Okay. So, the call to wp_next_scheduled('dhg_cron_hook') should return a timestamp. In your case, presumably that timestamp is 1383579337. (Check this…)

    This suggests that your cron isn’t working properly… somehow.

    If the cron process is getting executed, then it’s going to do the following:

    – Call wp_reschedule_event to schedule it for the next execution time. This should add in a new event with a new timestamp.

    – Call wp_unschedule_event to remove the old event. This should get rid of the 1383579337 event and let the new one take over.

    – Call the dhg_cron_hook action (and thus run your function). Check this to make sure your function is indeed running, which presumably it is.

    So what you need to do is to dump whatever is returned by _get_cron_array() and see what happens before and after the cron event is run. This should tell you at what point the process is breaking down.

    Edit: I checked the order of execution, the rescheduling stuff happens before your hooked function is called, so a fatal/die happening there won’t break the rescheduling.

    Thread Starter danhgilmore

    (@danhgilmore)

    Looks like dhg_cron_settings() is not being run when the site is hit, or when I go to the menu item….

    EDIT: Never mind, it is being called when I click on the menu item.

    Thread Starter danhgilmore

    (@danhgilmore)

    – The call to wp_next_scheduled(‘dhg_cron_hook’) does in fact return a timestamp: 1383744454 = Wed, 06 Nov 2013 13:27:34 GMT. My local Win7 time is 08:29.

    I guess my confusion is why does the function dhg_cron_get_stats() run every time I hit any page on my site?

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Stop thinking about your local time. That will only confuse you. WordPress operates in UTC, all the time. The only meaningful comparison for timestamps is to other timestamps.

    1383744454 is a realistic result to have, because it was 2 minutes earlier.

    The dhg_cron_get_stats() function should only run once an hour, but it can run at at time you visit any page on your site. If it is running more often than that, then what you need to do is to examine the dump of a call to _get_cron_array().

    _get_cron_array() returns the information in the current cron array in the options table. If you examine it before and after a run, then it should change. Without examining what is in there before and after the cron run, there’s no realistic way to debug your problem.

    Thread Starter danhgilmore

    (@danhgilmore)

    sounds good. Adding code to dump the array to the log file before and after…back in a bit.

    Thread Starter danhgilmore

    (@danhgilmore)

    Ok, did some more testing. I dumped _get_cron_array() both inside dhg_cron_menu() and in dhg_cron_get_stats(). The latter function fires before the former.

    – in dhg_cron_get_stats(), next sked: Nov 6, 2013 @ 17:27
    – in dhg_cron_menu() (called by the admin_menu hook), next sked: Nov 6, 2013 @ 14:27

    Would it be helpful to post the data from the cron option_value in wp_1_options?

    EDIT: the times listed were converted from the timestamp, and are in UTC

    EDIT2: This is a MU-Plugin

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    So, that seems like it’s working, basically. The schedule changed properly.

    What’s the question again? Is the dhg_cron_get_stats function firing more than once per hour?

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Also, note that the wp-cron mechanism is a best-effort mechanism, not an actual scheduler. It won’t fire when nobody is visiting the site. The first time somebody visits the site after the time for the cron job is scheduled, it will fire it off then. Meaning that if nobody visits your site for a day, then it will only run that job once per day.

    Thread Starter danhgilmore

    (@danhgilmore)

    ” Is the dhg_cron_get_stats function firing more than once per hour? ” – Yep, it’s running every single time I hit the site, be it the main page or any admin page.

    Yeah, I’m cool with it NOT running when it’s scheduled to. It’s just a plugin to count the number of sites, users, and ALL the posts, and it’ll be run once a day.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    If it’s firing every time, and that’s more often than once an hour, then you need to examine the results from that _get_cron_array when it fired *and was not supposed to do so*. The results you posted above basically showed a 3 hour difference, so it naturally should have fired at that point. You need to see what it’s doing when it fires and it’s less than an hour difference. You also need to compare that timestamp with the results from time().

    Also, stop converting things to normal times. Look at the raw numbers. It’s obvious when a number is greater or less than another number. ??

    Thread Starter danhgilmore

    (@danhgilmore)

    Roger all…

    When I click the link to the main dashboard (or any other page), dhg_cron_menu() runs, and it shouldn’t. I can replicate this every time I click a link or load a page. I’ve grabbed the data from _get_cron_array() and dumped the data to a log:

    20131106 – 06:55:35 – time() = 1383764135
    20131106 – 06:55:35 – wp_next_scheduled() = 1383766054

    and I blame Ryan Duff for the normal times…was using a snippet of his code ??

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Try using the WP Crontrol plugin to examine the current cron jobs. All I can think is that your cron entry is wrong somehow.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘WP_Cron troubles’ is closed to new replies.