Skip to content

Commit 7b9fc92

Browse files
authored
Merge pull request #230 from hkulekci/queue-timeout-configuration
a config initialized for queue timeout
2 parents d8b0737 + ea3e2f4 commit 7b9fc92

File tree

7 files changed

+116
-6
lines changed

7 files changed

+116
-6
lines changed

config/elasticsearch.php

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
'password' => env('ELASTICSEARCH_PASSWORD'),
77
'cloud_id' => env('ELASTICSEARCH_CLOUD_ID'),
88
'api_key' => env('ELASTICSEARCH_API_KEY'),
9+
'queue' => [
10+
'timeout' => env('SCOUT_QUEUE_TIMEOUT'),
11+
],
912
'indices' => [
1013
'mappings' => [
1114
'default' => [

src/Console/Commands/ImportCommand.php

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Console\Command;
88
use Illuminate\Support\Collection;
9+
use Matchish\ScoutElasticSearch\ElasticSearch\Config\Config;
910
use Matchish\ScoutElasticSearch\Jobs\Import;
1011
use Matchish\ScoutElasticSearch\Jobs\QueueableJob;
1112
use Matchish\ScoutElasticSearch\Searchable\ImportSource;
@@ -48,9 +49,11 @@ private function import(string $searchable): void
4849
$sourceFactory = app(ImportSourceFactory::class);
4950
$source = $sourceFactory::from($searchable);
5051
$job = new Import($source);
52+
$job->timeout = Config::queueTimeout();
5153

5254
if (config('scout.queue')) {
5355
$job = (new QueueableJob())->chain([$job]);
56+
$job->timeout = Config::queueTimeout();
5457
}
5558

5659
$bar = (new ProgressBarFactory($this->output))->create();

src/ElasticSearch/Config/Config.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @method static password()
99
* @method static elasticCloudId()
1010
* @method static apiKey()
11+
* @method static queueTimeout()
1112
*/
1213
class Config
1314
{

src/ElasticSearch/Config/Storage.php

+8
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ public function apiKey(): ?string
6363
return $this->loadConfig('api_key');
6464
}
6565

66+
/**
67+
* @return ?int
68+
*/
69+
public function queueTimeout(): ?int
70+
{
71+
return (int) $this->loadConfig('queue.timeout') ?: null;
72+
}
73+
6674
/**
6775
* @param string $path
6876
* @return mixed

src/Jobs/Import.php

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ final class Import
2121
*/
2222
private $source;
2323

24+
public ?int $timeout = null;
25+
2426
/**
2527
* @param ImportSource $source
2628
*/

src/Jobs/QueueableJob.php

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class QueueableJob implements ShouldQueue
1111
use Queueable;
1212
use ProgressReportable;
1313

14+
public ?int $timeout = null;
15+
1416
public function handle(): void
1517
{
1618
}

tests/Feature/ImportCommandTest.php

+97-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use App\BookWithCustomKey;
99
use App\Product;
1010
use Illuminate\Support\Facades\Artisan;
11+
use Illuminate\Support\Facades\Bus;
12+
use Matchish\ScoutElasticSearch\Jobs\Import;
13+
use Matchish\ScoutElasticSearch\Jobs\QueueableJob;
1114
use stdClass;
1215
use Symfony\Component\Console\Output\BufferedOutput;
1316
use Tests\IntegrationTestCase;
@@ -19,11 +22,11 @@ public function test_import_entites(): void
1922
$dispatcher = Product::getEventDispatcher();
2023
Product::unsetEventDispatcher();
2124

22-
$productsAmount = rand(1, 5);
25+
$productsAmount = random_int(1, 5);
2326

2427
factory(Product::class, $productsAmount)->create();
2528

26-
$productsUnsearchableAmount = rand(1, 5);
29+
$productsUnsearchableAmount = random_int(1, 5);
2730
factory(Product::class, $productsUnsearchableAmount)->states(['archive'])->create();
2831

2932
Product::setEventDispatcher($dispatcher);
@@ -50,7 +53,7 @@ public function test_import_entites_in_queue(): void
5053
$dispatcher = Product::getEventDispatcher();
5154
Product::unsetEventDispatcher();
5255

53-
$productsAmount = rand(1, 5);
56+
$productsAmount = random_int(1, 5);
5457
factory(Product::class, $productsAmount)->create();
5558

5659
Product::setEventDispatcher($dispatcher);
@@ -134,7 +137,7 @@ public function test_remove_old_index_after_switching_to_new(): void
134137
$dispatcher = Product::getEventDispatcher();
135138
Product::unsetEventDispatcher();
136139

137-
$productsAmount = rand(1, 5);
140+
$productsAmount = random_int(1, 5);
138141

139142
factory(Product::class, $productsAmount)->create();
140143

@@ -145,7 +148,7 @@ public function test_remove_old_index_after_switching_to_new(): void
145148
$this->assertFalse($this->elasticsearch->indices()->exists(['index' => 'products_old'])->asBool(), 'Old index must be deleted');
146149
}
147150

148-
public function test_progress_report()
151+
public function test_progress_report(): void
149152
{
150153
$output = new BufferedOutput();
151154
Artisan::call('scout:import', ['searchable' => [Product::class, Book::class]], $output);
@@ -165,7 +168,7 @@ public function test_progress_report()
165168
trim($output[30]));
166169
}
167170

168-
public function test_progress_report_in_queue()
171+
public function test_progress_report_in_queue(): void
169172
{
170173
$this->app['config']->set('scout.queue', ['connection' => 'sync', 'queue' => 'scout']);
171174

@@ -177,4 +180,92 @@ public function test_progress_report_in_queue()
177180
$this->assertContains(trans('scout::import.start', ['searchable' => Product::class]), $output);
178181
$this->assertContains('[OK] '.trans('scout::import.done.queue', ['searchable' => Product::class]), $output);
179182
}
183+
184+
public function test_queue_timeout_configuration(): void
185+
{
186+
Bus::fake([
187+
QueueableJob::class,
188+
]);
189+
190+
$this->app['config']->set('scout.queue', ['connection' => 'sync', 'queue' => 'scout']);
191+
$this->app['config']->set('elasticsearch.queue.timeout', 2);
192+
193+
$output = new BufferedOutput();
194+
Artisan::call('scout:import', [], $output);
195+
196+
$output = array_map('trim', explode("\n", $output->fetch()));
197+
198+
$this->assertContains(trans('scout::import.start', ['searchable' => Product::class]), $output);
199+
$this->assertContains('[OK] '.trans('scout::import.done.queue', ['searchable' => Product::class]), $output);
200+
201+
Bus::assertDispatched(function (QueueableJob $job) {
202+
return $job->timeout === 2;
203+
});
204+
}
205+
206+
public function test_chained_queue_timeout_configuration(): void
207+
{
208+
Bus::fake([
209+
Import::class,
210+
]);
211+
212+
$this->app['config']->set('scout.queue', ['connection' => 'sync', 'queue' => 'scout']);
213+
$this->app['config']->set('elasticsearch.queue.timeout', 2);
214+
215+
$output = new BufferedOutput();
216+
Artisan::call('scout:import', [], $output);
217+
218+
$output = array_map('trim', explode("\n", $output->fetch()));
219+
220+
$this->assertContains(trans('scout::import.start', ['searchable' => Product::class]), $output);
221+
$this->assertContains('[OK] '.trans('scout::import.done.queue', ['searchable' => Product::class]), $output);
222+
223+
Bus::assertDispatched(function (Import $job) {
224+
return $job->timeout === 2;
225+
});
226+
}
227+
228+
public function test_chained_queue_timeout_configuration_with_null_value(): void
229+
{
230+
Bus::fake([
231+
Import::class,
232+
]);
233+
234+
$this->app['config']->set('scout.queue', ['connection' => 'sync', 'queue' => 'scout']);
235+
$this->app['config']->set('elasticsearch.queue.timeout', null);
236+
237+
$output = new BufferedOutput();
238+
Artisan::call('scout:import', [], $output);
239+
240+
$output = array_map('trim', explode("\n", $output->fetch()));
241+
242+
$this->assertContains(trans('scout::import.start', ['searchable' => Product::class]), $output);
243+
$this->assertContains('[OK] '.trans('scout::import.done.queue', ['searchable' => Product::class]), $output);
244+
245+
Bus::assertDispatched(function (Import $job) {
246+
return $job->timeout === null;
247+
});
248+
}
249+
250+
public function test_chained_queue_timeout_configuration_with_empty_string(): void
251+
{
252+
Bus::fake([
253+
Import::class,
254+
]);
255+
256+
$this->app['config']->set('scout.queue', ['connection' => 'sync', 'queue' => 'scout']);
257+
$this->app['config']->set('elasticsearch.queue.timeout', '');
258+
259+
$output = new BufferedOutput();
260+
Artisan::call('scout:import', [], $output);
261+
262+
$output = array_map('trim', explode("\n", $output->fetch()));
263+
264+
$this->assertContains(trans('scout::import.start', ['searchable' => Product::class]), $output);
265+
$this->assertContains('[OK] '.trans('scout::import.done.queue', ['searchable' => Product::class]), $output);
266+
267+
Bus::assertDispatched(function (Import $job) {
268+
return $job->timeout === null;
269+
});
270+
}
180271
}

0 commit comments

Comments
 (0)