0

I am trying to debug a cron job in wordpress. It's supposed to update user hours but doesn't, and after putting some test statements in the code, I'm not too sure it's even running.

I have debug mode on, as in a put this in wp-config.php:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', true);

And I call my cron event in functions.php like so:

add_action('wp', 'my_activation');
add_action('update_user_hours', 'do_this_hourly');

function my_activation() {
    if ( !wp_next_scheduled( 'update_user_hours' ) ) {
        wp_schedule_event(time(), 'hourly', 'update_user_hours');
    }
}

function update_user_hours(){
     error_log('the function actually gets called')
} 

Basically I cannot get any error_log statements to print in my log file, and I'm not totally sure how to debug otherwise. There are no other errors showing up in the log file either.

For testing, I'm running the event with WP-Crontrol.

Any ideas on how to debug this?

Thanks!

UPDATE: Someone pointed out a mistake with adding the cron event and I have updated my code like so:

add_action('wp', 'my_activation');
add_action('update_user_hours', 'do_this_hourly');

function do_this_hourly(){
    error_log('foo');
    //update the user hours
}

function my_activation() {
    if ( !wp_next_scheduled( 'update_user_hours' ) ) {
        wp_schedule_event(time(), 'hourly', 'update_user_hours');
    }
}

I also began to use wp-cli and was able to run the event successfully from there. I checked the php error log and there was nothing about my cron job in there.

The user hours are still not being updated. At this point it's possible it's an error with the script itself, but if it's crashing it isn't going to the php error log somehow. The error log I'm viewing is in /var/log/apache2, I'm using a LAMP stack on Ubuntu 16. Any input on how to debug this?

Thanks again!

UPDATE (AGAIN): I found a syntax error and I'm now able to run the script with the wp-cli. It does exactly what I want. However, it doesn't seem to be running on its own every hour when I put it on the server. How can I make sure the event actually gets scheduled now that the script is working?

  • Note that there is a missing semicolon in your update_user_hours function, it's likely it doesn't run because it shows a syntax error in the PHP error log. Have you contacted the WP Crontrol support? Have you tried running it via normal cron by using WP CLI? How are you getting the 302 response? – Tom J Nowell Nov 7 '18 at 20:39
  • Thanks for your reply! I added the error_log statement when writing my question after deleting the original contents of the function so the missing semicolon wasn't an issue turns out – ellen Nov 8 '18 at 6:56
  • /var/log/apache2 is very likely an Apache log, not a PHP log, are you sure that error_log actually works and that it's not being silenced? Have you tested a known good cron example? If you can run the code explicitly via WP CLI and still not get a message, then something else is wrong, and a major assumption somewhere is broken ( most probably the error_log assumption ) – Tom J Nowell Nov 8 '18 at 16:23
  • Additionally, why is this cron job being managed on the wp hook rather than the init hook? – Tom J Nowell Nov 8 '18 at 16:25
  • I have never done this before and the example I saw used the wp hook. I just tried changing it to init but got the same result – ellen Nov 8 '18 at 18:01
1

The problem here is a mistake regarding how to schedule a cron event, lets begin with:

wp_schedule_event(time(), 'hourly', 'my_schedule_hook', $args);

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

Here you are telling WordPress to fire the update_user_hours action/event on an hourly basis.

You then hook into this to fire a callback:

add_action('update_user_hours', 'do_this_hourly');

But then, instead of declaring do_this_hourly, for some reason the code declares a mystery function named update_user_hours:

function update_user_hours(){
     error_log('the function actually gets called')
}

So the do_this_hourly function is undefined, a completely random new function is added that's totally disconnected, and to top it off, there's a missing semi colon. I would expect both syntax errors and missing callback warnings in your PHP error log

Extra Notes

  • WP_DEBUG_LOG doesn't enable the debug log, it just moves the PHP error log, you'd be better off using the real error log instead
  • Use WP CLI instead to trigger list and debug cron events, rather than a 3rd party plugin. Who knows what that plugin is doing or how it's meddling
  • Don't hide warnings, fix them. If you'd checked it would have lead you straight to the cause and the fix without having to come here
  • Try the Query Monitor plugin out, it'll flag warnings, errors and other things
  • Thank you! I have updated the hooks for now and will update my question – ellen Nov 8 '18 at 6:56
  • And the issue still persists even with the hook change? – Tom J Nowell Nov 8 '18 at 16:21
  • The do_this_hourly function makes an api call that is not being hit. I don't know for sure whether it's the exact same issue, but the hours are still not being updated – ellen Nov 8 '18 at 17:55
  • OK, the script seems to be working now but somehow the event doesn't run on its own every hour. How can I make sure it is scheduled? – ellen Nov 8 '18 at 22:47
  • WP Cron has no way of self-running, it's a non-blocking self-http request that happens at the end of a normal page request. If nobody visits your site it has no way to run. You'll have to set up a real server cron job to run it via WP CLI – Tom J Nowell Nov 9 '18 at 0:30

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.