Skip to content

Commit 9cf7a37

Browse files
committed
Add wait method to task
1 parent 9a9349e commit 9cf7a37

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+405
-536
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
"psr-4": {
2929
"MeiliSearch\\": "src/",
3030
"Meilisearch\\": "src/"
31-
}
31+
},
32+
"files": [
33+
"src/functions.php"
34+
]
3235
},
3336
"autoload-dev": {
3437
"psr-4": {

src/Contracts/Task.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
use Meilisearch\Contracts\TaskDetails\SettingsUpdateDetails;
1616
use Meilisearch\Contracts\TaskDetails\TaskCancelationDetails;
1717
use Meilisearch\Contracts\TaskDetails\TaskDeletionDetails;
18+
use Meilisearch\Exceptions\LogicException;
1819

1920
final class Task
2021
{
2122
/**
22-
* @param non-negative-int $taskUid
23-
* @param non-empty-string|null $indexUid
24-
* @param non-empty-string|null $duration
23+
* @param non-negative-int $taskUid
24+
* @param non-empty-string|null $indexUid
25+
* @param non-empty-string|null $duration
26+
* @param \Closure(int, int, int): Task|null $await
2527
*/
2628
public function __construct(
2729
private readonly int $taskUid,
@@ -36,6 +38,7 @@ public function __construct(
3638
private readonly ?int $batchUid = null,
3739
private readonly ?TaskDetails $details = null,
3840
private readonly ?TaskError $error = null,
41+
private readonly ?\Closure $await = null,
3942
) {
4043
}
4144

@@ -113,6 +116,19 @@ public function isFinished(): bool
113116
return TaskStatus::Enqueued !== $this->status && TaskStatus::Processing !== $this->status;
114117
}
115118

119+
public function wait(int $timeoutInMs = 5000, int $intervalInMs = 50): Task
120+
{
121+
if ($this->isFinished()) {
122+
return $this;
123+
}
124+
125+
if (null !== $this->await) {
126+
return ($this->await)($this->taskUid, $timeoutInMs, $intervalInMs);
127+
}
128+
129+
throw new LogicException(\sprintf('Cannot wait for task because wait function is not provided.'));
130+
}
131+
116132
/**
117133
* @param array{
118134
* taskUid?: int,
@@ -129,8 +145,9 @@ public function isFinished(): bool
129145
* details?: array<mixed>|null,
130146
* error?: array<mixed>|null
131147
* } $data
148+
* @param \Closure(int, int, int): Task|null $await
132149
*/
133-
public static function fromArray(array $data): Task
150+
public static function fromArray(array $data, ?\Closure $await = null): Task
134151
{
135152
$details = $data['details'] ?? null;
136153

@@ -162,6 +179,7 @@ public static function fromArray(array $data): Task
162179
TaskType::SnapshotCreation => null,
163180
},
164181
\array_key_exists('error', $data) && null !== $data['error'] ? TaskError::fromArray($data['error']) : null,
182+
$await,
165183
);
166184
}
167185
}

src/Endpoints/Delegates/HandlesDocuments.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
use Meilisearch\Contracts\DocumentsQuery;
88
use Meilisearch\Contracts\DocumentsResults;
99
use Meilisearch\Contracts\Task;
10+
use Meilisearch\Endpoints\Tasks;
1011
use Meilisearch\Exceptions\ApiException;
1112
use Meilisearch\Exceptions\InvalidResponseBodyException;
1213

