update 1. GTP-6 Astra, оптимизация кода и фикс

This commit is contained in:
2026-09-26 21:19:09 +03:00
parent 70fce14772
commit 22e10b53b7
31 changed files with 2211 additions and 162 deletions

View File

@@ -2,6 +2,7 @@ from __future__ import annotations
import os
from contextlib import contextmanager
from contextvars import ContextVar
from datetime import datetime
from pathlib import Path
from threading import RLock
@@ -49,6 +50,10 @@ from .models import (
)
# Scoped to a request/task and this database object, never shared across threads.
_atomic_session: ContextVar[tuple[object, Session] | None] = ContextVar("hockey_atomic_session", default=None)
class HockeyDatabase:
"""Remote PostgreSQL database layer with additive schema repair.
@@ -375,6 +380,12 @@ class HockeyDatabase:
@contextmanager
def session(self) -> Iterator[Session]:
current = _atomic_session.get()
if current is not None and current[0] is self:
current[1].flush()
yield current[1]
current[1].flush()
return
session = self.SessionFactory()
try:
yield session
@@ -384,3 +395,13 @@ class HockeyDatabase:
raise
finally:
session.close()
@contextmanager
def atomic_session(self) -> Iterator[Session]:
"""Keep the authority check, service writes and receipt in one transaction."""
with self.session() as session:
token = _atomic_session.set((self, session))
try:
yield session
finally:
_atomic_session.reset(token)