반복적인 IPC 오류 해결, 봇 오류 해결, 인증 오류 해결, 서버 자원 할당 오류 해결, 코드 리팩토링
This commit is contained in:
@@ -4,72 +4,73 @@
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import multiprocessing
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# 환경 변수 로드
|
||||
load_dotenv()
|
||||
|
||||
def run_telegram_bot_standalone():
|
||||
|
||||
def run_telegram_bot_standalone(ipc_lock=None, command_queue=None, shutdown_event=None):
|
||||
"""텔레그램 봇만 독립적으로 실행"""
|
||||
# 경로 문제 해결을 위해 상위 디렉토리 추가
|
||||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../')))
|
||||
|
||||
from modules.services.telegram_bot.server import TelegramBotServer
|
||||
from modules.utils.ipc import BotIPC
|
||||
from modules.config import Config
|
||||
from modules.utils.ipc import SharedIPC
|
||||
from modules.utils.process_tracker import ProcessTracker
|
||||
|
||||
token = os.getenv("TELEGRAM_BOT_TOKEN")
|
||||
if not token:
|
||||
print("❌ [Telegram] TELEGRAM_BOT_TOKEN not found in .env")
|
||||
print("[Telegram] TELEGRAM_BOT_TOKEN not found in .env")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
ProcessTracker.register("Telegram Bot Standalone")
|
||||
print(f"🤖 [Telegram Bot Process] Starting... (PID: {os.getpid()})")
|
||||
print(f"🔗 [Telegram Bot] Standalone Process Mode (IPC Enabled)")
|
||||
|
||||
# IPC 초기화
|
||||
ipc = BotIPC()
|
||||
|
||||
# [최적화] 재시작 루프 구현
|
||||
print(f"[Telegram Bot Process] Starting... (PID: {os.getpid()})")
|
||||
|
||||
# IPC 초기화 (shared memory + command queue)
|
||||
ipc = SharedIPC(lock=ipc_lock, command_queue=command_queue)
|
||||
|
||||
while True:
|
||||
# shutdown 체크
|
||||
if shutdown_event and shutdown_event.is_set():
|
||||
print("[Telegram Bot] Shutdown signal received.")
|
||||
break
|
||||
|
||||
try:
|
||||
# 봇 서버 생성 (매번 새로 생성)
|
||||
bot_server = TelegramBotServer(token)
|
||||
|
||||
# IPC를 통해 메인 봇 데이터 가져오기
|
||||
bot_server = TelegramBotServer(token, ipc=ipc, shutdown_event=shutdown_event)
|
||||
|
||||
# 초기 데이터 로드
|
||||
try:
|
||||
# 초기 연결 시도
|
||||
instance_data = ipc.get_bot_instance_data()
|
||||
if instance_data:
|
||||
bot_server.set_bot_instance(instance_data)
|
||||
except Exception:
|
||||
pass # 연결 실패해도 일단 봇은 띄움
|
||||
|
||||
# 봇 실행 (블로킹)
|
||||
pass
|
||||
|
||||
bot_server.run()
|
||||
|
||||
# 재시작 요청 확인
|
||||
|
||||
if bot_server.should_restart:
|
||||
print("🔄 [Telegram Bot] Restarting instance...")
|
||||
import time
|
||||
time.sleep(1) # 잠시 대기
|
||||
print("[Telegram Bot] Restarting instance...")
|
||||
time.sleep(1)
|
||||
continue
|
||||
else:
|
||||
print("🛑 [Telegram Bot] Process exiting.")
|
||||
print("[Telegram Bot] Process exiting.")
|
||||
break
|
||||
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("\n🛑 [Telegram Bot] Stopped by user")
|
||||
print("[Telegram Bot] Stopped by user")
|
||||
break
|
||||
except Exception as e:
|
||||
if "Conflict" not in str(e):
|
||||
print(f"❌ [Telegram Bot] Error: {e}")
|
||||
print(f"[Telegram Bot] Error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
break
|
||||
|
||||
# 정리
|
||||
ipc.cleanup()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
multiprocessing.freeze_support()
|
||||
run_telegram_bot_standalone()
|
||||
|
||||
Reference in New Issue
Block a user