diff --git a/scene_search/meili.py b/scene_search/meili.py index 63f40b0..c615e4e 100644 --- a/scene_search/meili.py +++ b/scene_search/meili.py @@ -3,6 +3,7 @@ from __future__ import annotations import json import time from collections.abc import Iterable +from typing import Callable import requests @@ -73,7 +74,7 @@ class Meili: documents: list[dict], *, wait: bool = False, - ) -> None: + ) -> int | None: """Replace complete documents (used for indexing and targeted repairs).""" if not documents: return @@ -82,18 +83,38 @@ class Meili: params={"primaryKey": "id"}, json=documents, )) - if wait: - task_uid = response.json().get("taskUid") - if task_uid is None: + task_uid = response.json().get("taskUid") + if task_uid is None: + if wait: raise RuntimeError("Meilisearch replacement did not return a taskUid") - self.wait_for_task(int(task_uid)) + return None + task_uid = int(task_uid) + if wait: + self.wait_for_task(task_uid) + return task_uid - def wait_for_task(self, task_uid: int, *, timeout_seconds: float = 120) -> dict: - deadline = time.monotonic() + timeout_seconds + def get_task(self, task_uid: int) -> dict: + """Read the durable status of an asynchronous Meilisearch task.""" + return self._check( + self.session.get(f"{self.url}/tasks/{task_uid}", timeout=15) + ).json() + + def wait_for_task( + self, + task_uid: int, + *, + timeout_seconds: float | None = 120, + poll_seconds: float = 2.0, + on_poll: Callable[[dict], None] | None = None, + ) -> dict: + """Wait for a task. None timeout permits an intentionally long repair.""" + deadline = ( + time.monotonic() + timeout_seconds + if timeout_seconds is not None + else None + ) while True: - task = self._check( - self.session.get(f"{self.url}/tasks/{task_uid}") - ).json() + task = self.get_task(task_uid) status = str(task.get("status") or "") if status == "succeeded": return task @@ -104,11 +125,14 @@ class Meili: f"Meilisearch task {task_uid} {status}: " f"{message or task}" ) - if time.monotonic() >= deadline: + if on_poll is not None: + on_poll(task) + if deadline is not None and time.monotonic() >= deadline: raise RuntimeError( - f"Timed out waiting for Meilisearch task {task_uid}" + f"Timed out waiting for Meilisearch task {task_uid} " + f"(last status: {status or 'unknown'})" ) - time.sleep(0.1) + time.sleep(max(0.1, poll_seconds)) def update_documents( self,