better cache handling

This commit is contained in:
Blake Blackshear 2021-11-17 08:57:57 -06:00
parent 7533f2a8ab
commit ba55b5a6db

View File

@ -88,22 +88,16 @@ class RecordingMaintainer(threading.Thread):
} }
) )
# delete all cached files past the most recent 2, but not on the first check # delete all cached files past the most recent 2
if self.first_pass: for camera in grouped_recordings.keys():
self.first_pass = False if len(grouped_recordings[camera]) > 2:
else: sorted_recordings = sorted(
for camera in grouped_recordings.keys(): grouped_recordings[camera], key=lambda i: i["start_time"]
if len(grouped_recordings[camera]) > 2: )
logger.warning( to_remove = sorted_recordings[:-2]
"Proactively cleaning cache. Your recordings disk may be too slow." for f in to_remove:
) Path(f["cache_path"]).unlink(missing_ok=True)
sorted_recordings = sorted( grouped_recordings[camera] = sorted_recordings[-2:]
grouped_recordings[camera], key=lambda i: i["start_time"]
)
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
@ -182,8 +176,6 @@ 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(
@ -236,7 +228,14 @@ class RecordingMaintainer(threading.Thread):
"Error occurred when attempting to maintain recording cache" "Error occurred when attempting to maintain recording cache"
) )
logger.error(e) logger.error(e)
wait_time = max(0, 5 - (datetime.datetime.now().timestamp() - run_start)) duration = datetime.datetime.now().timestamp() - run_start
wait_time = max(0, 5 - duration)
if duration > 10 and not self.first_pass:
logger.warning(
"Cache maintenance is taking longer than 10 seconds to clear. Your recordings disk may be too slow."
)
if self.first_pass:
self.first_pass = False
logger.info(f"Exiting recording maintenance...") logger.info(f"Exiting recording maintenance...")