Skip to content

Commit

Permalink
Merge pull request #116 from deliciousbrains/fix-healthcheck-running-…
Browse files Browse the repository at this point in the history
…too-soon

Ensure 1st healthcheck runs in the future, not immediately before dispatch
  • Loading branch information
ianmjones committed Feb 28, 2024
2 parents e4c0e21 + dccdbdf commit 6d1e481
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
27 changes: 21 additions & 6 deletions classes/wp-background-process.php
Expand Up @@ -650,6 +650,25 @@ protected function completed() {
do_action( $this->identifier . '_completed' );
}

/**
* Get the cron healthcheck interval in minutes.
*
* Default is 5 minutes, minimum is 1 minute.
*
* @return int
*/
public function get_cron_interval() {
$interval = 5;

if ( property_exists( $this, 'cron_interval' ) ) {
$interval = $this->cron_interval;
}

$interval = apply_filters( $this->cron_interval_identifier, $interval );

return is_int( $interval ) && 0 < $interval ? $interval : 5;
}

/**
* Schedule the cron healthcheck job.
*
Expand All @@ -660,11 +679,7 @@ protected function completed() {
* @return mixed
*/
public function schedule_cron_healthcheck( $schedules ) {
$interval = apply_filters( $this->cron_interval_identifier, 5 );

if ( property_exists( $this, 'cron_interval' ) ) {
$interval = apply_filters( $this->cron_interval_identifier, $this->cron_interval );
}
$interval = $this->get_cron_interval();

if ( 1 === $interval ) {
$display = __( 'Every Minute' );
Expand Down Expand Up @@ -707,7 +722,7 @@ public function handle_cron_healthcheck() {
*/
protected function schedule_event() {
if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) {
wp_schedule_event( time(), $this->cron_interval_identifier, $this->cron_hook_identifier );
wp_schedule_event( time() + ( $this->get_cron_interval() * MINUTE_IN_SECONDS ), $this->cron_interval_identifier, $this->cron_hook_identifier );
}
}

Expand Down
39 changes: 33 additions & 6 deletions tests/Test_WP_Background_Process.php
Expand Up @@ -5,7 +5,7 @@
* @package WP-Background-Processing
*/

require_once __DIR__ . '/fixtures/Test_Batch_Data.php';
require_once __DIR__ . '/fixtures/Test_Batch_Data.php';

use PHPUnit\Framework\MockObject\MockObject;

Expand Down Expand Up @@ -208,31 +208,31 @@ public function test_get_batch() {
$this->assertEquals( array( $batch_data_object ), $this->getWPBPProperty( 'data' ) );
$this->wpbp->save();
$third_batch = $this->executeWPBPMethod( 'get_batch' );
$this->assertCount( 1, $third_batch->data );
$this->assertCount( 1, $third_batch->data );
$this->assertInstanceOf( Test_Batch_Data::class, $third_batch->data[0] );

// Explicitly set allowed classes to Test_Batch_Data.
$this->setWPBPProperty( 'allowed_batch_data_classes', array( Test_Batch_Data::class ) );
$third_batch = $this->executeWPBPMethod( 'get_batch' );
$this->assertCount( 1, $third_batch->data );
$this->assertCount( 1, $third_batch->data );
$this->assertInstanceOf( Test_Batch_Data::class, $third_batch->data[0] );

// Allow a different class.
$this->setWPBPProperty( 'allowed_batch_data_classes', array( stdClass::class ) );
$third_batch = $this->executeWPBPMethod( 'get_batch' );
$this->assertCount( 1, $third_batch->data );
$this->assertCount( 1, $third_batch->data );
$this->assertInstanceOf( __PHP_Incomplete_Class::class, $third_batch->data[0] );

// Disallow all classes.
$this->setWPBPProperty( 'allowed_batch_data_classes', false );
$third_batch = $this->executeWPBPMethod( 'get_batch' );
$this->assertCount( 1, $third_batch->data );
$this->assertCount( 1, $third_batch->data );
$this->assertInstanceOf( __PHP_Incomplete_Class::class, $third_batch->data[0] );

// Allow everything.
$this->setWPBPProperty( 'allowed_batch_data_classes', true );
$third_batch = $this->executeWPBPMethod( 'get_batch' );
$this->assertCount( 1, $third_batch->data );
$this->assertCount( 1, $third_batch->data );
$this->assertInstanceOf( Test_Batch_Data::class, $third_batch->data[0] );
}

Expand Down Expand Up @@ -642,4 +642,31 @@ public function test_is_active() {
$this->wpbp->maybe_handle();
$this->assertFalse( $this->wpbp->is_active(), 'cancel handled, queue emptied, so no longer active' );
}

/**
* Test get_cron_interval.
*
* @return void
*/
public function test_get_cron_interval() {
// Default value.
$this->assertEquals( 5, $this->wpbp->get_cron_interval() );

// Override via property (usually on subclass).
$this->wpbp->cron_interval = 3;
$this->assertEquals( 3, $this->wpbp->get_cron_interval() );

// Override via filter.
$callback = function ( $interval ) {
return 1;
};
add_filter( $this->getWPBPProperty( 'identifier' ) . '_cron_interval', $callback );
$this->assertEquals( 1, $this->wpbp->get_cron_interval() );

remove_filter( $this->getWPBPProperty( 'identifier' ) . '_cron_interval', $callback );
$this->assertEquals( 3, $this->wpbp->get_cron_interval() );

unset( $this->wpbp->cron_interval );
$this->assertEquals( 5, $this->wpbp->get_cron_interval() );
}
}

0 comments on commit 6d1e481

Please sign in to comment.