init
This commit is contained in:
commit
fe636432d0
154 files changed
+30381
No files matched your search
@@ -0,0 +1,82 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib
|
||||
import traceback
|
||||
from datetime import timedelta
|
||||
|
||||
from app.config import get_settings
|
||||
|
||||
|
||||
DIFY_SYNC_COOLDOWN_GATE = "dify-sync-cooldown"
|
||||
|
||||
|
||||
class InlineJobRef:
|
||||
def __init__(self, job_id: str) -> None:
|
||||
self.id = job_id
|
||||
|
||||
|
||||
class InlineQueue:
|
||||
def enqueue(self, task_name: str, *args, **kwargs) -> InlineJobRef:
|
||||
job_id = str(args[0]) if args else f"inline:{task_name}"
|
||||
module_name, function_name = task_name.rsplit(".", 1)
|
||||
task = getattr(importlib.import_module(module_name), function_name)
|
||||
try:
|
||||
task(*args, **kwargs)
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
return InlineJobRef(f"inline:{job_id}")
|
||||
|
||||
def enqueue_in(self, delay: timedelta, task_name: str, *args, **kwargs) -> InlineJobRef:
|
||||
if delay.total_seconds() <= 0:
|
||||
return self.enqueue(task_name, *args, **kwargs)
|
||||
job_id = str(args[0]) if args else f"inline:{task_name}"
|
||||
return InlineJobRef(f"inline-delayed:{job_id}")
|
||||
|
||||
|
||||
def get_redis_connection():
|
||||
settings = get_settings()
|
||||
if settings.task_execution_mode == "sync":
|
||||
return None
|
||||
|
||||
from redis import Redis
|
||||
|
||||
return Redis.from_url(settings.redis_url)
|
||||
|
||||
|
||||
def _gate_key(name: str) -> str:
|
||||
settings = get_settings()
|
||||
return f"{settings.rq_queue_name}:{name}"
|
||||
|
||||
|
||||
def set_delay_gate(name: str, ttl_seconds: int) -> None:
|
||||
redis = get_redis_connection()
|
||||
if redis is None or ttl_seconds <= 0:
|
||||
return
|
||||
try:
|
||||
redis.setex(_gate_key(name), ttl_seconds, "1")
|
||||
except Exception:
|
||||
return
|
||||
|
||||
|
||||
def get_delay_gate_ttl(name: str) -> int:
|
||||
redis = get_redis_connection()
|
||||
if redis is None:
|
||||
return 0
|
||||
try:
|
||||
ttl = redis.ttl(_gate_key(name))
|
||||
except Exception:
|
||||
return 0
|
||||
if ttl is None or ttl < 0:
|
||||
return 0
|
||||
return int(ttl)
|
||||
|
||||
|
||||
def get_queue():
|
||||
settings = get_settings()
|
||||
if settings.task_execution_mode == "sync":
|
||||
return InlineQueue()
|
||||
|
||||
from rq import Queue
|
||||
|
||||
redis = get_redis_connection()
|
||||
return Queue(name=settings.rq_queue_name, connection=redis, default_timeout=settings.rq_default_timeout)
|
||||
Reference in new issue
Block a user