From 4ac53fe332df42c9b5f518f8b1c12bdb1c84344d Mon Sep 17 00:00:00 2001 From: vasyukova Date: Fri, 23 Oct 2026 13:13:02 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A4=D0=B8=D0=BA=D1=81:=20=D0=B8=D1=81=D0=BF?= =?UTF-8?q?=D1=80=D0=B0=D0=B2=D0=B8=D0=BB=D0=B0=20cleanup:=20staging-?= =?UTF-8?q?=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D1=8B=20=D0=B2=D0=BB=D0=BE?= =?UTF-8?q?=D0=B6=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=B2=20=D1=81=D1=82=D0=B0?= =?UTF-8?q?=D1=82=D1=83=D1=81=D0=B0=D1=85=20queued/processing=20=D0=B1?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D1=88=D0=B5=20=D0=BD=D0=B5=20=D1=83=D0=B4?= =?UTF-8?q?=D0=B0=D0=BB=D1=8F=D1=8E=D1=82=D1=81=D1=8F=20=D0=BF=D0=BE=20gra?= =?UTF-8?q?ce-=D0=BF=D0=B5=D1=80=D0=B8=D0=BE=D0=B4=D1=83,=20=D0=B0=20?= =?UTF-8?q?=D1=82=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=BE=D1=81=D0=B8=D1=80?= =?UTF-8?q?=D0=BE=D1=82=D0=B5=D0=B2=D1=88=D0=B8=D0=B5=20(=D0=BD=D0=B5?= =?UTF-8?q?=D1=82=20=D0=B0=D0=BA=D1=82=D0=B8=D0=B2=D0=BD=D0=BE=D0=B9=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D0=B8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/storage/s3_cleanup.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/app/storage/s3_cleanup.py b/src/app/storage/s3_cleanup.py index 61e3c2e4..3d0d4cb8 100644 --- a/src/app/storage/s3_cleanup.py +++ b/src/app/storage/s3_cleanup.py @@ -60,11 +60,21 @@ def _known_keys() -> set[str]: return keys +def _active_attachment_ids() -> set[str]: + """ID вложений в статусах queued/processing — их staging трогать нельзя.""" + with connection() as conn, conn.cursor() as cur: + cur.execute( + "SELECT id FROM attachments WHERE status IN ('queued', 'processing')" + ) + return {str(r[0]) for r in cur.fetchall()} + + def _run_once() -> dict: now = datetime.now(timezone.utc) grace_cutoff = now - timedelta(seconds=_grace()) known = _known_keys() + active_ids = _active_attachment_ids() orphan_deleted = 0 missing = 0 scanned = 0 @@ -93,8 +103,18 @@ def _run_once() -> dict: log.error("event=orphan_delete_failed key=%s type=%s", key, type(exc).__name__) - # staging/ — удаляем только старое (не in-flight). + # staging/ — удаляем только старое И не принадлежащее активной задаче. + # Ключ: staging///original for obj in s3.list_objects("staging/"): + key = obj["Key"] + parts = key.split("/") + # Ожидаем минимум 4 части: staging / chat_id / attachment_id / original + if len(parts) < 4 or parts[0] != "staging": + continue + attachment_id = parts[2] + if attachment_id in active_ids: + # Задача ещё в queued/processing — staging нужен worker'у. + continue lm = obj.get("LastModified") if lm is None: continue @@ -103,7 +123,7 @@ def _run_once() -> dict: if lm > grace_cutoff: continue try: - s3.delete_object(obj["Key"]) + s3.delete_object(key) staging_deleted += 1 except Exception: pass