"백엔드_핵심_로직_구현_프론트엔드_연동_및_도커_배포_최적화_완료"
This commit is contained in:
69
backend/app/core/startup.py
Normal file
69
backend/app/core/startup.py
Normal file
@@ -0,0 +1,69 @@
|
||||
import logging
|
||||
import asyncio
|
||||
from sqlalchemy import select
|
||||
from app.db.database import SessionLocal
|
||||
from app.db.models import ApiSettings
|
||||
from app.core.config import settings
|
||||
from app.services.kis_auth import kis_auth
|
||||
from app.services.sync_service import sync_service
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
async def run_startup_sequence():
|
||||
"""
|
||||
Executes the Phase 1~4 startup sequence defined in ReadMe.md.
|
||||
"""
|
||||
logger.info("=== Starting System Initialization Sequence ===")
|
||||
|
||||
async with SessionLocal() as db_session:
|
||||
# Phase 1: DB & Settings Load
|
||||
stmt = select(ApiSettings).where(ApiSettings.id == 1)
|
||||
result = await db_session.execute(stmt)
|
||||
settings_obj = result.scalar_one_or_none()
|
||||
|
||||
if not settings_obj:
|
||||
settings_obj = ApiSettings(id=1)
|
||||
db_session.add(settings_obj)
|
||||
await db_session.commit()
|
||||
logger.info("Created Default ApiSettings.")
|
||||
|
||||
# Phase 2: KIS Connectivity
|
||||
if not settings_obj.appKey or not settings_obj.appSecret:
|
||||
logger.warning(">> [Phase 2] KIS Credentials (appKey/Secret) NOT FOUND in DB.")
|
||||
logger.warning(" Please configure them via the Settings Page.")
|
||||
logger.warning(" Skipping Token Issue & Realtime Connection.")
|
||||
else:
|
||||
logger.info(">> [Phase 2] KIS Credentials Found. Attempting Authentication...")
|
||||
try:
|
||||
# 1. Access Token
|
||||
token = await kis_auth.get_access_token(db_session)
|
||||
masked_token = token[:10] + "..." if token else "None"
|
||||
logger.info(f" [OK] Access Token Valid (Starts with: {masked_token})")
|
||||
|
||||
# 2. Approval Key (Optional, lazy load usually, but good to check)
|
||||
# approval_key = await kis_auth.get_approval_key(db_session)
|
||||
# logger.info(" [OK] WebSocket Approval Key Issued.")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f" [FAILED] Authentication Failed: {e}")
|
||||
logger.error(" Please check your AppKey/Secret and ensure KIS API Server is reachable.")
|
||||
|
||||
# Phase 2.5: Telegram (Placeholder)
|
||||
if settings_obj.useTelegram and settings_obj.telegramToken:
|
||||
logger.info(">> [Phase 2] Telegram Token Found. Sending Startup Message...")
|
||||
# TODO: Implement Telegram Sender
|
||||
else:
|
||||
logger.info(">> [Phase 2] Telegram Disabled or Token missing.")
|
||||
|
||||
# Phase 3: Data Sync (Master Stocks & Account)
|
||||
logger.info(">> [Phase 3-1] Syncing Account Data...")
|
||||
await sync_service.sync_account(db_session)
|
||||
|
||||
logger.info(">> [Phase 3-2] Syncing Master Data (This may take a while)...")
|
||||
from app.services.master_service import master_service
|
||||
await master_service.sync_master_data(db_session)
|
||||
|
||||
# Phase 4: Scheduler
|
||||
# (Scheduler is started in main.py)
|
||||
|
||||
logger.info("=== System Initialization Complete ===")
|
||||
Reference in New Issue
Block a user