날짜 수정

This commit is contained in:
2026-02-23 08:37:24 +09:00
parent 1814f5e55e
commit d2829a47a7
7 changed files with 163 additions and 32 deletions

View File

@@ -13,6 +13,7 @@ import asyncio
import json
import logging
import time
import pytz
from app.core.config import settings
from app.api import api_router
@@ -207,16 +208,18 @@ async def lifespan(app: FastAPI):
from datetime import date, timedelta
async with AsyncSessionLocal() as session:
_kst = pytz.timezone("Asia/Seoul")
_today_kst = datetime.now(_kst).date()
# 오래된 stock_analysis 삭제 (7일 이전)
cutoff_date = date.today() - timedelta(days=7)
cutoff_date = _today_kst - timedelta(days=7)
result = await session.execute(
delete(StockAnalysis).where(StockAnalysis.analysis_date < cutoff_date)
)
if result.rowcount > 0:
logger.info(f"🗑️ Deleted {result.rowcount} old stock_analysis records")
# PENDING/FAILED 오래된 거래 삭제 (1일 이전)
cutoff_datetime = date.today() - timedelta(days=1)
cutoff_datetime = _today_kst - timedelta(days=1)
result = await session.execute(
delete(TradeHistory).where(
TradeHistory.status.in_(['PENDING', 'FAILED']),
@@ -248,6 +251,13 @@ async def lifespan(app: FastAPI):
logger.error(traceback.format_exc())
raise
# 데이트레이딩 포지션 복구 (재시작 시 인메모리 유실 방지)
try:
from app.api.endpoints.day_trading import restore_positions_from_db
await restore_positions_from_db()
except Exception as e:
logger.warning(f"⚠️ 데이트레이딩 포지션 복구 실패 (non-critical): {e}")
# 스케줄러 시작
app.state.auto_trade_task = asyncio.create_task(auto_trade_scheduler())
app.state.day_trade_task = asyncio.create_task(day_trade_scheduler())
@@ -279,9 +289,20 @@ app = FastAPI(
)
# CORS 설정 (반드시 라우터 등록 전에)
# allow_origins=["*"] + allow_credentials=True 조합은 CORS 스펙 위반으로 브라우저 차단됨
_cors_origins = getattr(settings, "CORS_ORIGINS", None)
if _cors_origins:
_allowed_origins = [o.strip() for o in _cors_origins.split(",") if o.strip()]
else:
_allowed_origins = [
"http://localhost:5173",
"http://localhost:3000",
"http://127.0.0.1:5173",
"http://127.0.0.1:3000",
]
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_origins=_allowed_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],