very new files are not removed

this makes download_http more reliable when reusing a file that could otherwise have been removed by UnusedCleaner
This commit is contained in:
boyska 2022-01-03 00:53:18 +01:00
parent b35da0a8d0
commit c0cc90a2f3

View file

@ -7,6 +7,8 @@ This component will look for files to be removed. There are some assumptions:
import logging import logging
import os import os
from os.path import normpath from os.path import normpath
from pathlib import Path
import time
import mpd import mpd
@ -30,6 +32,10 @@ except ImportError:
class UnusedCleaner: 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): def __init__(self, conf):
self.conf = conf self.conf = conf
self.waiting_removal_files = set() self.waiting_removal_files = set()
@ -69,7 +75,19 @@ class UnusedCleaner:
for song in mpdc.playlistid() for song in mpdc.playlistid()
if song["file"].startswith("/") if song["file"].startswith("/")
} }
now = time.time()
for fpath in self.waiting_removal_files - files_in_playlist: 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! # we can remove it!
self.log.debug("removing unused: %s", fpath) self.log.debug("removing unused: %s", fpath)
self.waiting_removal_files.remove(fpath) self.waiting_removal_files.remove(fpath)