From 68325756438f6df38fb88b6bdff0cf92d481c6e1 Mon Sep 17 00:00:00 2001 From: Blake Blackshear Date: Sun, 21 Nov 2021 07:55:35 -0600 Subject: [PATCH] cleanup missing files from database once per hour --- frigate/record.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/frigate/record.py b/frigate/record.py index b4561eb08..06e3108ef 100644 --- a/frigate/record.py +++ b/frigate/record.py @@ -413,6 +413,32 @@ class RecordingCleanup(threading.Thread): logger.debug("End expire files (legacy).") + def sync_recordings(self): + logger.debug("Start sync recordings.") + + # get all recordings in the db + recordings: Recordings = Recordings.select() + + # get all recordings files on disk + process = sp.run( + ["find", RECORD_DIR, "-type", "f"], + capture_output=True, + text=True, + ) + files_on_disk = process.stdout.splitlines() + + recordings_to_delete = [] + for recording in recordings.objects().iterator(): + if not recording.path in files_on_disk: + recordings_to_delete.append(recording.id) + + logger.debug( + f"Deleting {len(recordings_to_delete)} recordings with missing files" + ) + Recordings.delete().where(Recordings.id << recordings_to_delete).execute() + + logger.debug("End sync recordings.") + def run(self): # Expire recordings every minute, clean directories every hour. for counter in itertools.cycle(range(60)): @@ -426,3 +452,4 @@ class RecordingCleanup(threading.Thread): if counter == 0: self.expire_files() remove_empty_directories(RECORD_DIR) + self.sync_recordings()