add scheduler to remove files after 30 minutes instead of after download

This commit is contained in:
Sébastien Thuret 2021-10-25 11:46:49 +02:00
parent 738dba7476
commit c5f47f0917
No known key found for this signature in database
GPG key ID: 4742E2D66933BB08
2 changed files with 38 additions and 2 deletions

View file

@ -0,0 +1,26 @@
import atexit
import os
import time
from datetime import datetime
from apscheduler.schedulers.background import BackgroundScheduler
def remove_translated_files(upload_dir: str):
now = time.mktime(datetime.now().timetuple())
for f in os.listdir(upload_dir):
f = os.path.join(upload_dir, f)
if os.path.isfile(f):
f_time = os.path.getmtime(f)
if (now - f_time) > 1800: # 30 minutes
os.remove(f)
def setup(upload_dir):
scheduler = BackgroundScheduler(daemon=True)
scheduler.add_job(remove_translated_files, "interval", minutes=30, kwargs={'upload_dir': upload_dir})
scheduler.start()
# Shut down the scheduler when exiting the app
atexit.register(lambda: scheduler.shutdown())