From 7104b2107db592756f4090ba772869eb86d6a44f Mon Sep 17 00:00:00 2001 From: jfriedli Date: Fri, 27 Mar 2020 10:59:16 -0700 Subject: file removal background job --- file_removal_scheduler.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 file_removal_scheduler.py (limited to 'file_removal_scheduler.py') diff --git a/file_removal_scheduler.py b/file_removal_scheduler.py new file mode 100644 index 0000000..2ce7912 --- /dev/null +++ b/file_removal_scheduler.py @@ -0,0 +1,26 @@ +import glob +import time +import sys +import os +import random + + +def run_file_removal_job(upload_folder_path): + if random.randint(0, 10) == 0: + for file in glob.glob(upload_folder_path + '/*'): + delete_file_when_too_old(file) + + +def delete_file_when_too_old(filepath): + file_mod_time = os.stat(filepath).st_mtime + + # time in second since last modification of file + last_time = time.time() - file_mod_time + + # if file is older than our configured max timeframe, delete it + if last_time > int(os.environ.get('MAT2_MAX_FILE_AGE_FOR_REMOVAL', 15 * 60)): + try: + os.remove(filepath) + except OSError: + print('Automatic File Removal failed on file: ' + str(filepath)) + sys.exit(1) -- cgit v1.3