From 29de723267bd4b16684d8bdd582eb1accb044bfd Mon Sep 17 00:00:00 2001 From: Blake Blackshear Date: Tue, 24 Aug 2021 06:50:04 -0500 Subject: [PATCH] limit legacy expiration to files after the oldest recording in the db --- frigate/record.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/frigate/record.py b/frigate/record.py index c5150103d..e1aed08f1 100644 --- a/frigate/record.py +++ b/frigate/record.py @@ -245,23 +245,32 @@ class RecordingCleanup(threading.Thread): def expire_files(self): logger.debug("Start expire files (legacy).") - shortest_retention = self.config.record.retain_days default_expire = ( datetime.datetime.now().timestamp() - SECONDS_IN_DAY * self.config.record.retain_days ) delete_before = {} + for name, camera in self.config.cameras.items(): delete_before[name] = ( datetime.datetime.now().timestamp() - SECONDS_IN_DAY * camera.record.retain_days ) - if camera.record.retain_days < shortest_retention: - shortest_retention = camera.record.retain_days - logger.debug(f"Shortest retention: {shortest_retention}") + # find all the recordings older than the oldest recording in the db + oldest_recording = ( + Recordings.select().order_by(Recordings.start_time.desc()).get() + ) + + oldest_timestamp = ( + oldest_recording.start_time + if oldest_recording + else datetime.datetime.now().timestamp() + ) + + logger.debug(f"Oldest recording in the db: {oldest_timestamp}") process = sp.run( - ["find", RECORD_DIR, "-type", "f", "-mtime", f"+{shortest_retention}"], + ["find", RECORD_DIR, "-type", "f", "-newermt", f"@{oldest_timestamp}"], capture_output=True, text=True, ) @@ -269,9 +278,6 @@ class RecordingCleanup(threading.Thread): for f in files_to_check: p = Path(f) - # Ignore files that have a record in the recordings DB - if Recordings.select().where(Recordings.path == str(p)).count(): - continue if p.stat().st_mtime < delete_before.get(p.parent.name, default_expire): p.unlink(missing_ok=True)