skip to Main Content

I’m registering an event like:

wp_schedule_single_event( time(), 'do_custom_hook', array( $body ) );

Above that, I have added the following action:

add_action('do_custom_hook', 'process_custom_hook', 10);

function process_custom_hook($body){
    // custom code here
}

However, sometimes the process_custom_hook function fires, while other times, it simply doesn’t (it fires most times, though. It’s missed about 10%-20% of the time)

The event is always returning true, meaning that it must have registered.

Also, while testing, I made sure that the arguments (body) is always different.

Any reason why this may occur?

5

Answers


  1. You are using cron from WordPress but some plugins disable or prevent CRON from working. My recommendation in this case is either you create this schedule through the Server Cron or you install a plugin to reaffirm your schedule.

    I had the same problem it was even a little difficult to find … And as you will see it doesn’t have a current update, but for me it works. https://wordpress.org/plugins/wp-crontrol
    &
    https://br.wordpress.org/plugins/advanced-cron-manager/

    you can identify which Cron you want to edit and thus have more precision in your edition. There are other plugins that can and should be related to this one. you mentioned about cron’s schedule. That’s why I indicated this one. So, you can know the Chrome configuration on the calendar. WP Cron you can edit your Cron schedule

    Login or Signup to reply.
  2. According to the official documentation on this instance,

    Scheduling an event to occur within 10 minutes of an existing event with the same action hook will be ignored unless you pass unique $args values for each scheduled event. which you have stated tht you did but maybe a double check will help.

    It is dependent on when a user visits the site so the action will trigger when someone visits your WordPress site if the scheduled time has passed.

    Documentation also says you could use wp_next_scheduled() to prevent duplicate events and use wp_schedule_event() to schedule a recurring event.

    The schedule might return true in certain instances where it run but was ignored. so it did run but it was ignored.

    I would suggest a detailed log of everything that is sent and received so you can see for yourself if what is occuring is same as what you are confident on.

    here are a few links with similar issues and documentation you could look at.

    I hope this helps. if not, lets figure it out together.

    Login or Signup to reply.
  3. From the codex:

    Note that scheduling an event to occur before 10 minutes after an existing event of the same name will be ignored, unless you pass unique values for $args to each scheduled event.

    If your custom hook is only working some of the time, then this might be an avenue to look at. If you require the hook to be handled immediately, then it might be prudent to look at giving a hook a unique name, or passing unique values to the hook.

    If you do not need your job to execute immediately, then you could look at utilising wp_next_scheduled() to determine when the job will next run, and set a job to run after the next scheduled job.

    It’s also worth noting that if this task is something which seems to have consistent logic behind it (as seems to be the case) – why not store the job information in to the database and run a cron job every 5-10 minutes to pick up any new jobs from the database and handle them as such? This would avoid needing to deal with the behaviour of wp_schedule_single_event().

    Login or Signup to reply.
  4. From WordPress Document:

    WP-Cron works by checking, on every page load, a list of scheduled
    tasks to see what needs to be run. Any tasks due to run will be called
    during that page load.

    WP-Cron does not run constantly as the system cron does; it is only
    triggered on page load.

    Scheduling errors could occur if you schedule
    a task for 2:00PM and no page loads occur until 5:00PM.

    I think your cron event may be missed because there is no page loads occur the scheduled time.

    Here is a solution for your problem:
    Hooking WP-Cron Into the System Task Scheduler

    As previously mentioned, WP-Cron does not run continuously, which can
    be an issue if there are critical tasks that must run on time. There
    is an easy solution for this. Simply set up your system’s task
    scheduler to run on the intervals you desire (or at the specific time
    needed). The easiest solution is to use a tool to make a web request
    to the wp-cron.php file…

    Login or Signup to reply.
  5. In my case this exact issue occurred when also Woocommerce’s action scheduler was running. Action Scheduler is a cron task manager that ships with Woocommerce, but also other plugins like for instance wp-mail-smtp.

    I had exactly the same issue and couldn’t figure out what was wrong. I’ve tried to debug the WordPress code, and came to the conclusion that when a task was scheduled (meaning, the moment it was added to the scheduled tasks) within 10 second of each whole minute, it just got removed straight away. It seemed some sort of racing condition where the action scheduler just popped it of the stack without the normal wp cron being able to execute it, because the task was already gone.

    I need to also say, that I’ve setup crontab calling wp-cron.php every minute on the minute (instead of the ‘fake cron’ of WordPress).

    When I replaced wp_schedule_single_event with the as_enqueue_async_action function of the Action Scheduler, no tasks were dropped anymore.

    I think an alternative is deinstalling anything that uses Action Scheduler, but I haven’t tried that.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search