• Hello everyone,

    I’m encountering an issue with WP Cron in my WordPress setup (with woocommerce, but it’s not important), where multiple scheduled cron events seem to overwrite each other, causing only the last event to execute. Here’s a detailed description of the problem:

    Whenever I place multiple WooCommerce orders at the same time, the WP Cron events are scheduled for each new order. However, only the last scheduled event runs, and the previous ones are not executed, despite being logged as scheduled. Here are the relevant log entries:

    [27-Jun-2024 09:25:44 UTC] Scheduling WP Cron event for subscription ID: 3490
    [27-Jun-2024 09:25:44 UTC] Scheduling WP Cron event for subscription ID: 3491
    [27-Jun-2024 09:25:45 UTC] Scheduling WP Cron event for subscription ID: 3492

    Here is the code I’m using to schedule the WP Cron events:

    add_action('woocommerce_subscription_payment_complete', 'schedule_wpcron_generate_pdf_and_send_email', 10, 1);

    function schedule_wpcron_generate_pdf_and_send_email($subscription) {
    $subscription_id = $subscription->get_id();
    $last_order = $subscription->get_last_order('all', 'any');
    $order_id = $last_order->get_id();
    error_log('Subscription id: ' . $subscription_id . " order id: " . $order_id);

    $args = array($subscription_id, $order_id);

    // Schedule the event
    wp_schedule_single_event(time() + 60, 'process_wpcron_generate_pdf_and_send_email', $args);
    }

    add_action('process_wpcron_generate_pdf_and_send_email', 'handle_wpcron_generate_pdf_and_send_email', 10, 2);

    function handle_wpcron_generate_pdf_and_send_email($subscription_id, $order_id) {
    error_log("Running WP Cron for Subscription ID: $subscription_id, Order ID: $order_id, Unique Hash: $unique_hash");
    // Custom code to generate PDF and send email
    }

    Observations:

    • Each event is logged as scheduled with unique parameters.
    • Only the last scheduled event executes, and previous events do not run.
    • I have confirmed that the parameters passed are unique.

    Steps Taken:

    • Ensured unique scheduling by using unique parameters.
    • Removed wp_next_scheduled check to prevent missing event scheduling.
    • Verified through logs that each event is indeed being scheduled with unique parameters.

    Request:

    I’m looking for guidance on how to ensure that all scheduled WP Cron events are executed independently without overwriting each other. Any insights or suggestions to resolve this issue would be greatly appreciated. I’m fighting with this for 2 days… :c

    Thank you!

    • This topic was modified 4 months, 3 weeks ago by Dawid.
    • This topic was modified 4 months, 3 weeks ago by Dawid.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    I don’t think it’s a good idea to schedule events so close together. I realize that providing unique $args should allow this, but the unique $args is to help ensure the events are not discarded as duplicates, they don’t do anything to ensure that your event callback (to make a PDF and send email) doesn’t encounter issues from being called multiple times in rapid sequence. I suggest the events be spaced out longer to ensure your callback for each event fully completes before it’s called again. Doesn’t have to be spaced out every 10 minutes, but I think events within seconds of the others could be problematic.

    TBH, the WP Cron scheme isn’t the most reliable way to manage timed events. It is in use because it is in theory possible to use on any server, even those without traditional Cron jobs. If you’re able to, I think scheduling traditional server Cron jobs would be a more reliable solution.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.