The Symptoms
I have created a WordPress plugin for a client that sends a daily summary email about activity within their system. The plugin in running on four sites. The host and server config are identical for all four sites. The PHP version is currently 7.4 on all sites.
The function rekreate_raisedonors_orders_to_shipbob sends the summary email. On one, and only one, site we are receiving multiple summary emails most days. The emails are all the same, they just come randomly throughout the day. One day it might fire one time as expected. Another day it might fire 20 times.
The scheduled action is set for 01:00 CST. It always fires within a minute of that time so there is enough site traffic to cause it to fire. Most days it also randomly fires around 08:30ish CST. This morning it fired 18 times between 01:00 and 01:04 CST, while the last 4 days it fired only once at the desired time. It has also sent emails in the afternoon or other random times.
I have checked the cron array on the site in question many times and it's always had one scheduled task for my action, and always at the correct time.
The Code
The only place that the function rekreate_raisedonors_orders_to_shipbob is called is in the code snippet below. The is all of the code relating to the cron job. rekreate_raisedonors_orders_to_shipbob is the only function that sends the email in question.
/* Run rekreate_raisedonors_orders_to_shipbob daily, while making sure it's only called once */
if( did_action( 'rekreate_cron_raisedonors_to_shipbob_api_call' ) < 1 ) {
add_action( 'rekreate_cron_raisedonors_to_shipbob_api_call', 'rekreate_raisedonors_orders_to_shipbob' );
}
/* If the summary email is turned on in settings, then schedule the next cron job. */
if ( !wp_next_scheduled( 'rekreate_cron_raisedonors_to_shipbob_api_call' ) && get_option( 'rd_to_sb_active' ) ) {
wp_schedule_event( 1627455600, 'daily', 'rekreate_cron_raisedonors_to_shipbob_api_call' );
}
/* If the summary email is turned off in settings, then unschedule any events that were already scheduled */
if ( wp_next_scheduled( 'rekreate_cron_raisedonors_to_shipbob_api_call' ) && !get_option( 'rd_to_sb_active' ) ) {
$timestamp = wp_next_scheduled( 'rekreate_cron_raisedonors_to_shipbob_api_call' );
wp_unschedule_event( $timestamp, 'rekreate_cron_raisedonors_to_shipbob_api_call' );
}