55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
|
|
import subprocess, os, sys, asyncio, time
|
||
|
|
|
||
|
|
BASE = r'C:\Users\whdwo\OneDrive\바탕 화면\stockbot_v3'
|
||
|
|
pid_file = os.path.join(BASE, 'logs', 'bot.pid')
|
||
|
|
os.chdir(BASE)
|
||
|
|
sys.path.insert(0, BASE)
|
||
|
|
|
||
|
|
# 토큰 발급 가능한지 테스트 (최대 3분 재시도)
|
||
|
|
from app.main import load_env
|
||
|
|
load_env()
|
||
|
|
|
||
|
|
max_wait = 180
|
||
|
|
interval = 15
|
||
|
|
elapsed = 0
|
||
|
|
while elapsed < max_wait:
|
||
|
|
try:
|
||
|
|
import httpx
|
||
|
|
from app.execution.kis_client import KISClient
|
||
|
|
client = KISClient()
|
||
|
|
asyncio.run(client.get_access_token())
|
||
|
|
print(f"토큰 발급 성공 ({elapsed}초 경과)")
|
||
|
|
break
|
||
|
|
except RuntimeError as e:
|
||
|
|
if "EGW00133" in str(e) or "잠시 후" in str(e):
|
||
|
|
print(f"토큰 대기 중... ({elapsed}초, {interval}초 후 재시도)")
|
||
|
|
time.sleep(interval)
|
||
|
|
elapsed += interval
|
||
|
|
else:
|
||
|
|
print(f"토큰 오류 (재시도 불가): {e}")
|
||
|
|
sys.exit(1)
|
||
|
|
else:
|
||
|
|
print("토큰 발급 타임아웃")
|
||
|
|
sys.exit(1)
|
||
|
|
|
||
|
|
# 봇 시작
|
||
|
|
log_path = os.path.join(BASE, "logs", "bot_stderr.log")
|
||
|
|
proc = subprocess.Popen(
|
||
|
|
[sys.executable, "app/main.py"],
|
||
|
|
creationflags=subprocess.DETACHED_PROCESS | subprocess.CREATE_NEW_PROCESS_GROUP,
|
||
|
|
stdout=open(log_path, "a", encoding="utf-8"),
|
||
|
|
stderr=subprocess.STDOUT,
|
||
|
|
close_fds=True,
|
||
|
|
)
|
||
|
|
with open(pid_file, "w") as f:
|
||
|
|
f.write(str(proc.pid))
|
||
|
|
print(f"봇 시작 완료 PID={proc.pid}")
|
||
|
|
|
||
|
|
from app.monitor.notifier import send
|
||
|
|
import os as _os
|
||
|
|
mode = _os.getenv("KIS_MOCK", "true")
|
||
|
|
dry = _os.getenv("DRY_RUN", "true")
|
||
|
|
label = "[모의투자]" if mode == "true" else "[실거래]"
|
||
|
|
asyncio.run(send(f"{label} 자동매매 봇 시작 (DRY_RUN={dry})"))
|
||
|
|
print("Discord 알림 전송 완료")
|