Don’t create a batch job that exceeds the range end.

This commit is contained in:
Jeff Stieler 2019-06-26 14:23:56 -06:00
parent 5b4d671833
commit b8abe63d15
2 changed files with 13 additions and 7 deletions

View File

@ -515,11 +515,15 @@ class WC_Admin_Reports_Sync {
if ( $range_size > $batch_size ) {
// If the current batch range is larger than a single batch,
// split the range into $queue_batch_size chunks.
$chunk_size = ceil( $range_size / $batch_size );
$chunk_size = (int) ceil( $range_size / $batch_size );
for ( $i = 0; $i < $batch_size; $i++ ) {
$batch_start = $range_start + ( $i * $chunk_size );
$batch_end = min( $range_end, $range_start + ( $chunk_size * ( $i + 1 ) ) - 1 );
$batch_start = (int) ( $range_start + ( $i * $chunk_size ) );
$batch_end = (int) min( $range_end, $range_start + ( $chunk_size * ( $i + 1 ) ) - 1 );
if ( $batch_start > $range_end ) {
return;
}
self::queue()->schedule_single(
$action_timestamp,

View File

@ -79,15 +79,17 @@ class WC_Tests_Reports_Regenerate_Batching extends WC_REST_Unit_Test_Case {
*/
public function test_queue_batches_splits_into_batches_correctly() {
$num_customers = 1234; // 1234 / 5 = 247 batches
$num_batches = ceil( $num_customers / $this->customers_batch_size );
$num_batches = (int) ceil( $num_customers / $this->customers_batch_size );
$chunk_size = (int) ceil( $num_batches / $this->queue_batch_size );
$num_chunks = (int) ceil( $num_batches / $chunk_size );
WC_Admin_Reports_Sync::queue_batches( 1, $num_batches, WC_Admin_Reports_Sync::CUSTOMERS_IMPORT_BATCH_ACTION );
$this->assertCount( $this->queue_batch_size, $this->queue->actions );
$this->assertCount( $num_chunks, $this->queue->actions );
$this->assertArraySubset(
array(
'hook' => WC_Admin_Reports_Sync::QUEUE_BATCH_ACTION,
'args' => array( 1, 3, WC_Admin_Reports_Sync::CUSTOMERS_IMPORT_BATCH_ACTION ),
'args' => array( 1, $chunk_size, WC_Admin_Reports_Sync::CUSTOMERS_IMPORT_BATCH_ACTION ),
),
$this->queue->actions[0]
);
@ -96,7 +98,7 @@ class WC_Tests_Reports_Regenerate_Batching extends WC_REST_Unit_Test_Case {
'hook' => WC_Admin_Reports_Sync::QUEUE_BATCH_ACTION,
'args' => array( 247, 247, WC_Admin_Reports_Sync::CUSTOMERS_IMPORT_BATCH_ACTION ),
),
$this->queue->actions[ $this->queue_batch_size - 1 ]
$this->queue->actions[ $num_chunks - 1 ]
);
}