|
@@ -245,23 +245,32 @@ class RecordingCleanup(threading.Thread):
|
|
|
def expire_files(self):
|
|
|
logger.debug("Start expire files (legacy).")
|
|
|
|
|
|
- shortest_retention = self.config.record.retain_days
|
|
|
default_expire = (
|
|
|
datetime.datetime.now().timestamp()
|
|
|
- SECONDS_IN_DAY * self.config.record.retain_days
|
|
|
)
|
|
|
delete_before = {}
|
|
|
+
|
|
|
for name, camera in self.config.cameras.items():
|
|
|
delete_before[name] = (
|
|
|
datetime.datetime.now().timestamp()
|
|
|
- SECONDS_IN_DAY * camera.record.retain_days
|
|
|
)
|
|
|
- if camera.record.retain_days < shortest_retention:
|
|
|
- shortest_retention = camera.record.retain_days
|
|
|
|
|
|
- logger.debug(f"Shortest retention: {shortest_retention}")
|
|
|
+ # find all the recordings older than the oldest recording in the db
|
|
|
+ oldest_recording = (
|
|
|
+ Recordings.select().order_by(Recordings.start_time.desc()).get()
|
|
|
+ )
|
|
|
+
|
|
|
+ oldest_timestamp = (
|
|
|
+ oldest_recording.start_time
|
|
|
+ if oldest_recording
|
|
|
+ else datetime.datetime.now().timestamp()
|
|
|
+ )
|
|
|
+
|
|
|
+ logger.debug(f"Oldest recording in the db: {oldest_timestamp}")
|
|
|
process = sp.run(
|
|
|
- ["find", RECORD_DIR, "-type", "f", "-mtime", f"+{shortest_retention}"],
|
|
|
+ ["find", RECORD_DIR, "-type", "f", "-newermt", f"@{oldest_timestamp}"],
|
|
|
capture_output=True,
|
|
|
text=True,
|
|
|
)
|
|
@@ -269,9 +278,6 @@ class RecordingCleanup(threading.Thread):
|
|
|
|
|
|
for f in files_to_check:
|
|
|
p = Path(f)
|
|
|
- # Ignore files that have a record in the recordings DB
|
|
|
- if Recordings.select().where(Recordings.path == str(p)).count():
|
|
|
- continue
|
|
|
if p.stat().st_mtime < delete_before.get(p.parent.name, default_expire):
|
|
|
p.unlink(missing_ok=True)
|
|
|
|