Skip to content

Commit 1543919

Browse files
authored
Try #132:
2 parents 7da911b + 88aacff commit 1543919

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

src/Command/MeiliSearchImportCommand.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
*/
2020
final class MeiliSearchImportCommand extends IndexCommand
2121
{
22+
private const DEFAULT_RESPONSE_TIMEOUT = 5000;
23+
2224
protected static $defaultName = 'meili:import';
2325
protected Client $searchClient;
2426
protected ManagerRegistry $managerRegistry;
@@ -41,7 +43,15 @@ protected function configure(): void
4143
InputOption::VALUE_NONE,
4244
'Update settings related to indices to the search engine'
4345
)
44-
->addOption('batch-size', null, InputOption::VALUE_REQUIRED);
46+
->addOption('batch-size', null, InputOption::VALUE_REQUIRED)
47+
->addOption(
48+
'response-timeout',
49+
't',
50+
InputOption::VALUE_REQUIRED,
51+
'Timeout (in ms) to get response from the search engine',
52+
self::DEFAULT_RESPONSE_TIMEOUT
53+
)
54+
;
4555
}
4656

4757
protected function execute(InputInterface $input, OutputInterface $output): int
@@ -67,6 +77,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6777
$entitiesToIndex = array_unique($indexes->toArray(), SORT_REGULAR);
6878
$batchSize = $input->getOption('batch-size');
6979
$batchSize = ctype_digit($batchSize) ? (int) $batchSize : $config->get('batchSize');
80+
$responseTimeout = ((int) $input->getOption('response-timeout')) ?: self::DEFAULT_RESPONSE_TIMEOUT;
7081

7182
/** @var array $index */
7283
foreach ($entitiesToIndex as $index) {
@@ -89,7 +100,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
89100
$batchSize * $page
90101
);
91102

92-
$responses = $this->formatIndexingResponse($this->searchService->index($manager, $entities));
103+
$responses = $this->formatIndexingResponse($this->searchService->index($manager, $entities), $responseTimeout);
93104
foreach ($responses as $indexName => $numberOfRecords) {
94105
$output->writeln(
95106
sprintf(
@@ -116,7 +127,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
116127
$update = $indexInstance->{$method}($value);
117128

118129
// Get Update status from updateID
119-
$indexInstance->waitForPendingUpdate($update['updateId']);
130+
$indexInstance->waitForPendingUpdate($update['updateId'], $responseTimeout);
120131
$updateStatus = $indexInstance->getUpdateStatus($update['updateId']);
121132

122133
if ('failed' === $updateStatus['status']) {
@@ -141,7 +152,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
141152
/*
142153
* @throws TimeOutException
143154
*/
144-
private function formatIndexingResponse(array $batch): array
155+
private function formatIndexingResponse(array $batch, int $responseTimeout): array
145156
{
146157
$formattedResponse = [];
147158

@@ -154,7 +165,7 @@ private function formatIndexingResponse(array $batch): array
154165
$indexInstance = $this->searchClient->index($indexName);
155166

156167
// Get Update status from updateID
157-
$indexInstance->waitForPendingUpdate($apiResponse['updateId']);
168+
$indexInstance->waitForPendingUpdate($apiResponse['updateId'], $responseTimeout);
158169
$updateStatus = $indexInstance->getUpdateStatus($apiResponse['updateId']);
159170

160171
if ('failed' === $updateStatus['status']) {

tests/Integration/CommandsTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,47 @@ public function testSearchImportWithCustomBatchSize(): void
156156
EOD, $importOutput);
157157
}
158158

159+
public function testSearchImportWithCustomResponseTimeout(): void
160+
{
161+
for ($i = 0; $i < 10; ++$i) {
162+
$this->createPage($i);
163+
}
164+
165+
$importCommand = $this->application->find('meili:import');
166+
$importCommandTester = new CommandTester($importCommand);
167+
$return = $importCommandTester->execute([
168+
'--indices' => 'pages',
169+
'--response-timeout' => 10000,
170+
]);
171+
$output = $importCommandTester->getDisplay();
172+
173+
$this->assertStringContainsString('Importing for index MeiliSearch\Bundle\Test\Entity\Page', $output);
174+
$this->assertStringContainsString('Indexed '.$i.' / '.$i.' MeiliSearch\Bundle\Test\Entity\Page entities into sf_phpunit__pages index', $output);
175+
$this->assertStringContainsString('Done!', $output);
176+
$this->assertSame(0, $return);
177+
178+
// Reset all
179+
parent::setUp();
180+
181+
for ($i = 0; $i < 10; ++$i) {
182+
$this->createPage($i);
183+
}
184+
185+
// test if it will work with a bad option
186+
$importCommand = $this->application->find('meili:import');
187+
$importCommandTester = new CommandTester($importCommand);
188+
$return = $importCommandTester->execute([
189+
'--indices' => 'pages',
190+
'--response-timeout' => 'asd',
191+
]);
192+
$output = $importCommandTester->getDisplay();
193+
194+
$this->assertStringContainsString('Importing for index MeiliSearch\Bundle\Test\Entity\Page', $output);
195+
$this->assertStringContainsString('Indexed '.$i.' / '.$i.' MeiliSearch\Bundle\Test\Entity\Page entities into sf_phpunit__pages index', $output);
196+
$this->assertStringContainsString('Done!', $output);
197+
$this->assertSame(0, $return);
198+
}
199+
159200
/**
160201
* Importing 'Tag' and 'Link' into the same 'tags' index.
161202
*/

0 commit comments

Comments
 (0)