From fb5e9523411e1a7d526ed1fc6fac4a9569a2de2c Mon Sep 17 00:00:00 2001 From: Haydar Kulekci Date: Fri, 27 Jan 2023 01:15:44 +0300 Subject: [PATCH 1/9] a config initialized for queue timeout --- config/elasticsearch.php | 3 +++ src/Console/Commands/ImportCommand.php | 1 + 2 files changed, 4 insertions(+) diff --git a/config/elasticsearch.php b/config/elasticsearch.php index 58c3a91b..aa1caaa1 100644 --- a/config/elasticsearch.php +++ b/config/elasticsearch.php @@ -6,6 +6,9 @@ 'password' => env('ELASTICSEARCH_PASSWORD'), 'cloud_id' => env('ELASTICSEARCH_CLOUD_ID'), 'api_key' => env('ELASTICSEARCH_API_KEY'), + 'queue' => [ + 'timeout' => env('SCOUT_QUEUE_TIMEOUT', 60), + ], 'indices' => [ 'mappings' => [ 'default' => [ diff --git a/src/Console/Commands/ImportCommand.php b/src/Console/Commands/ImportCommand.php index 18c8656a..0b2d22fb 100644 --- a/src/Console/Commands/ImportCommand.php +++ b/src/Console/Commands/ImportCommand.php @@ -51,6 +51,7 @@ private function import(string $searchable): void if (config('scout.queue')) { $job = (new QueueableJob())->chain([$job]); + $job->timeout = config('elasticsearch.queue.timeout'); } $bar = (new ProgressBarFactory($this->output))->create(); From d02c5978cc3e72f93cbf5d64ca08b55de250c35a Mon Sep 17 00:00:00 2001 From: Haydar Kulekci Date: Fri, 27 Jan 2023 14:36:18 +0300 Subject: [PATCH 2/9] added timeout property for the QueueableJob --- src/Jobs/QueueableJob.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Jobs/QueueableJob.php b/src/Jobs/QueueableJob.php index 027ab896..8f98d366 100644 --- a/src/Jobs/QueueableJob.php +++ b/src/Jobs/QueueableJob.php @@ -11,6 +11,8 @@ class QueueableJob implements ShouldQueue use Queueable; use ProgressReportable; + public int $timeout = 60; + public function handle(): void { } From 93e4efa2406a74e85292880a56dee8235db78454 Mon Sep 17 00:00:00 2001 From: Haydar Kulekci Date: Sun, 29 Jan 2023 19:15:51 +0300 Subject: [PATCH 3/9] ImportCommandTest improved with Bus::fake() --- src/Console/Commands/ImportCommand.php | 3 +- src/Jobs/Import.php | 2 + tests/Feature/ImportCommandTest.php | 51 +++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/Console/Commands/ImportCommand.php b/src/Console/Commands/ImportCommand.php index 0b2d22fb..611486f7 100644 --- a/src/Console/Commands/ImportCommand.php +++ b/src/Console/Commands/ImportCommand.php @@ -48,10 +48,11 @@ private function import(string $searchable): void $sourceFactory = app(ImportSourceFactory::class); $source = $sourceFactory::from($searchable); $job = new Import($source); + $job->timeout = config('elasticsearch.queue.timeout', 60); if (config('scout.queue')) { $job = (new QueueableJob())->chain([$job]); - $job->timeout = config('elasticsearch.queue.timeout'); + $job->timeout = config('elasticsearch.queue.timeout', 60); } $bar = (new ProgressBarFactory($this->output))->create(); diff --git a/src/Jobs/Import.php b/src/Jobs/Import.php index 7aa440f0..5e32db54 100644 --- a/src/Jobs/Import.php +++ b/src/Jobs/Import.php @@ -21,6 +21,8 @@ final class Import */ private $source; + public int $timeout = 60; + /** * @param ImportSource $source */ diff --git a/tests/Feature/ImportCommandTest.php b/tests/Feature/ImportCommandTest.php index 9b0c9147..2f4fe80a 100644 --- a/tests/Feature/ImportCommandTest.php +++ b/tests/Feature/ImportCommandTest.php @@ -8,6 +8,9 @@ use App\BookWithCustomKey; use App\Product; use Illuminate\Support\Facades\Artisan; +use Illuminate\Support\Facades\Bus; +use Matchish\ScoutElasticSearch\Jobs\Import; +use Matchish\ScoutElasticSearch\Jobs\QueueableJob; use stdClass; use Symfony\Component\Console\Output\BufferedOutput; use Tests\IntegrationTestCase; @@ -145,7 +148,7 @@ public function test_remove_old_index_after_switching_to_new(): void $this->assertFalse($this->elasticsearch->indices()->exists(['index' => 'products_old'])->asBool(), 'Old index must be deleted'); } - public function test_progress_report() + public function test_progress_report(): void { $output = new BufferedOutput(); Artisan::call('scout:import', ['searchable' => [Product::class, Book::class]], $output); @@ -165,7 +168,7 @@ public function test_progress_report() trim($output[30])); } - public function test_progress_report_in_queue() + public function test_progress_report_in_queue(): void { $this->app['config']->set('scout.queue', ['connection' => 'sync', 'queue' => 'scout']); @@ -177,4 +180,48 @@ public function test_progress_report_in_queue() $this->assertContains(trans('scout::import.start', ['searchable' => Product::class]), $output); $this->assertContains('[OK] '.trans('scout::import.done.queue', ['searchable' => Product::class]), $output); } + + public function test_queue_timeout_configuration(): void + { + Bus::fake([ + QueueableJob::class, + ]); + + $this->app['config']->set('scout.queue', ['connection' => 'sync', 'queue' => 'scout']); + $this->app['config']->set('elasticsearch.queue.timeout', 2); + + $output = new BufferedOutput(); + Artisan::call('scout:import', [], $output); + + $output = array_map('trim', explode("\n", $output->fetch())); + + $this->assertContains(trans('scout::import.start', ['searchable' => Product::class]), $output); + $this->assertContains('[OK] '.trans('scout::import.done.queue', ['searchable' => Product::class]), $output); + + Bus::assertDispatched(function (QueueableJob $job) { + return $job->timeout === 2; + }); + } + + public function test_chained_queue_timeout_configuration(): void + { + Bus::fake([ + Import::class, + ]); + + $this->app['config']->set('scout.queue', ['connection' => 'sync', 'queue' => 'scout']); + $this->app['config']->set('elasticsearch.queue.timeout', 2); + + $output = new BufferedOutput(); + Artisan::call('scout:import', [], $output); + + $output = array_map('trim', explode("\n", $output->fetch())); + + $this->assertContains(trans('scout::import.start', ['searchable' => Product::class]), $output); + $this->assertContains('[OK] '.trans('scout::import.done.queue', ['searchable' => Product::class]), $output); + + Bus::assertDispatched(function (Import $job) { + return $job->timeout === 2; + }); + } } From e7f51ef3f2cbf7aab8387d3068c446f200f64a3d Mon Sep 17 00:00:00 2001 From: Haydar Kulekci Date: Sun, 5 Feb 2023 22:13:02 +0300 Subject: [PATCH 4/9] broken test scenario --- tests/Feature/ImportCommandTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Feature/ImportCommandTest.php b/tests/Feature/ImportCommandTest.php index 2f4fe80a..e4b0c554 100644 --- a/tests/Feature/ImportCommandTest.php +++ b/tests/Feature/ImportCommandTest.php @@ -199,7 +199,7 @@ public function test_queue_timeout_configuration(): void $this->assertContains('[OK] '.trans('scout::import.done.queue', ['searchable' => Product::class]), $output); Bus::assertDispatched(function (QueueableJob $job) { - return $job->timeout === 2; + return $job->timeout === 60; }); } @@ -221,7 +221,7 @@ public function test_chained_queue_timeout_configuration(): void $this->assertContains('[OK] '.trans('scout::import.done.queue', ['searchable' => Product::class]), $output); Bus::assertDispatched(function (Import $job) { - return $job->timeout === 2; + return $job->timeout === 60; }); } } From 494d31ca4c1d1c35002134ec19c5a53e94978916 Mon Sep 17 00:00:00 2001 From: Haydar Kulekci Date: Sun, 5 Feb 2023 22:21:52 +0300 Subject: [PATCH 5/9] fix for the broken test --- tests/Feature/ImportCommandTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Feature/ImportCommandTest.php b/tests/Feature/ImportCommandTest.php index e4b0c554..2f4fe80a 100644 --- a/tests/Feature/ImportCommandTest.php +++ b/tests/Feature/ImportCommandTest.php @@ -199,7 +199,7 @@ public function test_queue_timeout_configuration(): void $this->assertContains('[OK] '.trans('scout::import.done.queue', ['searchable' => Product::class]), $output); Bus::assertDispatched(function (QueueableJob $job) { - return $job->timeout === 60; + return $job->timeout === 2; }); } @@ -221,7 +221,7 @@ public function test_chained_queue_timeout_configuration(): void $this->assertContains('[OK] '.trans('scout::import.done.queue', ['searchable' => Product::class]), $output); Bus::assertDispatched(function (Import $job) { - return $job->timeout === 60; + return $job->timeout === 2; }); } } From 0f410469a639a324ed280c5e50a041fa891c34b1 Mon Sep 17 00:00:00 2001 From: Haydar Kulekci Date: Sun, 12 Feb 2023 03:45:10 +0300 Subject: [PATCH 6/9] changed default timeout value as null instead of 60 --- config/elasticsearch.php | 2 +- src/Console/Commands/ImportCommand.php | 4 ++-- src/Jobs/Import.php | 2 +- src/Jobs/QueueableJob.php | 2 +- tests/Feature/ImportCommandTest.php | 22 ++++++++++++++++++++++ 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/config/elasticsearch.php b/config/elasticsearch.php index aa1caaa1..4f5a5273 100644 --- a/config/elasticsearch.php +++ b/config/elasticsearch.php @@ -7,7 +7,7 @@ 'cloud_id' => env('ELASTICSEARCH_CLOUD_ID'), 'api_key' => env('ELASTICSEARCH_API_KEY'), 'queue' => [ - 'timeout' => env('SCOUT_QUEUE_TIMEOUT', 60), + 'timeout' => env('SCOUT_QUEUE_TIMEOUT'), ], 'indices' => [ 'mappings' => [ diff --git a/src/Console/Commands/ImportCommand.php b/src/Console/Commands/ImportCommand.php index 611486f7..d67a9099 100644 --- a/src/Console/Commands/ImportCommand.php +++ b/src/Console/Commands/ImportCommand.php @@ -48,11 +48,11 @@ private function import(string $searchable): void $sourceFactory = app(ImportSourceFactory::class); $source = $sourceFactory::from($searchable); $job = new Import($source); - $job->timeout = config('elasticsearch.queue.timeout', 60); + $job->timeout = config('elasticsearch.queue.timeout'); if (config('scout.queue')) { $job = (new QueueableJob())->chain([$job]); - $job->timeout = config('elasticsearch.queue.timeout', 60); + $job->timeout = config('elasticsearch.queue.timeout'); } $bar = (new ProgressBarFactory($this->output))->create(); diff --git a/src/Jobs/Import.php b/src/Jobs/Import.php index 5e32db54..92c76045 100644 --- a/src/Jobs/Import.php +++ b/src/Jobs/Import.php @@ -21,7 +21,7 @@ final class Import */ private $source; - public int $timeout = 60; + public ?int $timeout = null; /** * @param ImportSource $source diff --git a/src/Jobs/QueueableJob.php b/src/Jobs/QueueableJob.php index 8f98d366..d799f7de 100644 --- a/src/Jobs/QueueableJob.php +++ b/src/Jobs/QueueableJob.php @@ -11,7 +11,7 @@ class QueueableJob implements ShouldQueue use Queueable; use ProgressReportable; - public int $timeout = 60; + public ?int $timeout = null; public function handle(): void { diff --git a/tests/Feature/ImportCommandTest.php b/tests/Feature/ImportCommandTest.php index 2f4fe80a..4bf4ae2c 100644 --- a/tests/Feature/ImportCommandTest.php +++ b/tests/Feature/ImportCommandTest.php @@ -224,4 +224,26 @@ public function test_chained_queue_timeout_configuration(): void return $job->timeout === 2; }); } + + public function test_chained_queue_timeout_configuration_with_empty_string(): void + { + Bus::fake([ + Import::class, + ]); + + $this->app['config']->set('scout.queue', ['connection' => 'sync', 'queue' => 'scout']); + $this->app['config']->set('elasticsearch.queue.timeout', null); + + $output = new BufferedOutput(); + Artisan::call('scout:import', [], $output); + + $output = array_map('trim', explode("\n", $output->fetch())); + + $this->assertContains(trans('scout::import.start', ['searchable' => Product::class]), $output); + $this->assertContains('[OK] '.trans('scout::import.done.queue', ['searchable' => Product::class]), $output); + + Bus::assertDispatched(function (Import $job) { + return $job->timeout === null; + }); + } } From e32007f0a0b4279f3280714541a7528f002b44fd Mon Sep 17 00:00:00 2001 From: Haydar Kulekci Date: Sun, 12 Feb 2023 03:49:56 +0300 Subject: [PATCH 7/9] test with empty valued queue timeout --- tests/Feature/ImportCommandTest.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/tests/Feature/ImportCommandTest.php b/tests/Feature/ImportCommandTest.php index 4bf4ae2c..e199f688 100644 --- a/tests/Feature/ImportCommandTest.php +++ b/tests/Feature/ImportCommandTest.php @@ -225,7 +225,7 @@ public function test_chained_queue_timeout_configuration(): void }); } - public function test_chained_queue_timeout_configuration_with_empty_string(): void + public function test_chained_queue_timeout_configuration_with_null_value(): void { Bus::fake([ Import::class, @@ -246,4 +246,26 @@ public function test_chained_queue_timeout_configuration_with_empty_string(): vo return $job->timeout === null; }); } + + public function test_chained_queue_timeout_configuration_with_empty_string(): void + { + Bus::fake([ + Import::class, + ]); + + $this->app['config']->set('scout.queue', ['connection' => 'sync', 'queue' => 'scout']); + $this->app['config']->set('elasticsearch.queue.timeout', ''); + + $output = new BufferedOutput(); + Artisan::call('scout:import', [], $output); + + $output = array_map('trim', explode("\n", $output->fetch())); + + $this->assertContains(trans('scout::import.start', ['searchable' => Product::class]), $output); + $this->assertContains('[OK] '.trans('scout::import.done.queue', ['searchable' => Product::class]), $output); + + Bus::assertDispatched(function (Import $job) { + return $job->timeout === null; + }); + } } From 292f8527c71dbd1281ca3d2079cee862204d2b42 Mon Sep 17 00:00:00 2001 From: Haydar Kulekci Date: Sun, 12 Feb 2023 18:17:24 +0300 Subject: [PATCH 8/9] queueTimeout config moved to Config/Storage::class with nullable logic --- src/Console/Commands/ImportCommand.php | 5 +++-- src/ElasticSearch/Config/Config.php | 1 + src/ElasticSearch/Config/Storage.php | 8 ++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Console/Commands/ImportCommand.php b/src/Console/Commands/ImportCommand.php index d67a9099..fbdf0675 100644 --- a/src/Console/Commands/ImportCommand.php +++ b/src/Console/Commands/ImportCommand.php @@ -6,6 +6,7 @@ use Illuminate\Console\Command; use Illuminate\Support\Collection; +use Matchish\ScoutElasticSearch\ElasticSearch\Config\Config; use Matchish\ScoutElasticSearch\Jobs\Import; use Matchish\ScoutElasticSearch\Jobs\QueueableJob; use Matchish\ScoutElasticSearch\Searchable\ImportSource; @@ -48,11 +49,11 @@ private function import(string $searchable): void $sourceFactory = app(ImportSourceFactory::class); $source = $sourceFactory::from($searchable); $job = new Import($source); - $job->timeout = config('elasticsearch.queue.timeout'); + $job->timeout = Config::queueTimeout(); if (config('scout.queue')) { $job = (new QueueableJob())->chain([$job]); - $job->timeout = config('elasticsearch.queue.timeout'); + $job->timeout = Config::queueTimeout(); } $bar = (new ProgressBarFactory($this->output))->create(); diff --git a/src/ElasticSearch/Config/Config.php b/src/ElasticSearch/Config/Config.php index f892eefd..93817fed 100644 --- a/src/ElasticSearch/Config/Config.php +++ b/src/ElasticSearch/Config/Config.php @@ -8,6 +8,7 @@ * @method static password() * @method static elasticCloudId() * @method static apiKey() + * @method static queueTimeout() */ class Config { diff --git a/src/ElasticSearch/Config/Storage.php b/src/ElasticSearch/Config/Storage.php index d583c5c9..87abf2d4 100644 --- a/src/ElasticSearch/Config/Storage.php +++ b/src/ElasticSearch/Config/Storage.php @@ -63,6 +63,14 @@ public function apiKey(): ?string return $this->loadConfig('api_key'); } + /** + * @return ?int + */ + public function queueTimeout(): ?int + { + return (int) $this->loadConfig('queue.timeout') ?: null; + } + /** * @param string $path * @return mixed From ea3e2f4a56accf4407da26baaa21ca6ee81fe6c8 Mon Sep 17 00:00:00 2001 From: Haydar Kulekci Date: Fri, 17 Feb 2023 12:43:23 +0300 Subject: [PATCH 9/9] rand() function changed as random_int() --- tests/Feature/ImportCommandTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Feature/ImportCommandTest.php b/tests/Feature/ImportCommandTest.php index e199f688..f973ab96 100644 --- a/tests/Feature/ImportCommandTest.php +++ b/tests/Feature/ImportCommandTest.php @@ -22,11 +22,11 @@ public function test_import_entites(): void $dispatcher = Product::getEventDispatcher(); Product::unsetEventDispatcher(); - $productsAmount = rand(1, 5); + $productsAmount = random_int(1, 5); factory(Product::class, $productsAmount)->create(); - $productsUnsearchableAmount = rand(1, 5); + $productsUnsearchableAmount = random_int(1, 5); factory(Product::class, $productsUnsearchableAmount)->states(['archive'])->create(); Product::setEventDispatcher($dispatcher); @@ -53,7 +53,7 @@ public function test_import_entites_in_queue(): void $dispatcher = Product::getEventDispatcher(); Product::unsetEventDispatcher(); - $productsAmount = rand(1, 5); + $productsAmount = random_int(1, 5); factory(Product::class, $productsAmount)->create(); Product::setEventDispatcher($dispatcher); @@ -137,7 +137,7 @@ public function test_remove_old_index_after_switching_to_new(): void $dispatcher = Product::getEventDispatcher(); Product::unsetEventDispatcher(); - $productsAmount = rand(1, 5); + $productsAmount = random_int(1, 5); factory(Product::class, $productsAmount)->create();