Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure 1st healthcheck runs in the future, not immediately before dispatch #116

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions classes/wp-background-process.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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() );
}
}