File size: 1,082 Bytes
1f47729
 
 
 
 
 
 
 
 
 
 
 
 
311bc41
1f47729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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)