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

Closes #6394: Reintroduce limit preload at job level #6409

Merged
merged 2 commits into from
Jan 31, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions inc/Engine/Preload/Database/Queries/Cache.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
return [
'pendingShouldAddToTheQueue' => [
'config' => [
'pendingShouldAddToTheQueue' => [
'config' => [
'rocket_preload_cache_pending_jobs_cron_rows_count' => 10,
'manual_preload' => true,
'rocket_preload_outdated' => -1,
Expand All @@ -18,7 +18,7 @@
'actions' => [

]
],
],
'expected' => [
'rows' => [
[
Expand Down Expand Up @@ -47,7 +47,7 @@
],
]
]
],
],
'InProgressAndInQueueShouldNotFail' => [
'config' => [
'rocket_preload_cache_pending_jobs_cron_rows_count' => 10,
Expand Down Expand Up @@ -120,7 +120,7 @@
],
[
'url' => 'http://example.org/test3',
'status' => 'in-progress'
'status' => 'pending'
],
[
'url' => 'http://example.org/test4',
Expand All @@ -147,11 +147,11 @@
],
[
'url' => 'http://example.org/test3',
'status' => 'in-progress'
'status' => 'pending'
],
[
'url' => 'http://example.org/test4',
'status' => 'in-progress'
'status' => 'pending'
],
[
'url' => 'http://example.org/test5',
Expand Down Expand Up @@ -181,7 +181,7 @@
],
],
[
'exists' => true,
'exists' => false,
'args' => [
'hook' => 'rocket_preload_job_preload_url',
'args' => ['http://example.org/test4']
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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