o2o-negosium-original/agent/bootstrap/lifespan.py

37 lines
1.9 KiB
Python

"""서버 기동 부트스트랩 (운영 자동화).
ensure_base_seeded: 공유 베이스 정책(_base)이 없으면 자동 시드한다.
- 이미 있으면 빠르게 스킵(매 재시작마다 가벼운 체크만).
- DB 미가용 시 서버 기동을 막지 않고 경고만 남긴다.
- 신규 테넌트는 첫 협상에서 이 베이스를 cold-start warm-start 복제한다(별도 작업 불필요).
"""
from common.database.db_session_manager import DB_SESSION_MNG
from common.database.model.models import BASE_COMPANY_ID
from common.enums import DBType, DBWRType, ErrorType
from common.logger import LOG
from config.server_configs import agent_config
from negotiation.qtable.infra.repository.learning_repository import LearningRepository
async def ensure_base_seeded():
if not agent_config.auto_seed_base:
LOG.i("[bootstrap] auto_seed_base=false — skip base seeding")
return
try:
repo = LearningRepository(BASE_COMPANY_ID)
err, active = await DB_SESSION_MNG.execute_lambda(
DBType.MAIN.value, DBWRType.DB_READ.value, lambda s: repo.get_active_version(s))
if err == ErrorType.SUCCESS and active is not None:
LOG.i(f"[bootstrap] base policy 존재 (v={active.version_name}, {active.state_space_size}x{active.action_space_size}) — 시드 스킵")
return
LOG.i("[bootstrap] base policy 없음 — _base 자동 시드 시작 ...")
from tools.init_base import seed_base
info = await seed_base(action_space=agent_config.base_action_space,
episodes=agent_config.base_seed_episodes)
LOG.i(f"[bootstrap] base 시드 완료: {info['state_space']}x{info['action_space']} cells={info['cells']} "
f"(신규 테넌트는 첫 협상에서 자동 warm-start)")
except Exception as ex:
LOG.e_no_callstack(f"[bootstrap] base 시드 스킵 (DB 미가용?): {type(ex).__name__}: {ex}")