Skip to content

Commit

Permalink
Ensure storage cleanup doesn't fail (blakeblackshear#8531)
Browse files Browse the repository at this point in the history
  • Loading branch information
NickM-27 committed Nov 7, 2023
1 parent 0b828ef commit ca84732
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
8 changes: 2 additions & 6 deletions frigate/record/cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from frigate.const import CACHE_DIR, RECORD_DIR
from frigate.models import Event, Recordings
from frigate.record.util import remove_empty_directories, sync_recordings
from frigate.util.builtin import get_tomorrow_at_time
from frigate.util.builtin import clear_and_unlink, get_tomorrow_at_time

logger = logging.getLogger(__name__)

Expand All @@ -31,11 +31,7 @@ def clean_tmp_clips(self) -> None:
logger.debug(f"Checking tmp clip {p}.")
if p.stat().st_mtime < (datetime.datetime.now().timestamp() - 60 * 1):
logger.debug("Deleting tmp clip.")

# empty contents of file before unlinking https://github.com/blakeblackshear/frigate/issues/4769
with open(p, "w"):
pass
p.unlink(missing_ok=True)
clear_and_unlink(p)

def expire_recordings(self) -> None:
"""Delete recordings based on retention config."""
Expand Down
5 changes: 3 additions & 2 deletions frigate/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from frigate.config import FrigateConfig
from frigate.const import RECORD_DIR
from frigate.models import Event, Recordings
from frigate.util.builtin import clear_and_unlink

logger = logging.getLogger(__name__)
bandwidth_equation = Recordings.segment_size / (
Expand Down Expand Up @@ -160,7 +161,7 @@ def reduce_storage_consumption(self) -> None:
# Delete recordings not retained indefinitely
if not keep:
try:
Path(recording.path).unlink(missing_ok=False)
clear_and_unlink(Path(recording.path), missing_ok=False)
deleted_recordings.add(recording.id)
deleted_segments_size += recording.segment_size
except FileNotFoundError:
Expand Down Expand Up @@ -188,7 +189,7 @@ def reduce_storage_consumption(self) -> None:
break

try:
Path(recording.path).unlink(missing_ok=False)
clear_and_unlink(Path(recording.path), missing_ok=False)
deleted_segments_size += recording.segment_size
deleted_recordings.add(recording.id)
except FileNotFoundError:
Expand Down
13 changes: 13 additions & 0 deletions frigate/util/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import urllib.parse
from collections import Counter
from collections.abc import Mapping
from pathlib import Path
from typing import Any, Tuple

import numpy as np
Expand Down Expand Up @@ -269,3 +270,15 @@ def get_tomorrow_at_time(hour: int) -> datetime.datetime:
return tomorrow.replace(hour=hour, minute=0, second=0).astimezone(
datetime.timezone.utc
)


def clear_and_unlink(file: Path, missing_ok: bool = True) -> None:
"""clear file then unlink to avoid space retained by file descriptors."""
if not missing_ok and not file.exists():
raise FileNotFoundError()

# empty contents of file before unlinking https://github.com/blakeblackshear/frigate/issues/4769
with open(file, "w"):
pass

file.unlink(missing_ok=missing_ok)

0 comments on commit ca84732

Please sign in to comment.