error handling for the recording maintainer

This commit is contained in:
Blake Blackshear 2021-11-09 07:05:21 -06:00
parent 273076e7f4
commit 34bc6a6457

View File

@ -185,25 +185,38 @@ class RecordingMaintainer(threading.Thread):
file_path = os.path.join(directory, file_name) file_path = os.path.join(directory, file_name)
# copy then delete is required when recordings are stored on some network drives # copy then delete is required when recordings are stored on some network drives
shutil.copyfile(cache_path, file_path) try:
os.remove(cache_path) shutil.copyfile(cache_path, file_path)
os.remove(cache_path)
rand_id = "".join(random.choices(string.ascii_lowercase + string.digits, k=6)) rand_id = "".join(
Recordings.create( random.choices(string.ascii_lowercase + string.digits, k=6)
id=f"{start_time.timestamp()}-{rand_id}", )
camera=camera, Recordings.create(
path=file_path, id=f"{start_time.timestamp()}-{rand_id}",
start_time=start_time.timestamp(), camera=camera,
end_time=end_time.timestamp(), path=file_path,
duration=duration, start_time=start_time.timestamp(),
) end_time=end_time.timestamp(),
duration=duration,
)
except Exception as e:
logger.error(f"Unable to store recording segment {cache_path}")
Path(cache_path).unlink(missing_ok=True)
logger.error(e)
def run(self): def run(self):
# Check for new files every 5 seconds # Check for new files every 5 seconds
wait_time = 5 wait_time = 5
while not self.stop_event.wait(wait_time): while not self.stop_event.wait(wait_time):
run_start = datetime.datetime.now().timestamp() run_start = datetime.datetime.now().timestamp()
self.move_files() try:
self.move_files()
except Exception as e:
logger.error(
"Error occurred when attempting to maintain recording cache"
)
logger.error(e)
wait_time = max(0, 5 - (datetime.datetime.now().timestamp() - run_start)) wait_time = max(0, 5 - (datetime.datetime.now().timestamp() - run_start))
logger.info(f"Exiting recording maintenance...") logger.info(f"Exiting recording maintenance...")