Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

3.6 KB
Raw Blame
"""Retain annotation import worker keys while scoping job reads to their owner."""

from typing import Literal
from uuid import uuid4

from extensions.ext_redis import RedisClientWrapper
from services.annotation_import_service import AnnotationImportJob, AnnotationImportJobStore

_PENDING_JOB_TTL_SECONDS = 24 * 60 * 60


def annotation_import_job_owner_key(*, job_id: str) -> str:
    return f"app_annotation_batch_import_owner_{job_id}"


def _decode(value: bytes | str | None) -> str | None:
    return value.decode("utf-8") if isinstance(value, bytes) else value


class RedisAnnotationImportJobRepository(AnnotationImportJobStore):
    def __init__(self, *, redis: RedisClientWrapper) -> None:
        self._redis = redis

    def record_request(self, *, tenant_id: str, window_seconds: Literal[60, 3600], now_ms: int) -> int:
        window = "1min" if window_seconds == 60 else "1hour"
        key = f"annotation_import_rate_limit:{tenant_id}:{window}"
        self._redis.zadd(key, {f"{now_ms}:{uuid4()}": now_ms})
        self._redis.zremrangebyscore(key, 0, now_ms - window_seconds * 1000)
        count = self._redis.zcard(key)
        self._redis.expire(key, window_seconds * 2)
        return count

    def count_active(self, *, tenant_id: str, now_ms: int) -> int:
        key = f"annotation_import_active:{tenant_id}"
        self._redis.zremrangebyscore(key, 0, now_ms - 120000)
        return self._redis.zcard(key)

    def create(self, *, tenant_id: str, app_id: str, job_id: str, now_ms: int) -> None:
        # Bound abandoned jobs while allowing queued imports a day to start; workers shorten terminal retention.
        owner_created = self._redis.set(
            annotation_import_job_owner_key(job_id=job_id),
            f"{tenant_id}:{app_id}",
            nx=True,
            ex=_PENDING_JOB_TTL_SECONDS,
        )
        if not owner_created and not self._is_owner(tenant_id=tenant_id, app_id=app_id, job_id=job_id):
            raise RuntimeError(f"Annotation import job {job_id} already belongs to another app or tenant")
        active_jobs_key = f"annotation_import_active:{tenant_id}"
        self._redis.zadd(active_jobs_key, {job_id: now_ms})
        self._redis.expire(active_jobs_key, 7200)
        self._redis.set(f"app_annotation_batch_import_{job_id}", "waiting", nx=True, ex=_PENDING_JOB_TTL_SECONDS)

    def release(self, *, tenant_id: str, app_id: str, job_id: str) -> None:
        """Remove an unpublished job only when its recorded workspace and app still match."""
        if not self._is_owner(tenant_id=tenant_id, app_id=app_id, job_id=job_id):
            return
        self._redis.zrem(f"annotation_import_active:{tenant_id}", job_id)
        self._redis.delete(f"app_annotation_batch_import_{job_id}")
        self._redis.delete(f"app_annotation_batch_import_error_msg_{job_id}")
        self._redis.delete(annotation_import_job_owner_key(job_id=job_id))

    def get(self, *, tenant_id: str, app_id: str, job_id: str) -> AnnotationImportJob | None:
        if not self._is_owner(tenant_id=tenant_id, app_id=app_id, job_id=job_id):
            return None
        status = _decode(self._redis.get(f"app_annotation_batch_import_{job_id}"))
        if status is None:
            return None
        error = (
            _decode(self._redis.get(f"app_annotation_batch_import_error_msg_{job_id}")) if status == "error" else None
        )
        return AnnotationImportJob(job_id=job_id, job_status=status, error_msg=error or "")

    def _is_owner(self, *, tenant_id: str, app_id: str, job_id: str) -> bool:
        owner = _decode(self._redis.get(annotation_import_job_owner_key(job_id=job_id)))
        return owner == f"{tenant_id}:{app_id}"