Sunday, September 11, 2022
HomeWordPress DevelopmentCron occasion operating greater than as soon as if triggered from WP...

Cron occasion operating greater than as soon as if triggered from WP Crontrol


That is only a pattern plugin I created as a way to observe this difficulty down in an actual plugin, however I’m expertise the identical habits:

<?php // cron-test.php
/**
 * Writer: me
 * Description: CRON Check plugin
 * Plugin Identify: CRON Check
 * Textual content Area: cron-test
 * Model: 1.0.0
*/

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

If I wait till the cron job is trigged then it’s working fantastic, but when I begin it utilizing WP Crontrol then the job is run two instances straight after each other.

Trying in my log file I can see two entries:

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

I can not perceive why that is occurring and I additionally tried to resolve this by utilizing a transient, however the issue persists:

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments