From 26424488a5c72ab0cd404bb3c0f2c4467f7b75b8 Mon Sep 17 00:00:00 2001 From: Blake Blackshear Date: Mon, 23 Aug 2021 07:21:27 -0500 Subject: [PATCH] use find to reduce CPU usage for legacy expiration --- frigate/record.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/frigate/record.py b/frigate/record.py index 5e51b0ddc..c5150103d 100644 --- a/frigate/record.py +++ b/frigate/record.py @@ -244,6 +244,8 @@ 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 @@ -254,8 +256,19 @@ class RecordingCleanup(threading.Thread): datetime.datetime.now().timestamp() - SECONDS_IN_DAY * camera.record.retain_days ) + if camera.record.retain_days < shortest_retention: + shortest_retention = camera.record.retain_days - for p in Path("/media/frigate/recordings").rglob("*.mp4"): + logger.debug(f"Shortest retention: {shortest_retention}") + process = sp.run( + ["find", RECORD_DIR, "-type", "f", "-mtime", f"+{shortest_retention}"], + capture_output=True, + text=True, + ) + files_to_check = process.stdout.splitlines() + + 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 @@ -265,8 +278,8 @@ class RecordingCleanup(threading.Thread): logger.debug("End expire files (legacy).") def run(self): - # Expire recordings every minute, clean directories every 5 minutes. - for counter in itertools.cycle(range(5)): + # Expire recordings every minute, clean directories every hour. + for counter in itertools.cycle(range(60)): if self.stop_event.wait(60): logger.info(f"Exiting recording cleanup...") break