Redis очередь: /upload → RPUSH → 200 сразу, воркер → DB + парсинг, v1.38
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
"""redis_client.py — Очередь загрузок через Redis."""
|
||||
|
||||
import os, json, redis
|
||||
|
||||
UPLOAD_QUEUE = "contracts:upload_queue"
|
||||
|
||||
_r = None
|
||||
|
||||
def _get():
|
||||
global _r
|
||||
if _r is None:
|
||||
_r = redis.Redis(
|
||||
host=os.getenv("REDIS_HOST", "redisk8s.f6303a9d-4ab1-4b2f-8aa8-08f933d02f82.svc.cluster.local"),
|
||||
port=int(os.getenv("REDIS_PORT", "6379")),
|
||||
password=os.getenv("REDIS_PASS", "aLITloRefJEiCPqUc2xB"),
|
||||
decode_responses=True,
|
||||
socket_connect_timeout=5,
|
||||
)
|
||||
return _r
|
||||
|
||||
|
||||
def push(task: dict):
|
||||
"""Добавить задание в очередь."""
|
||||
_get().rpush(UPLOAD_QUEUE, json.dumps(task, ensure_ascii=False))
|
||||
|
||||
|
||||
def pop(timeout=5):
|
||||
"""Взять задание из очереди (блокирующий)."""
|
||||
result = _get().blpop(UPLOAD_QUEUE, timeout=timeout)
|
||||
if result:
|
||||
return json.loads(result[1])
|
||||
return None
|
||||
Reference in New Issue
Block a user