• Resolved Kermit524

    (@kermit524)


    Hi, I have set Cron on my server and set a daily event scheduler that is supposed to send me an email every day. Just to see that everything works before I go further. The cron on the server is set to check with the wp-cron.php file every half hour. However the scheduled event on my plugin file is set to perform every day (‘daily’). Still, for some reason I get an email from the server every half hour. Any idea where did I go wrong? Which is the best way to debug this? Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    You should not be using your server’s CronTab to trigger WP scheduled events. I’m unsure how such triggers seem to bypass the event schedule, but they apparently are.

    WP scheduled events are triggered by normal site visitation, there’s generally no need for outside triggers. Normal visitor and bot traffic is usually adequate. If you have an unusual site with little traffic where an outside trigger is needed, make a simple general request, do not target wp-cron.php specifically.

    Thread Starter Kermit524

    (@kermit524)

    Ok, having disabled the server cron and relying only on WP-cron, now I don’t get any mail messages at all… :-\
    Is there any way to tell if a schedule task didn’t go through?
    Thanks again!

    Moderator bcworkz

    (@bcworkz)

    If your task didn’t happen then it didn’t go through? ??

    Sorry, couldn’t resist. There’s not any way to tell what didn’t happen. You can look at access logs to see if something fired by a POST to wp-cron.php, but that will not tell you what it was for.

    You can see what actions are scheduled to fire in the future by temporarily placing code on a template somewhere that gets the “cron” option and var_dumps it for examination. You’ll see an array keyed by timestamps of when the action is to fire next, followed by the action tag, a hash, and other information. For recurring events, you may be able to work backwards to determine what POST in the logs corresponds to what event stored here. Subtracting intervals from timestamps will not yield precise results because of the variance in events triggered by visitation.

    When any particular action fires, if it is for a recurring event, WP will then schedule the next event in the same process. It sounds like your server Cron managed to bypass the WP process because the WP one was never set up properly. The setup isn’t all that intuitive. Compare what you have to the second example in the schedule event doc’s notes. Notice how a plugin needs to be activated in order for the example event to be scheduled. Events need to be scheduled by code that only executes once. Most normal WP code runs on every request.

    If you’re willing to share your setup code here, we can see if any issues are apparent. We don’t really need to see the actual code that is supposed to execute in order to evaluate the setup, but if the problem is in the execution and not the setup, we would not be able to evaluate that unless it’s included.

    Thread Starter Kermit524

    (@kermit524)

    Thanks bcworkz! In fact what I meant to ask is if there is a way for me to know that a scheduled task did not work due to an error – same way I as when I run the script manually and get an error message that prevents it from executing.
    Anyway – apparently – the task did perform – the problem lied with the actual sending the email message – which is fixed now.
    Thing is – now I have another issue: Having set the cron to fire “daily” – I keep getting “hourly” email messages about the task.
    Here’s my code:

    
    add_action( 'post_published_notification_action', 'new_content_notify' );
    
    function register_daily_post_published_notification() { 
    	if( !wp_next_scheduled( 'post_published_notification_action' ) ) {
    		wp_schedule_event( time(), 'daily', 'post_published_notification_action' );
    	}
    }
    
    register_activation_hook( __FILE__, 'register_daily_post_published_notification' );
    
    register_deactivation_hook( __FILE__, 'unregister_daily_post_published_notification' );
    function unregister_daily_post_published_notification() {
    	wp_clear_scheduled_hook( 'post_published_notification_action' );
    }

    Any idea where do I go wrong?

    Many thanks again and in advance

    Thread Starter Kermit524

    (@kermit524)

    Hey, so apparently the problem is resolved: in order for the scheduled task to reset and change from hourly to daily – I had to deactivate and then activate the plugin.

    Moderator bcworkz

    (@bcworkz)

    Oh yeah, that would do it! ??

    I’m sorry I didn’t respond earlier to your previous post. I’m not sure what happened, I didn’t get or missed the notification. I wouldn’t have had much to say anyway, as we now know, the code looks good. I don’t think it would have occurred to me to question deactivation/activation. I glad you worked that out!

    Thread Starter Kermit524

    (@kermit524)

    Thanks! Yes, no doubt that sleeping over things is always a good idea. ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Scheduling a task using wp-cron’ is closed to new replies.