WP Cron Events Overwriting Issue
-
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: 3492Here 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!
- You must be logged in to reply to this topic.