36 lines
923 B
Python
36 lines
923 B
Python
"""arq job queue connection (Redis-backed), shared by the API (enqueuing)
|
|||
|
|
and the worker process (consuming) — see app/workers/worker.py."""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from collections.abc import AsyncGenerator
|
||
|
|
|
||
|
|
from arq import ArqRedis, create_pool
|
||
|
|
from arq.connections import RedisSettings
|
||
|
|
|
||
|
|
from app.core.config import get_settings
|
||
|
|
|
||
|
|
_pool: ArqRedis | None = None
|
||
|
|
|
||
|
|
|
||
|
|
def redis_settings() -> RedisSettings:
|
||
|
|
return RedisSettings.from_dsn(get_settings().REDIS_URL)
|
||
|
|
|
||
|
|
|
||
|
|
async def get_arq_pool() -> ArqRedis:
|
||
|
|
global _pool
|
||
|
|
if _pool is None:
|
||
|
|
_pool = await create_pool(redis_settings())
|
||
|
|
return _pool
|
||
|
|
|
||
|
|
|
||
|
|
async def get_queue() -> AsyncGenerator[ArqRedis, None]:
|
||
|
|
yield await get_arq_pool()
|
||
|
|
|
||
|
|
|
||
|
|
async def close_arq_pool() -> None:
|
||
|
|
global _pool
|
||
|
|
if _pool is not None:
|
||
|
|
await _pool.aclose() # type: ignore[attr-defined] # present at runtime; stubs lag redis-py 5.x
|
||
|
|
_pool = None
|