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:
+35
-11
@@ -3,6 +3,7 @@ from __future__ import annotations
|
|||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
from collections.abc import Iterable
|
from collections.abc import Iterable
|
||||||
|
from typing import Callable
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
@@ -73,7 +74,7 @@ class Meili:
|
|||||||
documents: list[dict],
|
documents: list[dict],
|
||||||
*,
|
*,
|
||||||
wait: bool = False,
|
wait: bool = False,
|
||||||
) -> None:
|
) -> int | None:
|
||||||
"""Replace complete documents (used for indexing and targeted repairs)."""
|
"""Replace complete documents (used for indexing and targeted repairs)."""
|
||||||
if not documents:
|
if not documents:
|
||||||
return
|
return
|
||||||
@@ -82,18 +83,38 @@ class Meili:
|
|||||||
params={"primaryKey": "id"},
|
params={"primaryKey": "id"},
|
||||||
json=documents,
|
json=documents,
|
||||||
))
|
))
|
||||||
if wait:
|
|
||||||
task_uid = response.json().get("taskUid")
|
task_uid = response.json().get("taskUid")
|
||||||
if task_uid is None:
|
if task_uid is None:
|
||||||
|
if wait:
|
||||||
raise RuntimeError("Meilisearch replacement did not return a taskUid")
|
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:
|
def get_task(self, task_uid: int) -> dict:
|
||||||
deadline = time.monotonic() + timeout_seconds
|
"""Read the durable status of an asynchronous Meilisearch task."""
|
||||||
while True:
|
return self._check(
|
||||||
task = self._check(
|
self.session.get(f"{self.url}/tasks/{task_uid}", timeout=15)
|
||||||
self.session.get(f"{self.url}/tasks/{task_uid}")
|
|
||||||
).json()
|
).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.get_task(task_uid)
|
||||||
status = str(task.get("status") or "")
|
status = str(task.get("status") or "")
|
||||||
if status == "succeeded":
|
if status == "succeeded":
|
||||||
return task
|
return task
|
||||||
@@ -104,11 +125,14 @@ class Meili:
|
|||||||
f"Meilisearch task {task_uid} {status}: "
|
f"Meilisearch task {task_uid} {status}: "
|
||||||
f"{message or task}"
|
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(
|
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(
|
def update_documents(
|
||||||
self,
|
self,
|
||||||
|
|||||||
Reference in New Issue
Block a user