49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
"""
|
|
KIS API 토큰 선제 발급 스크립트
|
|
|
|
매일 오전 6시에 실행하여 토큰을 미리 발급·캐시함으로써
|
|
장 시작 전(8:50 배치, 자동매매)의 불필요한 토큰 발급 지연을 방지한다.
|
|
|
|
토큰 유효기간: 23시간 (6시 발급 → 다음날 5시까지 유효)
|
|
Cron: 0 6 * * 1-5 docker exec autostock-backend python /app/prefetch_token.py >> /logs/token_prefetch.log 2>&1
|
|
"""
|
|
|
|
import asyncio
|
|
import sys
|
|
from pathlib import Path
|
|
from datetime import datetime
|
|
|
|
sys.path.insert(0, str(Path(__file__).parent))
|
|
|
|
import logging
|
|
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
|
)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
async def main():
|
|
from app.services.korea_investment_api import KoreaInvestmentAPI
|
|
|
|
logger.info(f"🔑 [{datetime.now().strftime('%H:%M:%S')}] 토큰 선제 발급 시작")
|
|
|
|
api = KoreaInvestmentAPI()
|
|
|
|
# 캐시된 토큰이 있어도 강제 재발급 (토큰 만료 임박 방지)
|
|
api.access_token = None
|
|
api.token_expires_at = None
|
|
|
|
try:
|
|
token = await api._get_access_token()
|
|
expires = api.token_expires_at.strftime('%Y-%m-%d %H:%M') if api.token_expires_at else "unknown"
|
|
logger.info(f"✅ 토큰 발급 완료 — 만료: {expires}")
|
|
except Exception as e:
|
|
logger.error(f"❌ 토큰 발급 실패: {e}")
|
|
sys.exit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|