debug log 502 error

This commit is contained in:
2025-09-06 02:49:44 +09:00
parent 4cd3745812
commit 49d2aa588b
2 changed files with 410 additions and 5 deletions

232
debug_502_error.py Normal file
View File

@ -0,0 +1,232 @@
#!/usr/bin/env python3
"""
502エラー調査スクリプト: checkin_from_rogappエンドポイントのデバッグ
"""
import subprocess
import time
import requests
import json
from datetime import datetime
def test_checkin_endpoint():
"""
checkin_from_rogappエンドポイントをテストして502エラーを再現
"""
print("🔍 502エラー調査: checkin_from_rogappエンドポイント")
print("=" * 60)
# テストデータ
test_data = {
"event_code": "fc_gifu_2025",
"team_name": "テストチーム",
"cp_number": "1",
"image": "data:image/jpeg;base64,/9j/4AAQSkZJRgABA...", # 短縮版
"buy_flag": False,
"gps_coordinates": {
"latitude": 35.6762,
"longitude": 139.6503,
"accuracy": 5.0,
"timestamp": datetime.now().isoformat()
},
"camera_metadata": {
"capture_time": datetime.now().isoformat(),
"device_info": "debug_script"
}
}
# URLを構築
base_url = "http://localhost:8100"
checkin_url = f"{base_url}/gifuroge/checkin_from_rogapp"
print(f"🎯 テスト対象URL: {checkin_url}")
print(f"📊 テストデータ: {json.dumps({k: v if k != 'image' else '[BASE64_DATA]' for k, v in test_data.items()}, indent=2, ensure_ascii=False)}")
try:
print(f"\n🚀 POSTリクエスト送信中...")
# リクエスト送信
response = requests.post(
checkin_url,
json=test_data,
headers={
'Content-Type': 'application/json',
'User-Agent': 'Debug-Script/1.0'
},
timeout=30
)
print(f"📥 レスポンス受信:")
print(f" ステータスコード: {response.status_code}")
print(f" ヘッダー: {dict(response.headers)}")
if response.status_code == 502:
print(f"❌ 502 Bad Gateway エラーを確認しました")
print(f" レスポンステキスト: {response.text}")
return False
elif response.status_code == 200:
print(f"✅ 正常レスポンス")
print(f" レスポンスデータ: {response.json()}")
return True
else:
print(f"⚠️ 予期しないステータスコード: {response.status_code}")
print(f" レスポンステキスト: {response.text}")
return False
except requests.exceptions.ConnectionError as e:
print(f"❌ 接続エラー: {e}")
return False
except requests.exceptions.Timeout as e:
print(f"❌ タイムアウトエラー: {e}")
return False
except Exception as e:
print(f"❌ その他のエラー: {e}")
return False
def monitor_logs_during_test():
"""
テスト実行中のログを監視
"""
print(f"\n🔍 ログ監視開始")
print("-" * 40)
try:
# アプリケーションログを監視
result = subprocess.run(
['docker', 'compose', 'logs', '--tail=20', '--follow', 'app'],
cwd='/Volumes/PortableSSD1TB/main/GifuTabi/rogaining_srv_exdb-2/rogaining_srv',
capture_output=True,
text=True,
timeout=10
)
print(f"📋 アプリケーションログ:")
print(result.stdout)
if result.stderr:
print(f"⚠️ エラー出力:")
print(result.stderr)
except subprocess.TimeoutExpired:
print(f"⏰ ログ監視タイムアウト(正常)")
except Exception as e:
print(f"❌ ログ監視エラー: {e}")
def check_docker_services():
"""
Dockerサービスの状態確認
"""
print(f"\n🐳 Dockerサービス状態確認")
print("-" * 40)
try:
# サービス状態確認
result = subprocess.run(
['docker', 'compose', 'ps'],
cwd='/Volumes/PortableSSD1TB/main/GifuTabi/rogaining_srv_exdb-2/rogaining_srv',
capture_output=True,
text=True,
check=True
)
print(f"📊 サービス状態:")
print(result.stdout)
# ヘルスチェック
health_result = subprocess.run(
['docker', 'compose', 'exec', 'app', 'python', 'manage.py', 'check'],
cwd='/Volumes/PortableSSD1TB/main/GifuTabi/rogaining_srv_exdb-2/rogaining_srv',
capture_output=True,
text=True
)
if health_result.returncode == 0:
print(f"✅ Djangoアプリケーション: 正常")
print(health_result.stdout)
else:
print(f"❌ Djangoアプリケーション: エラー")
print(health_result.stderr)
except subprocess.CalledProcessError as e:
print(f"❌ Dockerコマンドエラー: {e}")
print(f"stderr: {e.stderr}")
except Exception as e:
print(f"❌ その他のエラー: {e}")
def analyze_nginx_config():
"""
nginx設定の確認
"""
print(f"\n🌐 nginx設定確認")
print("-" * 40)
try:
# nginx設定テスト
result = subprocess.run(
['docker', 'compose', 'exec', 'nginx', 'nginx', '-t'],
cwd='/Volumes/PortableSSD1TB/main/GifuTabi/rogaining_srv_exdb-2/rogaining_srv',
capture_output=True,
text=True
)
if result.returncode == 0:
print(f"✅ nginx設定: 正常")
print(result.stdout)
else:
print(f"❌ nginx設定: エラー")
print(result.stderr)
# 最近のnginxエラーログ
error_log_result = subprocess.run(
['docker', 'compose', 'logs', '--tail=10', 'nginx'],
cwd='/Volumes/PortableSSD1TB/main/GifuTabi/rogaining_srv_exdb-2/rogaining_srv',
capture_output=True,
text=True
)
print(f"\n📋 nginx最近のログ:")
print(error_log_result.stdout)
except Exception as e:
print(f"❌ nginx確認エラー: {e}")
def main():
"""
メイン実行関数
"""
print(f"🚨 502 Bad Gateway エラー調査ツール")
print(f"時刻: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print("=" * 60)
# 1. Dockerサービス状態確認
check_docker_services()
# 2. nginx設定確認
analyze_nginx_config()
# 3. エンドポイントテスト
print(f"\n🎯 エンドポイントテスト実行")
print("-" * 40)
success = test_checkin_endpoint()
# 4. ログ確認
monitor_logs_during_test()
# 結果まとめ
print(f"\n📊 調査結果まとめ")
print("=" * 60)
if success:
print(f"✅ checkin_from_rogappエンドポイントは正常に動作しています")
print(f"💡 502エラーは一時的な問題だった可能性があります")
else:
print(f"❌ 502エラーを確認しました")
print(f"💡 次の対策が必要です:")
print(f" 1. アプリケーションのエラーログを詳細確認")
print(f" 2. データベース接続状態の確認")
print(f" 3. メモリ使用量やリソース状況の確認")
print(f" 4. 依存関係やモジュールの問題確認")
if __name__ == "__main__":
main()