spik-backend / core /utils.py
Nam Fam
update files
311bc41
import os
import glob
import time
import asyncio
from pathlib import Path
from app.config import Config
async def cleanup_old_files():
"""Background task to clean up old audio files"""
while True:
try:
now = time.time()
max_age_seconds = Config.MAX_AUDIO_AGE_HOURS * 3600
pattern = str(Config.OUTPUT_DIR / "tts_*.mp3")
for filepath in glob.glob(pattern):
if not os.path.isfile(filepath):
continue
file_mtime = os.path.getmtime(filepath)
if now - file_mtime > max_age_seconds:
try:
os.remove(filepath)
print(f"Removed old file: {filepath}")
except Exception as e:
print(f"Error removing {filepath}: {e}")
except Exception as e:
print(f"Error in cleanup_old_files: {e}")
# Run every hour
await asyncio.sleep(3600)