Skip to content

Commit

Permalink
Merge pull request #856 from roflcoopter/feature/check-tier-cursor-fix
Browse files Browse the repository at this point in the history
use yield_per to avoid cursor closing in _check_tier
  • Loading branch information
roflcoopter authored Dec 12, 2024
2 parents 0e70128 + 48bcf50 commit 07679e5
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions viseron/components/storage/tier_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ def check_tier(self) -> None:
self._check_tier(self._storage.get_session)

def _check_tier(self, get_session: Callable[[], Session]) -> None:
file_ids = None
with get_session() as session:
file_ids = get_files_to_move(
session,
Expand All @@ -215,18 +214,25 @@ def _check_tier(self, get_session: Callable[[], Session]) -> None:
self._max_age,
)

if file_ids is not None:
for file in file_ids:
# Process in batches to avoid memory issues
for batch in file_ids.yield_per(100):
# Materialize current batch
files = [
{"path": file.path, "tier_path": file.tier_path} for file in batch
]
for file in files:
handle_file(
get_session,
self._storage,
self._camera.identifier,
self._tier,
self._next_tier,
file.path,
file.tier_path,
file["path"],
file["tier_path"],
self._logger,
)
# Commit batch
session.flush()
session.commit()

def _process_events(self) -> None:
Expand Down

0 comments on commit 07679e5

Please sign in to comment.