fix: DB 타임스탬프 KST 통일 + KIS 토큰 캐시 영속화

시간대 혼재: 모델 기본값은 datetime.utcnow(UTC), 수동 매매 등 일부 경로는 _get_kst_now(KST)로
저장해 같은 테이블에 9시간 차가 섞였다(화면 매매시각 02:14 등). 조회 구간(utcnow - days)도
KST 저장분과 비교돼 어긋났고, 추가매수 쿨다운은 (UTC now - KST created_at)이 음수라 상시
발동했다. app.core.timeutil.now_kst() 도입 후 모델 기본값 27곳과 app/ 전역 utcnow() 호출을
모두 KST 기준으로 통일(잔존 0건, 테스트로 고정).

KIS 토큰 캐시(~/.cache/auto-stock)가 컨테이너에만 있어 배포마다 사라지고 토큰을 새로
발급받다 KIS가 EGW00103(유효하지 않은 AppKey)로 거부 → 502·잔고 0 유발. 볼륨 마운트로
재배포 후에도 재사용.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-09-23 11:24:14 +09:00
parent e2d4b607cb
commit ec2fa2b423
22 changed files with 177 additions and 69 deletions

View File

@@ -24,6 +24,7 @@ from bs4 import BeautifulSoup
from app.core.config import settings
from app.core.redis import setex_json, get_json
from app.services.db_worker import db_task_queue
from app.core.timeutil import now_kst
logger = logging.getLogger(__name__)
@@ -168,7 +169,7 @@ def _compute_aggregate_score(articles: List[Dict[str, Any]]) -> float:
if not articles:
return 0.0
now = datetime.utcnow()
now = now_kst()
weighted_sum = 0.0
weight_total = 0.0
@@ -327,7 +328,7 @@ async def collect_market_news(limit: int = 0) -> List[Dict[str, Any]]:
"stock_codes": [], # 시장 뉴스는 특정 종목 없음
"sentiment": label,
"sentiment_score": score,
"published_at": datetime.utcnow(), # 파싱 불가 시 현재 시간
"published_at": now_kst(), # 파싱 불가 시 현재 시간
})
if len(articles) >= limit:
@@ -392,7 +393,7 @@ async def collect_newsapi_news(
try:
published_at = datetime.fromisoformat(pub_str.replace("Z", "+00:00"))
except Exception:
published_at = datetime.utcnow()
published_at = now_kst()
score, label = analyze_sentiment_keyword(title, content)
@@ -567,7 +568,7 @@ def _parse_naver_date(date_str: str) -> datetime:
형식: "2026.03.24 09:15" 또는 "2026.03.24"
"""
if not date_str:
return datetime.utcnow()
return now_kst()
date_str = date_str.strip()
for fmt in ("%Y.%m.%d %H:%M", "%Y.%m.%d"):
@@ -576,7 +577,7 @@ def _parse_naver_date(date_str: str) -> datetime:
except ValueError:
continue
return datetime.utcnow()
return now_kst()
# ═════════════════════════════════════════════════════════════════════════════