mirror of
https://github.com/MrSleeps/jellyfin-scene-search.git
synced 2026-11-20 00:33:36 +00:00
Expose task IDs and support unbounded, observable Meilisearch waits
This commit is contained in:
+37
-13
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user