Skip to content

Commit

Permalink
Closes #6394: Reintroduce limit preload at job level
Browse files Browse the repository at this point in the history
  • Loading branch information
Miraeld committed Jan 24, 2024
1 parent ecc9ced commit 40da0dd
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 16 deletions.
12 changes: 10 additions & 2 deletions inc/Engine/Preload/Database/Queries/Cache.php
Expand Up @@ -338,8 +338,16 @@ public function remove_all_not_accessed_rows( float $delay = 1, string $unit = '
* @return array
*/
public function get_pending_jobs( int $total = 45 ) {
$inprogress_count = $this->query(
[
'count' => true,
'status' => 'in-progress',
'is_locked' => false,
],
false
);

if ( $total <= 0 ) {
if ( $total <= 0 || $inprogress_count >= $total ) {
return [];
}

Expand All @@ -358,7 +366,7 @@ public function get_pending_jobs( int $total = 45 ) {

return $this->query(
[
'number' => $total,
'number' => ( $total - $inprogress_count ),
'status' => 'pending',
'fields' => [
'id',
Expand Down
Expand Up @@ -8,6 +8,7 @@
'shouldReturnPendingRowsWhenInProgressLessThanTotal' => [
'config' => [
'total' => 45,
'in-progress' => 1,
'results' => [
$cache,
]
Expand All @@ -16,9 +17,33 @@
$cache
]
],
'shouldNotReturnEmptyPendingRowsWhenInProgressNotEqualsTotal' => [
'config' => [
'total' => 45,
'in-progress' => 44,
'results' => [
$cache,
],
],
'expected' => [
$cache
]
],
'shouldReturnEmptyPendingRowsWhenInProgressEqualsTotal' => [
'config' => [
'total' => 45,
'in-progress' => 45,
'results' => [
$cache,
],
],
'expected' => [
]
],
'negativeCountShouldReturnEmpty' => [
'config' => [
'total' => -10,
'in-progress' => 35,
'results' => [
$cache,
]
Expand Down
Expand Up @@ -24,22 +24,55 @@ protected function setUp(): void
* @dataProvider configTestData
*/
public function testShouldReturnPending($config, $expected) {
$queue = new SplQueue();

if($config['total'] > 0) {
if($config['total'] > 0 && $config['in-progress'] < $config['total'] ) {
$queue->enqueue(
[
'params' =>
[
'count' => true,
'status' => 'in-progress',
'is_locked' => false,
],
'return' => $config['in-progress']
]
);

$queue->enqueue(
[
'params' =>
[
'number' => $config['total']-$config['in-progress'],
'status' => 'pending',
'fields' => [
'id',
'url',
],
'job_id__not_in' => [
'not_in' => '',
],
'orderby' => Filters\applied( 'rocket_preload_order' ) > 0 ? 'id' : 'modified',
'order' => 'asc',
'is_locked' => false
],
'return' => $config['results']
]
);

$this->query->expects(self::exactly(2))->method('query')
->willReturnCallback(function($params) use ($queue) {
$dequeue = $queue->dequeue();
$this->assertEquals($dequeue['params'], $params);
return $dequeue['return'];
});
} else {
$this->query->expects(self::once())->method('query')->with([
'number' => $config['total'],
'status' => 'pending',
'fields' => [
'id',
'url',
],
'job_id__not_in' => [
'not_in' => '',
],
'orderby' => Filters\applied( 'rocket_preload_order' ) > 0 ? 'id' : 'modified',
'order' => 'asc',
'is_locked' => false
])->willReturn($config['results']);
'count' => true,
'status' => 'in-progress',
'is_locked' => false,
])->willReturn($config['in-progress']);

}

$this->assertSame($expected, $this->query->get_pending_jobs($config['total']));
Expand Down

0 comments on commit 40da0dd

Please sign in to comment.