You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
2.7 KiB
135 lines
2.7 KiB
import os
|
|
import time
|
|
import shutil
|
|
from datetime import datetime
|
|
|
|
from common.logger import system_logger
|
|
|
|
|
|
# ==========================
|
|
# 项目路径
|
|
# ==========================
|
|
|
|
BASE_DIR = os.path.dirname(
|
|
os.path.abspath(__file__)
|
|
)
|
|
|
|
# 需要清理的目录
|
|
CLEAN_DIRS = [
|
|
os.path.join(BASE_DIR, "uploads"),
|
|
os.path.join(BASE_DIR, "uploads", "temp")
|
|
]
|
|
|
|
|
|
# ==========================
|
|
# 清理函数
|
|
# ==========================
|
|
|
|
def clear_dir(path):
|
|
|
|
if not os.path.exists(path):
|
|
|
|
system_logger.error(
|
|
f"[Cleanup] path not exist: {path}"
|
|
)
|
|
return
|
|
|
|
deleted_count = 0
|
|
|
|
for filename in os.listdir(path):
|
|
|
|
file_path = os.path.join(path, filename)
|
|
|
|
try:
|
|
|
|
if os.path.isfile(file_path):
|
|
os.remove(file_path)
|
|
deleted_count += 1
|
|
|
|
elif os.path.isdir(file_path):
|
|
shutil.rmtree(file_path)
|
|
deleted_count += 1
|
|
|
|
except Exception as e:
|
|
|
|
system_logger.exception(
|
|
f"[Cleanup] delete failed: {file_path}"
|
|
)
|
|
|
|
system_logger.info(
|
|
f"[Cleanup] done path={path} deleted={deleted_count}"
|
|
)
|
|
|
|
|
|
# ==========================
|
|
# 总清理入口
|
|
# ==========================
|
|
|
|
def clear_all():
|
|
|
|
system_logger.info(
|
|
"[Cleanup] task started"
|
|
)
|
|
|
|
for path in CLEAN_DIRS:
|
|
clear_dir(path)
|
|
|
|
system_logger.info(
|
|
"[Cleanup] task finished"
|
|
)
|
|
|
|
|
|
# ==========================
|
|
# 定时调度
|
|
# ==========================
|
|
TTL_SECONDS = 3600 # 1小时
|
|
|
|
def scheduler():
|
|
|
|
system_logger.info("[Cleanup] TTL scheduler started")
|
|
|
|
while True:
|
|
|
|
now = time.time()
|
|
|
|
for path in CLEAN_DIRS:
|
|
|
|
if not os.path.exists(path):
|
|
continue
|
|
|
|
deleted = 0
|
|
|
|
for filename in os.listdir(path):
|
|
|
|
file_path = os.path.join(path, filename)
|
|
|
|
try:
|
|
if not os.path.isfile(file_path):
|
|
continue
|
|
|
|
# 文件创建时间
|
|
file_time = os.path.getmtime(file_path)
|
|
|
|
if now - file_time > TTL_SECONDS:
|
|
os.remove(file_path)
|
|
deleted += 1
|
|
|
|
except Exception as e:
|
|
system_logger.exception(f"[Cleanup] failed: {file_path}")
|
|
|
|
if deleted > 0:
|
|
system_logger.info(f"[Cleanup] path={path} deleted={deleted}")
|
|
|
|
time.sleep(300) # 每5分钟检查一次
|
|
|
|
# ==========================
|
|
# main
|
|
# ==========================
|
|
|
|
if __name__ == "__main__":
|
|
|
|
system_logger.info(
|
|
"[Cleanup] service started"
|
|
)
|
|
|
|
scheduler() |