db867dcbe5
Deploy to Staging / deploy (push) Has been cancelled
Conflict detector / main (push) Has been cancelled
Lint Backend / lint-backend (push) Has been cancelled
Playwright Tests / changes (push) Has been cancelled
Test Backend / test-backend (push) Has been cancelled
Test Docker Compose / test-docker-compose (push) Has been cancelled
Playwright Tests / test-playwright (1, 4) (push) Has been cancelled
Playwright Tests / test-playwright (2, 4) (push) Has been cancelled
Playwright Tests / test-playwright (3, 4) (push) Has been cancelled
Playwright Tests / test-playwright (4, 4) (push) Has been cancelled
Playwright Tests / merge-playwright-reports (push) Has been cancelled
Playwright Tests / alls-green-playwright (push) Has been cancelled
Issue Manager / issue-manager (push) Has been cancelled
40 lines
920 B
Python
40 lines
920 B
Python
import logging
|
|
|
|
from sqlalchemy import Engine
|
|
from sqlmodel import Session, select
|
|
from tenacity import after_log, before_log, retry, stop_after_attempt, wait_fixed
|
|
|
|
from app.core.db import engine
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
max_tries = 60 * 5 # 5 minutes
|
|
wait_seconds = 1
|
|
|
|
|
|
@retry(
|
|
stop=stop_after_attempt(max_tries),
|
|
wait=wait_fixed(wait_seconds),
|
|
before=before_log(logger, logging.INFO),
|
|
after=after_log(logger, logging.WARN),
|
|
)
|
|
def init(db_engine: Engine) -> None:
|
|
try:
|
|
with Session(db_engine) as session:
|
|
# 尝试创建会话以检查数据库是否已就绪
|
|
session.exec(select(1))
|
|
except Exception as e:
|
|
logger.error(e)
|
|
raise e
|
|
|
|
|
|
def main() -> None:
|
|
logger.info("正在初始化服务")
|
|
init(engine)
|
|
logger.info("服务初始化完成")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|