generated from john/python-template
This commit is contained in:
@@ -7,12 +7,10 @@ from dataclasses import dataclass
|
||||
from datetime import UTC
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy import update
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from sqlmodel import SQLModel
|
||||
from sqlmodel import col
|
||||
from sqlmodel import func
|
||||
from sqlmodel import select
|
||||
@@ -51,10 +49,6 @@ class MaintenanceError(AppError):
|
||||
class MaintenanceService(ServiceBase):
|
||||
"""Persist and execute background maintenance runs."""
|
||||
|
||||
def __init__(self, session_factory: Any = None, settings: Any = None):
|
||||
super().__init__(session_factory=session_factory, settings=settings)
|
||||
self._maintenance_table_ready = False
|
||||
|
||||
async def list_runs(
|
||||
self,
|
||||
*,
|
||||
@@ -63,7 +57,6 @@ class MaintenanceService(ServiceBase):
|
||||
) -> list[MaintenanceRun]:
|
||||
try:
|
||||
async with self._session_scope(session) as _session:
|
||||
await self._ensure_runs_table(session=_session)
|
||||
query = (
|
||||
select(MaintenanceRun)
|
||||
.order_by(col(MaintenanceRun.created_at).desc(), col(MaintenanceRun.id).desc())
|
||||
@@ -92,14 +85,13 @@ class MaintenanceService(ServiceBase):
|
||||
)
|
||||
try:
|
||||
async with self._session_scope(session) as _session:
|
||||
await self._ensure_runs_table(session=_session)
|
||||
_session.add(run)
|
||||
await self._finalize(session=_session, caller_session=session, refresh=(run,))
|
||||
except SQLAlchemyError as exc:
|
||||
raise MaintenanceError(
|
||||
"Maintenance run could not be queued.",
|
||||
category=ErrorCategory.INFRA_PERSISTENT,
|
||||
suggestion="Verify database schema access and retry.",
|
||||
suggestion="Run the V6.1 schema migration, then retry.",
|
||||
detail=f"Failed to enqueue maintenance run: {type(exc).__name__}: {exc}",
|
||||
) from exc
|
||||
return run
|
||||
@@ -107,7 +99,6 @@ class MaintenanceService(ServiceBase):
|
||||
async def claim_next_queued_run(self, *, session: AsyncSession | None = None) -> MaintenanceRun | None:
|
||||
try:
|
||||
async with self._session_scope(session) as _session:
|
||||
await self._ensure_runs_table(session=_session)
|
||||
now = _utc_now_naive()
|
||||
queued_run_id = (
|
||||
select(col(MaintenanceRun.id))
|
||||
@@ -144,29 +135,6 @@ class MaintenanceService(ServiceBase):
|
||||
detail=f"Failed to claim queued maintenance run: {type(exc).__name__}: {exc}",
|
||||
) from exc
|
||||
|
||||
async def _ensure_runs_table(self, *, session: AsyncSession) -> None:
|
||||
if self._maintenance_table_ready:
|
||||
return
|
||||
table = SQLModel.metadata.tables.get("maintenance_run")
|
||||
if table is None:
|
||||
raise MaintenanceError(
|
||||
"Maintenance storage is unavailable.",
|
||||
category=ErrorCategory.INTERNAL_UNEXPECTED,
|
||||
suggestion="Check database schema registration, then retry.",
|
||||
detail="maintenance_run table metadata is not registered.",
|
||||
)
|
||||
try:
|
||||
connection = await session.connection()
|
||||
await connection.run_sync(lambda sync_connection: table.create(sync_connection, checkfirst=True))
|
||||
except SQLAlchemyError as exc:
|
||||
raise MaintenanceError(
|
||||
"Maintenance storage is unavailable.",
|
||||
category=ErrorCategory.INFRA_PERSISTENT,
|
||||
suggestion="Check database availability and schema permissions, then retry.",
|
||||
detail=f"Failed to ensure maintenance_run table: {type(exc).__name__}: {exc}",
|
||||
) from exc
|
||||
self._maintenance_table_ready = True
|
||||
|
||||
async def process_next_queued_run(self, *, session: AsyncSession | None = None) -> bool:
|
||||
run = await self.claim_next_queued_run(session=session)
|
||||
if run is None:
|
||||
|
||||
Reference in New Issue
Block a user