diff options
| author | jfriedli | 2020-04-23 10:39:35 -0700 |
|---|---|---|
| committer | jfriedli | 2020-04-23 10:39:35 -0700 |
| commit | e1bac8b6a7fd857f38b7bcb678398c82baaa8fd5 (patch) | |
| tree | fa87e526289e455f2f17b86973d08eb6850e721f /matweb/file_removal_scheduler.py | |
| parent | d14988fa3fa97f549fb8eaf601cb2c687cdce143 (diff) | |
Refactoring
Diffstat (limited to 'matweb/file_removal_scheduler.py')
| -rw-r--r-- | matweb/file_removal_scheduler.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/matweb/file_removal_scheduler.py b/matweb/file_removal_scheduler.py new file mode 100644 index 0000000..2ce7912 --- /dev/null +++ b/matweb/file_removal_scheduler.py | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | import glob | ||
| 2 | import time | ||
| 3 | import sys | ||
| 4 | import os | ||
| 5 | import random | ||
| 6 | |||
| 7 | |||
| 8 | def run_file_removal_job(upload_folder_path): | ||
| 9 | if random.randint(0, 10) == 0: | ||
| 10 | for file in glob.glob(upload_folder_path + '/*'): | ||
| 11 | delete_file_when_too_old(file) | ||
| 12 | |||
| 13 | |||
| 14 | def delete_file_when_too_old(filepath): | ||
| 15 | file_mod_time = os.stat(filepath).st_mtime | ||
| 16 | |||
| 17 | # time in second since last modification of file | ||
| 18 | last_time = time.time() - file_mod_time | ||
| 19 | |||
| 20 | # if file is older than our configured max timeframe, delete it | ||
| 21 | if last_time > int(os.environ.get('MAT2_MAX_FILE_AGE_FOR_REMOVAL', 15 * 60)): | ||
| 22 | try: | ||
| 23 | os.remove(filepath) | ||
| 24 | except OSError: | ||
| 25 | print('Automatic File Removal failed on file: ' + str(filepath)) | ||
| 26 | sys.exit(1) | ||
