mirror of
https://github.com/open-webui/open-webui.git
synced 2026-12-01 01:12:43 -05:00
refac
This commit is contained in:
1 parent
e3651761a4
commit
ca3b5c5156
3 files changed
+26
-11
No files matched your search
@@ -26,7 +26,12 @@ from open_webui.models.users import UserModel
|
||||
from open_webui.utils.access_control import filter_allowed_access_grants, has_permission, validate_access_grants
|
||||
from open_webui.utils.auth import get_verified_user
|
||||
from open_webui.utils.calendar import expand_recurring_event
|
||||
from open_webui.utils.recurrence import next_run_ns, schedule_start_ns
|
||||
from open_webui.utils.recurrence import (
|
||||
RecurrenceEvaluationTimeout,
|
||||
evaluate_recurrence,
|
||||
next_run_ns,
|
||||
schedule_start_ns,
|
||||
)
|
||||
from pydantic import BaseModel
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
@@ -184,7 +189,13 @@ async def get_events(
|
||||
for event in events:
|
||||
event_dict = event.model_dump()
|
||||
if event_dict.get('rrule'):
|
||||
instances = expand_recurring_event(event_dict, start_ns, end_ns, tz=user.timezone)
|
||||
try:
|
||||
instances = await evaluate_recurrence(
|
||||
expand_recurring_event, event_dict, start_ns, end_ns, user.timezone
|
||||
)
|
||||
except RecurrenceEvaluationTimeout:
|
||||
log.warning(f'Timed out expanding RRULE for event {event.id}: {event.rrule}')
|
||||
instances = [event_dict]
|
||||
for inst in instances:
|
||||
expanded.append(CalendarEventUserResponse(**{**inst, 'user': event.user}))
|
||||
else:
|
||||
@@ -236,7 +247,9 @@ async def get_events(
|
||||
# Only expand into the future — past runs are handled below
|
||||
now_ns = int(time.time_ns())
|
||||
rrule_start = max(start_ns, now_ns)
|
||||
instances = expand_recurring_event(virtual, rrule_start, end_ns, tz=user.timezone)
|
||||
instances = await evaluate_recurrence(
|
||||
expand_recurring_event, virtual, rrule_start, end_ns, user.timezone
|
||||
)
|
||||
for inst in instances:
|
||||
expanded.append(CalendarEventUserResponse(**inst))
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import logging
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from dateutil.rrule import rrulestr
|
||||
from open_webui.utils.automations import _resolve_tz
|
||||
from open_webui.utils.recurrence import _resolve_tz
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from collections.abc import Callable
|
||||
from datetime import datetime, timedelta
|
||||
from functools import partial
|
||||
from typing import Optional
|
||||
@@ -100,17 +101,18 @@ def _next_occurrences(s: str, now: datetime, n: int) -> list[datetime]:
|
||||
return occurrences
|
||||
|
||||
|
||||
async def _get_next_occurrences(s: str, now: datetime, n: int) -> list[datetime]:
|
||||
async def evaluate_recurrence(func: Callable, *args):
|
||||
"""Run *func* in a worker process, raising RecurrenceEvaluationTimeout past the time limit."""
|
||||
# A result-count or date limit cannot bound work before the first match.
|
||||
try:
|
||||
with fail_after(RRULE_TIMEOUT_SECONDS):
|
||||
return await to_process.run_sync(_next_occurrences, s, now, n, cancellable=True)
|
||||
return await to_process.run_sync(func, *args, cancellable=True)
|
||||
except TimeoutError as e:
|
||||
raise RecurrenceEvaluationTimeout('Schedule took too long to evaluate; simplify its recurrence rule.') from e
|
||||
except NotImplementedError:
|
||||
# Windows' SelectorEventLoop (required by psycopg) cannot spawn subprocesses.
|
||||
run_on_proactor_loop = partial(
|
||||
anyio.run, _get_next_occurrences, s, now, n, backend_options={'loop_factory': asyncio.ProactorEventLoop}
|
||||
anyio.run, evaluate_recurrence, func, *args, backend_options={'loop_factory': asyncio.ProactorEventLoop}
|
||||
)
|
||||
return await to_thread.run_sync(run_on_proactor_loop)
|
||||
|
||||
@@ -128,7 +130,7 @@ async def validate_rrule(s: str, tz: str = None) -> None:
|
||||
zi = _resolve_tz(tz)
|
||||
now = datetime.now(zi).replace(tzinfo=None) if zi else datetime.now()
|
||||
try:
|
||||
occurrences = await _get_next_occurrences(s, now, 1)
|
||||
occurrences = await evaluate_recurrence(_next_occurrences, s, now, 1)
|
||||
except RecurrenceEvaluationTimeout:
|
||||
raise
|
||||
except Exception as e:
|
||||
@@ -142,7 +144,7 @@ async def next_run_ns(s: str, tz: str = None) -> Optional[int]:
|
||||
zi = _resolve_tz(tz)
|
||||
now = datetime.now(zi) if zi else datetime.now()
|
||||
now_naive = now.replace(tzinfo=None)
|
||||
occurrences = await _get_next_occurrences(s, now_naive, 1)
|
||||
occurrences = await evaluate_recurrence(_next_occurrences, s, now_naive, 1)
|
||||
if not occurrences:
|
||||
return None
|
||||
dt = occurrences[0]
|
||||
@@ -172,7 +174,7 @@ async def next_n_runs_ns(s: str, n: int = 5, tz: str = None) -> list[int]:
|
||||
zi = _resolve_tz(tz)
|
||||
result = []
|
||||
now = datetime.now(zi).replace(tzinfo=None) if zi else datetime.now()
|
||||
for dt in await _get_next_occurrences(s, now, n):
|
||||
for dt in await evaluate_recurrence(_next_occurrences, s, now, n):
|
||||
if zi:
|
||||
dt_tz = dt.replace(tzinfo=zi)
|
||||
result.append(int(dt_tz.timestamp() * 1_000_000_000))
|
||||
@@ -189,7 +191,7 @@ async def rrule_interval_seconds(s: str) -> Optional[int]:
|
||||
"""
|
||||
s = '\n'.join(part for part in s.split() if not part.upper().startswith('DTSTART')) or s
|
||||
now = datetime.now()
|
||||
occurrences = await _get_next_occurrences(s, now, 2)
|
||||
occurrences = await evaluate_recurrence(_next_occurrences, s, now, 2)
|
||||
if len(occurrences) < 2:
|
||||
return None
|
||||
return int((occurrences[1] - occurrences[0]).total_seconds())
|
||||
Reference in new issue
Block a user