Cron event running more than once if triggered from WP Crontrol

Question

This is just a sample plugin I created in order to track this issue down in a real plugin, but I am experience the same behavior:

<?php // cron-test.php
/**
 * Author: me
 * Description: CRON Test plugin
 * Plugin Name: CRON Test
 * Text Domain: cron-test
 * Version: 1.0.0
*/

add_action( 'plugins_loaded', function() {
  add_action( 'cron-test', function () {
    // Cron is running
    error_log( 'cron-test callback running: ' . time() );
  } );
  if ( ! wp_next_scheduled( 'cron-test' ) ) {
    wp_schedule_event( time(), 'hourly', 'cron-test' );
  }
} );

If I wait until the cron job is trigged then it is working fine, but if I start it using WP Crontrol then the job is run two times straight after one another.

Looking in my log file I can see two entries:

[11-Sep-2022 11:08:39 UTC] cron-test callback is running 1662894519
[11-Sep-2022 11:08:40 UTC] cron-test callback is running 1662894520

I cannot understand why this is happening and I also tried to solve this by using a transient, but the problem persists:

add_action( 'cron-test', function () {
   // Cron is running
   if ( ! get_transient( 'cron-test-running' ) ) {
     set_transient( 'cron-test-running', 1 );
     error_log( 'cron-test callback running: ' . time() );
     delete_transient( 'cron-test-running' );
   }
} );

0
Cyclonecode 2 weeks 2022-09-11T06:19:20-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse