break out recording maintenance and cleanup into separate threads

This commit is contained in:
Jason Hunter 2021-07-11 15:34:48 -04:00 committed by Blake Blackshear
parent 5d940bcb86
commit 181a504a14
2 changed files with 33 additions and 9 deletions

View File

@ -23,7 +23,7 @@ from frigate.models import Event, Recordings
from frigate.mqtt import create_mqtt_client, MqttSocketRelay
from frigate.object_processing import TrackedObjectProcessor
from frigate.output import output_frames
from frigate.record import RecordingMaintainer
from frigate.record import RecordingCleanup, RecordingMaintainer
from frigate.stats import StatsEmitter, stats_init
from frigate.video import capture_camera, track_camera
from frigate.watchdog import FrigateWatchdog
@ -301,6 +301,10 @@ class FrigateApp:
self.recording_maintainer = RecordingMaintainer(self.config, self.stop_event)
self.recording_maintainer.start()
def start_recording_cleanup(self):
self.recording_cleanup = RecordingCleanup(self.config, self.stop_event)
self.recording_cleanup.start()
def start_stats_emitter(self):
self.stats_emitter = StatsEmitter(
self.config,
@ -346,6 +350,7 @@ class FrigateApp:
self.start_event_processor()
self.start_event_cleanup()
self.start_recording_maintainer()
self.start_recording_cleanup()
self.start_stats_emitter()
self.start_watchdog()
# self.zeroconf = broadcast_zeroconf(self.config.mqtt.client_id)
@ -372,6 +377,7 @@ class FrigateApp:
self.event_processor.join()
self.event_cleanup.join()
self.recording_maintainer.join()
self.recording_cleanup.join()
self.stats_emitter.join()
self.frigate_watchdog.join()
self.db.stop()

View File

@ -68,6 +68,7 @@ class RecordingMaintainer(threading.Thread):
continue
for f in recordings:
# Skip files currently in use
if f in files_in_use:
continue
@ -76,6 +77,11 @@ class RecordingMaintainer(threading.Thread):
camera, date = basename.rsplit("-", maxsplit=1)
start_time = datetime.datetime.strptime(date, "%Y%m%d%H%M%S")
# Just delete files if recordings are turned off
if not self.config.cameras[camera].record.enabled:
Path(cache_path).unlink(missing_ok=True)
continue
ffprobe_cmd = [
"ffprobe",
"-v",
@ -119,6 +125,21 @@ class RecordingMaintainer(threading.Thread):
duration=duration,
)
def run(self):
# Check for new files every 5 seconds
while not self.stop_event.wait(5):
self.move_files()
logger.info(f"Exiting recording maintenance...")
class RecordingCleanup(threading.Thread):
def __init__(self, config: FrigateConfig, stop_event):
threading.Thread.__init__(self)
self.name = "recording_cleanup"
self.config = config
self.stop_event = stop_event
def expire_recordings(self):
logger.debug("Start expire recordings (new).")
@ -234,17 +255,14 @@ class RecordingMaintainer(threading.Thread):
logger.debug("End expire files (legacy).")
def run(self):
# only expire events every 10 minutes, but check for new files every 5 seconds
for counter in itertools.cycle(range(120)):
if self.stop_event.wait(5):
logger.info(f"Exiting recording maintenance...")
# Expire recordings every minute, clean directories every 5 minutes.
for counter in itertools.cycle(range(5)):
if self.stop_event.wait(60):
logger.info(f"Exiting recording cleanup...")
break
if counter % 12 == 0:
self.expire_recordings()
self.expire_recordings()
if counter == 0:
self.expire_files()
remove_empty_directories(RECORD_DIR)
self.move_files()