avoid proactive messages with retain_days 0 and handle first pass

This commit is contained in:
Blake Blackshear 2021-11-17 07:44:58 -06:00
parent 543a8a1712
commit 7533f2a8ab

View File

@ -45,6 +45,7 @@ class RecordingMaintainer(threading.Thread):
self.name = "recording_maint" self.name = "recording_maint"
self.config = config self.config = config
self.stop_event = stop_event self.stop_event = stop_event
self.first_pass = True
def move_files(self): def move_files(self):
cache_files = [ cache_files = [
@ -87,19 +88,22 @@ class RecordingMaintainer(threading.Thread):
} }
) )
# delete all cached files past the most recent 2 # delete all cached files past the most recent 2, but not on the first check
for camera in grouped_recordings.keys(): if self.first_pass:
if len(grouped_recordings[camera]) > 2: self.first_pass = False
logger.warning( else:
"Proactively cleaning cache. Your recordings disk may be too slow." for camera in grouped_recordings.keys():
) if len(grouped_recordings[camera]) > 2:
sorted_recordings = sorted( logger.warning(
grouped_recordings[camera], key=lambda i: i["start_time"] "Proactively cleaning cache. Your recordings disk may be too slow."
) )
to_remove = sorted_recordings[:-2] sorted_recordings = sorted(
for f in to_remove: grouped_recordings[camera], key=lambda i: i["start_time"]
Path(f["cache_path"]).unlink(missing_ok=True) )
grouped_recordings[camera] = sorted_recordings[-2:] to_remove = sorted_recordings[:-2]
for f in to_remove:
Path(f["cache_path"]).unlink(missing_ok=True)
grouped_recordings[camera] = sorted_recordings[-2:]
for camera, recordings in grouped_recordings.items(): for camera, recordings in grouped_recordings.items():
# get all events with the end time after the start of the oldest cache file # get all events with the end time after the start of the oldest cache file
@ -158,7 +162,7 @@ class RecordingMaintainer(threading.Thread):
overlaps = False overlaps = False
for event in events: for event in events:
# if the event starts in the future, stop checking events # if the event starts in the future, stop checking events
# and let this recording segment expire # and remove this segment
if event.start_time > end_time.timestamp(): if event.start_time > end_time.timestamp():
overlaps = False overlaps = False
break break
@ -178,6 +182,8 @@ class RecordingMaintainer(threading.Thread):
duration, duration,
cache_path, cache_path,
) )
else:
Path(cache_path).unlink(missing_ok=True)
# else retain_days includes this segment # else retain_days includes this segment
else: else:
self.store_segment( self.store_segment(