Truncate write ahead log if it gets too big (#10866)

* Truncate write ahead log if it gets too big

* formatting

* Reduce size
This commit is contained in:
Nicolas Mowen 2024-04-07 14:37:12 -06:00 committed by GitHub
parent 12e6e43d6c
commit f6ff1c84b8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 1 deletions

View File

@ -39,6 +39,10 @@ AUDIO_MAX_BIT_RANGE = 32768.0
AUDIO_SAMPLE_RATE = 16000
AUDIO_MIN_CONFIDENCE = 0.5
# DB Consts
MAX_WAL_SIZE = 10 # MB
# Ffmpeg Presets
FFMPEG_HWACCEL_NVIDIA = "preset-nvidia"

View File

@ -3,12 +3,15 @@
import datetime
import itertools
import logging
import os
import threading
from multiprocessing.synchronize import Event as MpEvent
from pathlib import Path
from playhouse.sqlite_ext import SqliteExtDatabase
from frigate.config import CameraConfig, FrigateConfig, RetainModeEnum
from frigate.const import CACHE_DIR, RECORD_DIR
from frigate.const import CACHE_DIR, MAX_WAL_SIZE, RECORD_DIR
from frigate.models import Event, Previews, Recordings, ReviewSegment
from frigate.record.util import remove_empty_directories, sync_recordings
from frigate.util.builtin import clear_and_unlink, get_tomorrow_at_time
@ -33,6 +36,23 @@ class RecordingCleanup(threading.Thread):
logger.debug("Deleting tmp clip.")
clear_and_unlink(p)
def truncate_wal(self) -> None:
"""check if the WAL needs to be manually truncated."""
# by default the WAL should be check-pointed automatically
# however, high levels of activity can prevent an opportunity
# for the checkpoint to be finished which means the WAL will grow
# without bound
# with auto checkpoint most users should never hit this
if (
os.stat(f"{self.config.database.path}-wal").st_size / (1024 * 1024)
) > MAX_WAL_SIZE:
db = SqliteExtDatabase(self.config.database.path)
db.execute_sql("PRAGMA wal_checkpoint(TRUNCATE);")
db.close()
def expire_existing_camera_recordings(
self, expire_date: float, config: CameraConfig, events: Event
) -> None:
@ -328,3 +348,4 @@ class RecordingCleanup(threading.Thread):
if counter == 0:
self.expire_recordings()
remove_empty_directories(RECORD_DIR)
self.truncate_wal()