diff --git a/larigira/unused.py b/larigira/unused.py index 52f190f..e686031 100644 --- a/larigira/unused.py +++ b/larigira/unused.py @@ -7,6 +7,8 @@ This component will look for files to be removed. There are some assumptions: import logging import os from os.path import normpath +from pathlib import Path +import time import mpd @@ -30,6 +32,10 @@ except ImportError: class UnusedCleaner: + # ONLY_DELETE_OLDER_THAN is expressed in seconds. + # It configures the maximum age a file can have before being removed. + # Set it to "None" if you want to disable this feature. + ONLY_DELETE_OLDER_THAN = 30 def __init__(self, conf): self.conf = conf self.waiting_removal_files = set() @@ -69,7 +75,19 @@ class UnusedCleaner: for song in mpdc.playlistid() if song["file"].startswith("/") } + now = time.time() for fpath in self.waiting_removal_files - files_in_playlist: + + # audio files are sometimes reused, as in download_http. To avoid + # referencing a file that UnusedCleaner is going to remove, users + # are invited to touch the file, so that UnusedCleaner doesn't + # consider it for removal. While this doesn't conceptually solve + # the race condition, it should now be extremely rare. + + if ONLY_DELETE_OLDER_THAN is not None: + mtime = Path(fpath).stat().st_mtime + if now - mtime < ONLY_DELETE_OLDER_THAN: + continue # we can remove it! self.log.debug("removing unused: %s", fpath) self.waiting_removal_files.remove(fpath)