File size: 7,335 Bytes
4e3c158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python3
"""
🚀 ATOM Backend Diagnostic Script
Quickly diagnose and fix backend server issues
"""

import os
from pathlib import Path
import socket
import subprocess
import sys
import time
import requests


def check_port_availability(port=5058):
    """Check if port is available"""
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(1)
        result = sock.connect_ex(("localhost", port))
        sock.close()
        return result == 0
    except Exception as e:
        return False


def check_backend_process():
    """Check if backend process is running"""
    try:
        result = subprocess.run(
            ["pgrep", "-f", "python.*main_api_app.py"], capture_output=True, text=True
        )
        return result.returncode == 0
    except Exception:
        return False


def check_health_endpoint():
    """Check if health endpoint responds"""
    try:
        response = requests.get("http://localhost:5058/healthz", timeout=5)
        return response.status_code == 200
    except Exception:
        return False


def check_python_dependencies():
    """Check if required Python dependencies are available"""
    required_modules = [
        "flask",
        "werkzeug",
        "requests",
        "sqlalchemy",
        "psycopg2",
        "celery",
    ]

    missing_modules = []
    for module in required_modules:
        try:
            __import__(module)
        except ImportError:
            missing_modules.append(module)

    return missing_modules


def check_file_structure():
    """Check if required files exist"""
    required_files = [
        "backend/python-api-service/main_api_app.py",
        "backend/python-api-service/dashboard_routes.py",
        "backend/python-api-service/service_registry_routes.py",
        "backend/python-api-service/workflow_agent_integration.py",
        "backend/python-api-service/nlu_bridge_service.py",
    ]

    missing_files = []
    for file_path in required_files:
        if not Path(file_path).exists():
            missing_files.append(file_path)

    return missing_files


def start_backend_server():
    """Start the backend server"""
    print("🚀 Starting backend server...")
    try:
        # Kill any existing processes
        subprocess.run(["pkill", "-f", "python.*main_api_app.py"], capture_output=True)
        time.sleep(2)

        # Start the server in background
        process = subprocess.Popen(
            ["python3", "backend/python-api-service/main_api_app.py"],
            cwd=".",
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True,
        )

        # Wait for server to start
        print("⏳ Waiting for server to start...")
        for i in range(30):  # Wait up to 30 seconds
            if check_health_endpoint():
                print("✅ Backend server started successfully!")
                return True
            time.sleep(1)

        print("❌ Server failed to start within 30 seconds")
        return False

    except Exception as e:
        print(f"❌ Error starting server: {e}")
        return False


def run_comprehensive_diagnosis():
    """Run comprehensive diagnosis"""
    print("🔍 Running ATOM Backend Diagnosis...")
    print("=" * 50)

    # Check 1: Port availability
    print("1. Checking port 5058 availability...")
    port_available = check_port_availability()
    print(f"   {'✅ Port available' if port_available else '❌ Port in use'}")

    # Check 2: Backend process
    print("2. Checking backend process...")
    process_running = check_backend_process()
    print(f"   {'✅ Process running' if process_running else '❌ Process not running'}")

    # Check 3: Health endpoint
    print("3. Checking health endpoint...")
    health_ok = check_health_endpoint()
    print(
        f"   {'✅ Health endpoint responding' if health_ok else '❌ Health endpoint not responding'}"
    )

    # Check 4: Python dependencies
    print("4. Checking Python dependencies...")
    missing_deps = check_python_dependencies()
    if not missing_deps:
        print("   ✅ All dependencies available")
    else:
        print(f"   ❌ Missing dependencies: {', '.join(missing_deps)}")

    # Check 5: File structure
    print("5. Checking file structure...")
    missing_files = check_file_structure()
    if not missing_files:
        print("   ✅ All required files present")
    else:
        print(f"   ❌ Missing files: {', '.join(missing_files)}")

    print("=" * 50)

    # Summary and recommendations
    if health_ok:
        print("🎉 Backend is healthy and running!")
        return True
    else:
        print("⚠️  Backend issues detected:")

        if not process_running:
            print("   - Backend process is not running")
            print("   → Attempting to start server...")
            if start_backend_server():
                return True

        if port_available and not process_running:
            print("   - Port is available but no process")
            print("   → Try: cd backend/python-api-service && python main_api_app.py")

        if not port_available and not process_running:
            print("   - Port is in use by another process")
            print("   → Kill existing process: pkill -f 'python.*main_api_app.py'")

        if missing_deps:
            print(f"   - Missing Python dependencies: {', '.join(missing_deps)}")
            print("   → Install with: pip install " + " ".join(missing_deps))

        if missing_files:
            print(f"   - Missing required files: {', '.join(missing_files)}")
            print("   → Check file paths and restore missing files")

        return False


def quick_fix():
    """Attempt quick fixes for common issues"""
    print("🛠️  Attempting quick fixes...")

    # Kill any existing processes
    print("   - Killing existing processes...")
    subprocess.run(["pkill", "-f", "python.*main_api_app.py"], capture_output=True)
    time.sleep(2)

    # Start server directly
    print("   - Starting backend server...")
    try:
        os.chdir("backend/python-api-service")
        process = subprocess.Popen(
            ["python3", "main_api_app.py"],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True,
        )

        # Wait and check
        for i in range(20):
            if check_health_endpoint():
                print("   ✅ Server started successfully!")
                return True
            time.sleep(1)

        print("   ❌ Server failed to start")
        return False

    except Exception as e:
        print(f"   ❌ Error: {e}")
        return False


if __name__ == "__main__":
    # Change to project root if needed
    if not Path("backend").exists():
        print("⚠️  Not in project root, attempting to find correct directory...")
        # Try to find the project root
        for parent in Path(".").absolute().parents:
            if (parent / "backend").exists():
                os.chdir(parent)
                print(f"✅ Changed to project root: {parent}")
                break

    if len(sys.argv) > 1 and sys.argv[1] == "--quick-fix":
        success = quick_fix()
    else:
        success = run_comprehensive_diagnosis()

    sys.exit(0 if success else 1)