mirror of
https://github.com/LibreTranslate/LibreTranslate.git
synced 2024-12-13 10:46:38 +00:00
27 lines
765 B
Python
27 lines
765 B
Python
|
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())
|