Skip to content

Commit 6d1e481

Browse files
authored
Merge pull request #116 from deliciousbrains/fix-healthcheck-running-too-soon
Ensure 1st healthcheck runs in the future, not immediately before dispatch
2 parents e4c0e21 + dccdbdf commit 6d1e481

2 files changed

Lines changed: 54 additions & 12 deletions

File tree

classes/wp-background-process.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,25 @@ protected function completed() {
650650
do_action( $this->identifier . '_completed' );
651651
}
652652

653+
/**
654+
* Get the cron healthcheck interval in minutes.
655+
*
656+
* Default is 5 minutes, minimum is 1 minute.
657+
*
658+
* @return int
659+
*/
660+
public function get_cron_interval() {
661+
$interval = 5;
662+
663+
if ( property_exists( $this, 'cron_interval' ) ) {
664+
$interval = $this->cron_interval;
665+
}
666+
667+
$interval = apply_filters( $this->cron_interval_identifier, $interval );
668+
669+
return is_int( $interval ) && 0 < $interval ? $interval : 5;
670+
}
671+
653672
/**
654673
* Schedule the cron healthcheck job.
655674
*
@@ -660,11 +679,7 @@ protected function completed() {
660679
* @return mixed
661680
*/
662681
public function schedule_cron_healthcheck( $schedules ) {
663-
$interval = apply_filters( $this->cron_interval_identifier, 5 );
664-
665-
if ( property_exists( $this, 'cron_interval' ) ) {
666-
$interval = apply_filters( $this->cron_interval_identifier, $this->cron_interval );
667-
}
682+
$interval = $this->get_cron_interval();
668683

669684
if ( 1 === $interval ) {
670685
$display = __( 'Every Minute' );
@@ -707,7 +722,7 @@ public function handle_cron_healthcheck() {
707722
*/
708723
protected function schedule_event() {
709724
if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) {
710-
wp_schedule_event( time(), $this->cron_interval_identifier, $this->cron_hook_identifier );
725+
wp_schedule_event( time() + ( $this->get_cron_interval() * MINUTE_IN_SECONDS ), $this->cron_interval_identifier, $this->cron_hook_identifier );
711726
}
712727
}
713728

tests/Test_WP_Background_Process.php

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @package WP-Background-Processing
66
*/
77

8-
require_once __DIR__ . '/fixtures/Test_Batch_Data.php';
8+
require_once __DIR__ . '/fixtures/Test_Batch_Data.php';
99

1010
use PHPUnit\Framework\MockObject\MockObject;
1111

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

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

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

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

232232
// Allow everything.
233233
$this->setWPBPProperty( 'allowed_batch_data_classes', true );
234234
$third_batch = $this->executeWPBPMethod( 'get_batch' );
235-
$this->assertCount( 1, $third_batch->data );
235+
$this->assertCount( 1, $third_batch->data );
236236
$this->assertInstanceOf( Test_Batch_Data::class, $third_batch->data[0] );
237237
}
238238

@@ -642,4 +642,31 @@ public function test_is_active() {
642642
$this->wpbp->maybe_handle();
643643
$this->assertFalse( $this->wpbp->is_active(), 'cancel handled, queue emptied, so no longer active' );
644644
}
645+
646+
/**
647+
* Test get_cron_interval.
648+
*
649+
* @return void
650+
*/
651+
public function test_get_cron_interval() {
652+
// Default value.
653+
$this->assertEquals( 5, $this->wpbp->get_cron_interval() );
654+
655+
// Override via property (usually on subclass).
656+
$this->wpbp->cron_interval = 3;
657+
$this->assertEquals( 3, $this->wpbp->get_cron_interval() );
658+
659+
// Override via filter.
660+
$callback = function ( $interval ) {
661+
return 1;
662+
};
663+
add_filter( $this->getWPBPProperty( 'identifier' ) . '_cron_interval', $callback );
664+
$this->assertEquals( 1, $this->wpbp->get_cron_interval() );
665+
666+
remove_filter( $this->getWPBPProperty( 'identifier' ) . '_cron_interval', $callback );
667+
$this->assertEquals( 3, $this->wpbp->get_cron_interval() );
668+
669+
unset( $this->wpbp->cron_interval );
670+
$this->assertEquals( 5, $this->wpbp->get_cron_interval() );
671+
}
645672
}

0 commit comments

Comments
 (0)