14+
use function Meilisearch\partial;
15+
1316
trait HandlesDocuments
1417
{
1518
/**
@@ -42,22 +45,22 @@ public function getDocuments(?DocumentsQuery $options = null): DocumentsResults
4245

4346
public function addDocuments(array $documents, ?string $primaryKey = null): Task
4447
{
45-
return Task::fromArray($this->http->post(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey]));
48+
return Task::fromArray($this->http->post(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey]), partial(Tasks::waitTask(...), $this->http));
4649
}
4750

4851
public function addDocumentsJson(string $documents, ?string $primaryKey = null): Task
4952
{
50-
return Task::fromArray($this->http->post(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey], 'application/json'));
53+
return Task::fromArray($this->http->post(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey], 'application/json'), partial(Tasks::waitTask(...), $this->http));
5154
}
5255

5356
public function addDocumentsCsv(string $documents, ?string $primaryKey = null, ?string $delimiter = null): Task
5457
{
55-
return Task::fromArray($this->http->post(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey, 'csvDelimiter' => $delimiter], 'text/csv'));
58+
return Task::fromArray($this->http->post(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey, 'csvDelimiter' => $delimiter], 'text/csv'), partial(Tasks::waitTask(...), $this->http));
5659
}
5760

5861
public function addDocumentsNdjson(string $documents, ?string $primaryKey = null): Task
5962
{
60-
return Task::fromArray($this->http->post(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey], 'application/x-ndjson'));
63+
return Task::fromArray($this->http->post(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey], 'application/x-ndjson'), partial(Tasks::waitTask(...), $this->http));
6164
}
6265

6366
/**
@@ -104,22 +107,22 @@ public function addDocumentsNdjsonInBatches(string $documents, ?int $batchSize =
104107

105108
public function updateDocuments(array $documents, ?string $primaryKey = null): Task
106109
{
107-
return Task::fromArray($this->http->put(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey]));
110+
return Task::fromArray($this->http->put(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey]), partial(Tasks::waitTask(...), $this->http));
108111
}
109112

110113
public function updateDocumentsJson(string $documents, ?string $primaryKey = null): Task
111114
{
112-
return Task::fromArray($this->http->put(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey], 'application/json'));
115+
return Task::fromArray($this->http->put(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey], 'application/json'), partial(Tasks::waitTask(...), $this->http));
113116
}
114117

115118
public function updateDocumentsCsv(string $documents, ?string $primaryKey = null, ?string $delimiter = null): Task
116119
{
117-
return Task::fromArray($this->http->put(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey, 'csvDelimiter' => $delimiter], 'text/csv'));
120+
return Task::fromArray($this->http->put(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey, 'csvDelimiter' => $delimiter], 'text/csv'), partial(Tasks::waitTask(...), $this->http));
118121
}
119122

120123
public function updateDocumentsNdjson(string $documents, ?string $primaryKey = null): Task
121124
{
122-
return Task::fromArray($this->http->put(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey], 'application/x-ndjson'));
125+
return Task::fromArray($this->http->put(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey], 'application/x-ndjson'), partial(Tasks::waitTask(...), $this->http));
123126
}
124127

125128
/**
@@ -176,29 +179,29 @@ public function updateDocumentsNdjsonInBatches(string $documents, ?int $batchSiz
176179
*/
177180
public function updateDocumentsByFunction(string $function, array $options = []): Task
178181
{
179-
return Task::fromArray($this->http->post(self::PATH.'/'.$this->uid.'/documents/edit', array_merge(['function' => $function], $options)));
182+
return Task::fromArray($this->http->post(self::PATH.'/'.$this->uid.'/documents/edit', array_merge(['function' => $function], $options)), partial(Tasks::waitTask(...), $this->http));
180183
}
181184

182185
public function deleteAllDocuments(): Task
183186
{
184-
return Task::fromArray($this->http->delete(self::PATH.'/'.$this->uid.'/documents'));
187+
return Task::fromArray($this->http->delete(self::PATH.'/'.$this->uid.'/documents'), partial(Tasks::waitTask(...), $this->http));
185188
}
186189

187190
public function deleteDocument(string|int $documentId): Task
188191
{
189-
return Task::fromArray($this->http->delete(self::PATH.'/'.$this->uid.'/documents/'.$documentId));
192+
return Task::fromArray($this->http->delete(self::PATH.'/'.$this->uid.'/documents/'.$documentId), partial(Tasks::waitTask(...), $this->http));
190193
}
191194

192195
public function deleteDocuments(array $options): Task
193196
{
194197
try {
195198
if (\array_key_exists('filter', $options) && $options['filter']) {
196-
return Task::fromArray($this->http->post(self::PATH.'/'.$this->uid.'/documents/delete', $options));
199+
return Task::fromArray($this->http->post(self::PATH.'/'.$this->uid.'/documents/delete', $options), partial(Tasks::waitTask(...), $this->http));
197200
}
198201

199202
// backwards compatibility:
200203
// expect to be a array to send alongside as $documents_ids.
201-
return Task::fromArray($this->http->post(self::PATH.'/'.$this->uid.'/documents/delete-batch', $options));
204+
return Task::fromArray($this->http->post(self::PATH.'/'.$this->uid.'/documents/delete-batch', $options), partial(Tasks::waitTask(...), $this->http));
202205
} catch (InvalidResponseBodyException $e) {
203206
throw ApiException::rethrowWithHint($e, __FUNCTION__);
204207
}

0 commit comments

Comments
 (0)