mirror of
https://github.com/bellingcat/auto-archiver-api.git
synced 2026-06-11 21:18:35 +03:00
WAL checkpoint at startup
This commit is contained in:
@@ -6,9 +6,10 @@ from fastapi import FastAPI
|
||||
from contextlib import asynccontextmanager
|
||||
from fastapi_utils.tasks import repeat_every
|
||||
from loguru import logger
|
||||
from sqlalchemy import text
|
||||
|
||||
from db import crud, models, schemas
|
||||
from db.database import get_db, get_db_async, make_engine
|
||||
from db.database import get_db, get_db_async, make_engine, wal_checkpoint
|
||||
from shared.settings import get_settings
|
||||
from utils.metrics import measure_regular_metrics, redis_subscribe_worker_exceptions
|
||||
from worker.main import create_sheet_task
|
||||
@@ -41,6 +42,8 @@ async def lifespan(app: FastAPI):
|
||||
else:
|
||||
logger.warning("[CRON] Delete stale sheets cronjob is disabled.")
|
||||
|
||||
wal_checkpoint()
|
||||
|
||||
yield # separates startup from shutdown instructions
|
||||
|
||||
# SHUTDOWN
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from functools import lru_cache
|
||||
from sqlalchemy import Engine, create_engine, event
|
||||
from sqlalchemy import Engine, create_engine, event, text
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from shared.settings import get_settings
|
||||
from contextlib import asynccontextmanager, contextmanager
|
||||
@@ -36,11 +36,20 @@ def get_db_dependency():
|
||||
with get_db() as db:
|
||||
yield db
|
||||
|
||||
def wal_checkpoint():
|
||||
# WAL checkpointing, make sure the .sqlite file receives the latest changes
|
||||
# to be called at startup as it halts writes
|
||||
with get_db() as db:
|
||||
db.execute(text("PRAGMA wal_checkpoint(TRUNCATE)"))
|
||||
|
||||
|
||||
# ASYNC connections
|
||||
async def make_async_engine(database_url: str) -> AsyncEngine:
|
||||
engine = create_async_engine(database_url, connect_args={"check_same_thread": False})
|
||||
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(lambda sync_conn: sync_conn.execute("PRAGMA journal_mode=WAL;"))
|
||||
|
||||
return engine
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user