main_server.py가 중복 실행되면서 좀비 프로세스가 수행되는 오류 해결, process_tracker.py가 감시하면서 할당되지 않은 pid가 존재하면 좀비프로세스로 판단하여 kill

This commit is contained in:
2026-02-11 07:48:06 +09:00
parent 7f2f575ec8
commit 4fd0aa91bc
8 changed files with 689 additions and 371 deletions

View File

@@ -24,11 +24,12 @@ news_collector = None
import multiprocessing
from modules.bot import AutoTradingBot
from modules.utils.process_tracker import ProcessTracker
from modules.services.telegram_bot.runner import run_telegram_bot_standalone
# 봇 실행 래퍼 함수
def run_trading_bot():
ProcessTracker.register("Trading Bot Main")
bot = AutoTradingBot()
bot.loop()
@@ -41,6 +42,12 @@ async def lifespan(app: FastAPI):
Config.validate()
# 2. 전역 객체 초기화 (서버용)
# [Process Tracker] 초기화
try:
ProcessTracker.clear()
ProcessTracker.register("Main Server (Uvicorn Worker)")
except: pass
ai_agent = OllamaManager()
kis_client = KISClient()
news_collector = NewsCollector()
@@ -56,6 +63,13 @@ async def lifespan(app: FastAPI):
telegram_process = multiprocessing.Process(target=run_telegram_bot_standalone)
telegram_process.start()
# [Process Tracker] 자식 프로세스 PID 기록 (부모 관점)
try:
with open(ProcessTracker.FILE_PATH, "a", encoding="utf-8") as f:
f.write(f"{bot_process.pid}: Trading Bot Process (Parent View)\n")
f.write(f"{telegram_process.pid}: Telegram Bot Process (Parent View)\n")
except: pass
messenger.send_message("🖥️ **[Server Started]** Windows AI Server (Refactored) Online.")
yield
@@ -65,13 +79,17 @@ async def lifespan(app: FastAPI):
if telegram_process and telegram_process.is_alive():
print(" - Stopping Telegram Bot...")
telegram_process.terminate()
telegram_process.join()
telegram_process.join(timeout=5)
if telegram_process.is_alive():
telegram_process.terminate()
telegram_process.join()
if bot_process and bot_process.is_alive():
print(" - Stopping Trading Bot...")
bot_process.terminate()
bot_process.join()
bot_process.join(timeout=5)
if bot_process.is_alive():
bot_process.terminate()
bot_process.join()
messenger.send_message("🛑 **[Server Stopped]** Server Shutting Down.")
@@ -139,4 +157,12 @@ async def analyze_portfolio():
return {"analysis": analysis}
if __name__ == "__main__":
uvicorn.run("main_server:app", host="0.0.0.0", port=8000, reload=True)
# [안정성] 서버 시작 시 이전 좀비 프로세스 정리
try:
from modules.utils.process_tracker import ProcessTracker
ProcessTracker.check_and_kill_zombies()
except: pass
# Reload=True는 멀티프로세싱 자식 프로세스 관리에 취약하므로 비활성화 권장
print("🚀 Starting Windows AI Server...")
uvicorn.run("main_server:app", host="0.0.0.0", port=8000, reload=False)