162 lines
5.3 KiB
Python
Executable File
162 lines
5.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
원격 연결 테스트 스크립트
|
|
원격지에서 메인서버 Redis 연결 상태를 확인합니다.
|
|
"""
|
|
|
|
import sys
|
|
import argparse
|
|
import redis
|
|
from celery import Celery
|
|
|
|
def test_redis_connection(host, port=6379):
|
|
"""Redis 서버 연결 테스트"""
|
|
print(f"=== Redis 연결 테스트 ===")
|
|
print(f"서버: {host}:{port}")
|
|
|
|
try:
|
|
r = redis.Redis(host=host, port=port, db=0, socket_timeout=5)
|
|
pong = r.ping()
|
|
print(f"✅ Redis 연결 성공: {pong}")
|
|
|
|
# Redis 정보 확인
|
|
info = r.info()
|
|
print(f"Redis 버전: {info.get('redis_version')}")
|
|
print(f"연결된 클라이언트: {info.get('connected_clients')}")
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f"❌ Redis 연결 실패: {e}")
|
|
return False
|
|
|
|
def test_celery_connection(redis_url):
|
|
"""Celery 연결 및 워커 상태 테스트"""
|
|
print(f"\n=== Celery 연결 테스트 ===")
|
|
print(f"Redis URL: {redis_url}")
|
|
|
|
try:
|
|
celery_app = Celery('test_client', broker=redis_url, backend=redis_url)
|
|
|
|
# 워커 상태 확인
|
|
inspect = celery_app.control.inspect()
|
|
|
|
# 활성 워커 목록
|
|
active_workers = inspect.active()
|
|
if active_workers:
|
|
print("✅ 활성 워커:")
|
|
for worker_name, tasks in active_workers.items():
|
|
print(f" - {worker_name}: {len(tasks)}개 작업 실행 중")
|
|
else:
|
|
print("⚠️ 활성 워커가 없습니다")
|
|
|
|
# 등록된 작업 목록
|
|
registered_tasks = inspect.registered()
|
|
if registered_tasks:
|
|
print("\n📋 등록된 작업:")
|
|
for worker_name, tasks in registered_tasks.items():
|
|
print(f" {worker_name}:")
|
|
for task in tasks:
|
|
if 'worker.' in task or 'app.' in task:
|
|
print(f" - {task}")
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f"❌ Celery 연결 실패: {e}")
|
|
return False
|
|
|
|
def test_network_connectivity(host):
|
|
"""네트워크 연결 테스트"""
|
|
print(f"\n=== 네트워크 연결 테스트 ===")
|
|
|
|
import subprocess
|
|
try:
|
|
# ping 테스트
|
|
result = subprocess.run(['ping', '-c', '3', host],
|
|
capture_output=True, text=True, timeout=10)
|
|
if result.returncode == 0:
|
|
print(f"✅ {host} ping 성공")
|
|
return True
|
|
else:
|
|
print(f"❌ {host} ping 실패")
|
|
return False
|
|
except Exception as e:
|
|
print(f"❌ 네트워크 테스트 오류: {e}")
|
|
return False
|
|
|
|
def test_port_connectivity(host, port=6379):
|
|
"""포트 연결 테스트"""
|
|
print(f"\n=== 포트 연결 테스트 ===")
|
|
|
|
import socket
|
|
try:
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
sock.settimeout(5)
|
|
result = sock.connect_ex((host, port))
|
|
sock.close()
|
|
|
|
if result == 0:
|
|
print(f"✅ {host}:{port} 포트 연결 성공")
|
|
return True
|
|
else:
|
|
print(f"❌ {host}:{port} 포트 연결 실패")
|
|
return False
|
|
except Exception as e:
|
|
print(f"❌ 포트 테스트 오류: {e}")
|
|
return False
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="원격 메인서버 연결 테스트")
|
|
parser.add_argument('--host', required=True, help='메인서버 IP 주소')
|
|
parser.add_argument('--port', type=int, default=6379, help='Redis 포트 (기본: 6379)')
|
|
parser.add_argument('--skip-ping', action='store_true', help='ping 테스트 건너뛰기')
|
|
|
|
args = parser.parse_args()
|
|
|
|
print("🔍 원격 메인서버 연결 테스트 시작")
|
|
print(f"대상 서버: {args.host}:{args.port}")
|
|
print("=" * 50)
|
|
|
|
success_count = 0
|
|
total_tests = 4
|
|
|
|
# 1. 네트워크 연결 테스트
|
|
if not args.skip_ping:
|
|
if test_network_connectivity(args.host):
|
|
success_count += 1
|
|
else:
|
|
print("⏭️ ping 테스트 건너뜀")
|
|
total_tests -= 1
|
|
|
|
# 2. 포트 연결 테스트
|
|
if test_port_connectivity(args.host, args.port):
|
|
success_count += 1
|
|
|
|
# 3. Redis 연결 테스트
|
|
if test_redis_connection(args.host, args.port):
|
|
success_count += 1
|
|
|
|
# 4. Celery 연결 테스트
|
|
redis_url = f"redis://{args.host}:{args.port}/0"
|
|
if test_celery_connection(redis_url):
|
|
success_count += 1
|
|
|
|
# 결과 요약
|
|
print("\n" + "=" * 50)
|
|
print(f"📊 테스트 결과: {success_count}/{total_tests} 성공")
|
|
|
|
if success_count == total_tests:
|
|
print("🎉 모든 테스트 통과! 워커를 시작할 수 있습니다.")
|
|
print(f"\n🚀 워커 시작 명령:")
|
|
print(f"MAIN_SERVER_IP={args.host} ./start_worker.sh")
|
|
return 0
|
|
else:
|
|
print("❌ 일부 테스트 실패. 네트워크 및 방화벽 설정을 확인하세요.")
|
|
print(f"\n🛠️ 문제 해결:")
|
|
print(f"1. 메인서버 방화벽: sudo ufw allow {args.port}/tcp")
|
|
print(f"2. Redis 설정 확인: docker ps | grep redis")
|
|
print(f"3. 네트워크 연결 확인: ping {args.host}")
|
|
return 1
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main()) |