mirror of
https://github.com/MrSleeps/jellyfin-scene-search.git
synced 2026-11-19 00:03:36 +00:00
Wait for incremental caption update tasks
This commit is contained in:
+37
-2
@@ -1,6 +1,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import time
|
||||||
from collections.abc import Iterable
|
from collections.abc import Iterable
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
@@ -76,18 +77,52 @@ class Meili:
|
|||||||
json=documents,
|
json=documents,
|
||||||
))
|
))
|
||||||
|
|
||||||
def update_documents(self, documents: list[dict]) -> None:
|
def wait_for_task(self, task_uid: int, *, timeout_seconds: float = 120) -> dict:
|
||||||
|
deadline = time.monotonic() + timeout_seconds
|
||||||
|
while True:
|
||||||
|
task = self._check(
|
||||||
|
self.session.get(f"{self.url}/tasks/{task_uid}")
|
||||||
|
).json()
|
||||||
|
status = str(task.get("status") or "")
|
||||||
|
if status == "succeeded":
|
||||||
|
return task
|
||||||
|
if status in {"failed", "canceled"}:
|
||||||
|
error = task.get("error") or {}
|
||||||
|
message = error.get("message") if isinstance(error, dict) else error
|
||||||
|
raise RuntimeError(
|
||||||
|
f"Meilisearch task {task_uid} {status}: "
|
||||||
|
f"{message or task}"
|
||||||
|
)
|
||||||
|
if time.monotonic() >= deadline:
|
||||||
|
raise RuntimeError(
|
||||||
|
f"Timed out waiting for Meilisearch task {task_uid}"
|
||||||
|
)
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
def update_documents(
|
||||||
|
self,
|
||||||
|
documents: list[dict],
|
||||||
|
*,
|
||||||
|
wait: bool = False,
|
||||||
|
) -> None:
|
||||||
"""Partially update existing documents without replacing omitted fields."""
|
"""Partially update existing documents without replacing omitted fields."""
|
||||||
if not documents:
|
if not documents:
|
||||||
return
|
return
|
||||||
self.ensure_index()
|
self.ensure_index()
|
||||||
self._check(
|
response = self._check(
|
||||||
self.session.post(
|
self.session.post(
|
||||||
f"{self.url}/indexes/{self.index}/documents",
|
f"{self.url}/indexes/{self.index}/documents",
|
||||||
params={"primaryKey": "id"},
|
params={"primaryKey": "id"},
|
||||||
json=documents,
|
json=documents,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
if wait:
|
||||||
|
task_uid = response.json().get("taskUid")
|
||||||
|
if task_uid is None:
|
||||||
|
raise RuntimeError(
|
||||||
|
"Meilisearch update did not return a taskUid"
|
||||||
|
)
|
||||||
|
self.wait_for_task(int(task_uid))
|
||||||
|
|
||||||
def delete_jellyfin_libraries(self, library_ids: list[str]) -> None:
|
def delete_jellyfin_libraries(self, library_ids: list[str]) -> None:
|
||||||
if not library_ids:
|
if not library_ids:
|
||||||
|
|||||||
Reference in New Issue
Block a user