| 97 | |
| 98 | @app.task(ignore_result=True) |
| 99 | def cleanup_cache_directory(): |
| 100 | # Delete files and folder in the task_assets folder after 30 days |
| 101 | task_assets_cache = os.path.join(settings.MEDIA_CACHE, "task_assets") |
| 102 | time_limit = 60 * 60 * 24 * 30 |
| 103 | |
| 104 | if os.path.isdir(task_assets_cache): |
| 105 | for f in os.listdir(task_assets_cache): |
| 106 | now = time.time() |
| 107 | filepath = os.path.join(task_assets_cache, f) |
| 108 | modified = os.stat(filepath).st_mtime |
| 109 | if modified < now - time_limit: |
| 110 | if os.path.isfile(filepath): |
| 111 | os.remove(filepath) |
| 112 | else: |
| 113 | shutil.rmtree(filepath, ignore_errors=True) |
| 114 | |
| 115 | logger.info('Cleaned up: %s (%s)' % (filepath, modified)) |
| 116 | |
| 117 | # Based on https://stackoverflow.com/questions/22498038/improve-current-implementation-of-a-setinterval-python/22498708#22498708 |
| 118 | def setInterval(interval, func, *args): |