Skip to content

Commit

Permalink
fix: ignore temp exceptions during task polling (#127)
Browse files Browse the repository at this point in the history
fixes #123

---------

Signed-off-by: Anupam Kumar <[email protected]>
Co-authored-by: Marcel Klehr <[email protected]>
  • Loading branch information
kyteinsky and marcelklehr authored Jan 8, 2025
1 parent 4c867b1 commit cf1805f
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions context_chat_backend/models/nc_texttotext.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import time
from typing import Any

import httpx
from langchain_core.callbacks.manager import CallbackManagerForLLMRun
from langchain_core.language_models.llms import LLM
from nc_py_api import NextcloudApp
from nc_py_api import NextcloudApp, NextcloudException
from pydantic import BaseModel, ValidationError

from .types import LlmException
Expand Down Expand Up @@ -75,16 +76,42 @@ def _call(

try:
task = Response.model_validate(response).task
print(task)
print("Initial task schedule response:", task)

i = 0
# wait for 30 minutes
while task.status != "STATUS_SUCCESSFUL" and task.status != "STATUS_FAILED" and i < 60 * 6:
time.sleep(5)
i += 1
response = nc.ocs("GET", f"/ocs/v1.php/taskprocessing/task/{task.id}")
if i < 60 * 3:
time.sleep(5)
i += 1
else:
# pool every 10 secs in the second half
time.sleep(10)
i += 2

try:
response = nc.ocs("GET", f"/ocs/v1.php/taskprocessing/task/{task.id}")
except (
httpx.RemoteProtocolError,
httpx.ReadError,
httpx.LocalProtocolError,
httpx.PoolTimeout,
) as e:
print("Ignored error during task polling:", e)
time.sleep(5)
i += 1
continue
except NextcloudException as e:
if e.status_code == 429:
print("Rate limited during task polling, waiting 10s more")
time.sleep(10)
i += 2
continue
print('NextcloudException:', e)
raise LlmException("Failed to poll Nextcloud TaskProcessing task") from e

task = Response.model_validate(response).task
print(task)
print(f"Task poll ({i * 5}s) response: {task}")
except ValidationError as e:
raise LlmException("Failed to parse Nextcloud TaskProcessing task result") from e

Expand Down

0 comments on commit cf1805f

Please sign in to comment.