Skip to content

Commit

Permalink
fix: bad dep
Browse files Browse the repository at this point in the history
  • Loading branch information
sverben committed Apr 17, 2024
1 parent 0cc8be2 commit be8b59f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
2 changes: 1 addition & 1 deletion api/app/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ uvicorn~=0.29.0
ffmpeg-python~=0.2.0
fastapi~=0.110.0
SQLAlchemy~=2.0.29
pydantic~=2.7.0
cryptography~=42.0.5
pydantic-settings~=2.2.1
PyJWT~=2.8.0
Expand All @@ -12,4 +13,3 @@ baize~=0.20.8
alembic~=1.13.1
face-recognition~=1.3.0
weaviate-client~=4.5.5
fastapi-utils~=0.2.1
87 changes: 86 additions & 1 deletion api/app/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import asyncio
import logging
from asyncio import ensure_future
from enum import Enum
from functools import wraps
from hashlib import md5
from io import BytesIO
from typing import List
from traceback import format_exception
from typing import Any, Callable, Coroutine, Optional, Union, List
from urllib.request import urlopen
from uuid import UUID

Expand All @@ -10,6 +15,7 @@
from PIL import Image
from numpy import asarray, ndarray
from sqlalchemy.orm import Session
from starlette.concurrency import run_in_threadpool
from weaviate import connect_to_local

from app.conf import settings
Expand All @@ -20,6 +26,85 @@
known_faces = client.collections.get('known_faces')


NoArgsNoReturnFuncT = Callable[[], None]
NoArgsNoReturnAsyncFuncT = Callable[[], Coroutine[Any, Any, None]]
NoArgsNoReturnDecorator = Callable[
[Union[NoArgsNoReturnFuncT, NoArgsNoReturnAsyncFuncT]], NoArgsNoReturnAsyncFuncT
]


def repeat_every(
*,
seconds: float,
wait_first: bool = False,
logger: Optional[logging.Logger] = None,
raise_exceptions: bool = False,
max_repetitions: Optional[int] = None,
) -> NoArgsNoReturnDecorator:
"""
This function returns a decorator that modifies a function so it is periodically re-executed after its first call.
The function it decorates should accept no arguments and return nothing. If necessary, this can be accomplished
by using `functools.partial` or otherwise wrapping the target function prior to decoration.
Parameters
----------
seconds: float
The number of seconds to wait between repeated calls
wait_first: bool (default False)
If True, the function will wait for a single period before the first call
logger: Optional[logging.Logger] (default None)
The logger to use to log any exceptions raised by calls to the decorated function.
If not provided, exceptions will not be logged by this function (though they may be handled by the event loop).
raise_exceptions: bool (default False)
If True, errors raised by the decorated function will be raised to the event loop's exception handler.
Note that if an error is raised, the repeated execution will stop.
Otherwise, exceptions are just logged and the execution continues to repeat.
See https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.set_exception_handler for more info.
max_repetitions: Optional[int] (default None)
The maximum number of times to call the repeated function. If `None`, the function is repeated forever.
"""

def decorator(
func: Union[NoArgsNoReturnAsyncFuncT, NoArgsNoReturnFuncT]
) -> NoArgsNoReturnAsyncFuncT:
"""
Converts the decorated function into a repeated, periodically-called version of itself.
"""
is_coroutine = asyncio.iscoroutinefunction(func)

@wraps(func)
async def wrapped() -> None:
repetitions = 0

async def loop() -> None:
nonlocal repetitions
if wait_first:
await asyncio.sleep(seconds)
while max_repetitions is None or repetitions < max_repetitions:
try:
if is_coroutine:
await func() # type: ignore
else:
await run_in_threadpool(func)
repetitions += 1
except Exception as exc: # pylint: disable=broad-except
if logger is not None:
formatted_exception = "".join(
format_exception(type(exc), exc, exc.__traceback__)
)
logger.error(formatted_exception)
if raise_exceptions:
raise exc
await asyncio.sleep(seconds)

ensure_future(loop())

return wrapped

return decorator


class FaceAction(Enum):
NONE = 0
CREATE = 1
Expand Down

0 comments on commit be8b59f

Please sign in to comment.