Skip to content

Commit

Permalink
improve cleanup
Browse files Browse the repository at this point in the history
reduce risk of conflict errors and allow to force cleanup
  • Loading branch information
frisi committed Aug 18, 2021
1 parent 76fa41e commit 28075cc
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions plone/formwidget/namedfile/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@ def upload_map(self):
upload_map = annotations.setdefault(FILE_UPLOAD_MAP_KEY, OOBTree())
return upload_map

def cleanup(self):
def cleanup(self, force=False):
"""Remove obsolete temporary uploads.
To avoid conflict errors, files are deleted on every ~5th method call.
Use force to make sure all expired files are deleted
"""
upload_map = self.upload_map
expiration_limit = datetime.now() - timedelta(seconds=FILE_UPLOAD_EXPIRATION_TIME)
# Avoid conflict errors by deleting only every fifth time
delete = force or randint(0, 5) == 0
for key in list(upload_map.keys()):
dt = upload_map[key].get('dt', FALLBACK_DATE)
if dt < expiration_limit and randint(0, 5) == 0: # Avoid conflict errors by deleting only every fifth time # noqa
if dt < expiration_limit and delete:
# Delete expired files or files without timestamp
del upload_map[key]

0 comments on commit 28075cc

Please sign in to comment.