From feb42181de768e5fc48a12642424e939a1a5fcf4 Mon Sep 17 00:00:00 2001 From: Blake Blackshear Date: Mon, 30 Nov 2020 21:08:47 -0600 Subject: [PATCH] cleanup empty directories --- frigate/app.py | 4 ++-- frigate/events.py | 6 ++++-- frigate/record.py | 17 +++++++++++++++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/frigate/app.py b/frigate/app.py index f922f9906..a74fa2b3d 100644 --- a/frigate/app.py +++ b/frigate/app.py @@ -116,11 +116,11 @@ class FrigateApp(): def start_event_processor(self): self.event_processor = EventProcessor(self.config, self.camera_metrics, self.event_queue, self.event_processed_queue, self.stop_event) self.event_processor.start() - + def start_event_cleanup(self): self.event_cleanup = EventCleanup(self.config, self.stop_event) self.event_cleanup.start() - + def start_recording_maintainer(self): self.recording_maintainer = RecordingMaintainer(self.config, self.stop_event) self.recording_maintainer.start() diff --git a/frigate/events.py b/frigate/events.py index eaa7dcd4c..44eeded1c 100644 --- a/frigate/events.py +++ b/frigate/events.py @@ -166,14 +166,16 @@ class EventProcessor(threading.Thread): # if save clips is not enabled for this camera, just continue if not save_clips_config.enabled: - self.event_processed_queue.put((event_data['id'], camera)) + if event_type == 'end': + self.event_processed_queue.put((event_data['id'], camera)) continue # if specific objects are listed for this camera, only save clips for them # TODO: default to all tracked objects rather than checking for None if save_clips_config.objects: if not event_data['label'] in save_clips_config.objects: - self.event_processed_queue.put((event_data['id'], camera)) + if event_type == 'end': + self.event_processed_queue.put((event_data['id'], camera)) continue if event_type == 'start': diff --git a/frigate/record.py b/frigate/record.py index 505dbd4dc..6dc5db480 100644 --- a/frigate/record.py +++ b/frigate/record.py @@ -17,6 +17,18 @@ logger = logging.getLogger(__name__) SECONDS_IN_DAY = 60 * 60 * 24 +def remove_empty_directories(directory): + # list all directories recursively and sort them by path, + # longest first + paths = sorted( + [x[0] for x in os.walk('/media/frigate/recordings/')], + key=lambda p: len(str(p)), + reverse=True, + ) + for path in paths: + if len(os.listdir(path)) == 0: + os.rmdir(path) + class RecordingMaintainer(threading.Thread): def __init__(self, config: FrigateConfig, stop_event): threading.Thread.__init__(self) @@ -103,9 +115,10 @@ class RecordingMaintainer(threading.Thread): # only expire events every 10 minutes, but check for new files every 10 seconds time.sleep(10) counter = counter + 1 - if counter < 60: + if counter > 60: self.expire_files() - counter = 0 + remove_empty_directories(self.record_dir) + counter = 0 self.move_files()