ImageProcessor_MainServer/app/del-tasks.py

30 lines
989 B
Python

import os
import time
import logging
from .celery_worker_for_main import celery_app
logger = logging.getLogger(__name__)
@celery_app.task
def delete_old_files(directory_path: str, max_age_seconds: int):
deleted_count = 0
current_time = time.time()
try:
for root, _, files in os.walk(directory_path):
for file in files:
file_path = os.path.join(root, file)
file_age = current_time - os.path.getmtime(file_path) # mtime 사용
if file_age > max_age_seconds:
os.remove(file_path)
deleted_count += 1
logger.info(f"삭제된 파일: {file_path}")
logger.info(f"{deleted_count}개 파일 삭제 완료")
return {"deleted_count": deleted_count}
except Exception as e:
logger.exception("파일 정리 작업 오류")
# 실패 시 Celery가 실패로 인식하게 하려면 예외를 다시 던짐
raise