forked from mtusi2026/mtusi2026lab4
Лабораторная 4: S3-хранилище и вложения
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
"""Remove abandoned upload intents after an hour; never remove ready objects."""
|
||||
import os
|
||||
import time
|
||||
import logging
|
||||
import psycopg
|
||||
from psycopg.rows import dict_row
|
||||
import storage
|
||||
|
||||
|
||||
def sweep():
|
||||
count = 0
|
||||
# Small transactions, no unbounded listing of the bucket into memory.
|
||||
for _ in range(100):
|
||||
with psycopg.connect(storage.secret('DATABASE_URL'), row_factory=dict_row,
|
||||
connect_timeout=3, options='-c statement_timeout=15000') as conn:
|
||||
row = conn.execute("""SELECT id,object_key FROM attachments
|
||||
WHERE status='pending' AND created_at < clock_timestamp()-interval '1 hour'
|
||||
ORDER BY created_at FOR UPDATE SKIP LOCKED LIMIT 1""").fetchone()
|
||||
if not row:
|
||||
return count
|
||||
storage.client.delete_object(Bucket=storage.BUCKET, Key=row['object_key'])
|
||||
conn.execute("DELETE FROM attachments WHERE id=%s AND status='pending'", (row['id'],))
|
||||
count += 1
|
||||
return count
|
||||
|
||||
if __name__ == '__main__':
|
||||
while True:
|
||||
try:
|
||||
print('Removed abandoned uploads:', sweep(), flush=True)
|
||||
except Exception:
|
||||
logging.error('Cleanup failed; will retry next cycle')
|
||||
if os.environ.get('CLEANUP_ONCE') == '1':
|
||||
break
|
||||
time.sleep(60)
|
||||
Reference in New Issue
Block a user