File size: 10,534 Bytes
383cb38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env python3
"""
ATOM Main Application Monitor

This script monitors the main application startup and provides diagnostics
when the application gets stuck during initialization.
"""

from datetime import datetime, timedelta
import logging
import os
import subprocess
import sys
import threading
import time
import requests

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
    handlers=[
        logging.FileHandler("main_app_monitor.log"),
        logging.StreamHandler(sys.stdout),
    ],
)
logger = logging.getLogger(__name__)


class MainAppMonitor:
    """Monitor for ATOM main application startup and health"""

    def __init__(self, port=5058, timeout_seconds=30, check_interval=5):
        self.port = port
        self.timeout_seconds = timeout_seconds
        self.check_interval = check_interval
        self.start_time = None
        self.process = None
        self.is_running = False

    def start_main_app(self):
        """Start the main application"""
        logger.info("🚀 Starting ATOM Main Application...")

        try:
            # Change to backend directory
            backend_dir = os.path.join(
                os.path.dirname(__file__), "backend", "python-api-service"
            )
            os.chdir(backend_dir)

            # Start the main application
            self.process = subprocess.Popen(
                [sys.executable, "main_api_app.py"],
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                text=True,
            )

            self.start_time = datetime.now()
            self.is_running = True
            logger.info(f"Main application started with PID: {self.process.pid}")

            # Start output monitoring in separate thread
            output_thread = threading.Thread(target=self._monitor_output)
            output_thread.daemon = True
            output_thread.start()

            return True

        except Exception as e:
            logger.error(f"Failed to start main application: {e}")
            return False

    def _monitor_output(self):
        """Monitor application output in real-time"""
        try:
            while self.process and self.process.poll() is None:
                # Read stdout
                stdout_line = self.process.stdout.readline()
                if stdout_line:
                    logger.info(f"[APP] {stdout_line.strip()}")

                # Read stderr
                stderr_line = self.process.stderr.readline()
                if stderr_line:
                    logger.warning(f"[APP-ERROR] {stderr_line.strip()}")

                time.sleep(0.1)

        except Exception as e:
            logger.error(f"Error monitoring application output: {e}")

    def check_health(self):
        """Check if the application is healthy and responding"""
        try:
            response = requests.get(f"http://localhost:{self.port}/healthz", timeout=5)
            if response.status_code == 200:
                data = response.json()
                logger.info(f"✅ Application healthy: {data}")
                return True
            else:
                logger.warning(
                    f"⚠️  Application responded with status: {response.status_code}"
                )
                return False

        except requests.exceptions.RequestException as e:
            logger.warning(f"❌ Application not responding: {e}")
            return False

    def diagnose_stuck_issue(self):
        """Diagnose why the application might be stuck"""
        logger.info("🔍 Running diagnostics...")

        diagnostics = {
            "port_in_use": self._check_port_in_use(),
            "import_issues": self._check_import_issues(),
            "database_issues": self._check_database_issues(),
            "blueprint_registration": self._check_blueprint_registration(),
            "process_status": self._check_process_status(),
        }

        # Print diagnostic summary
        logger.info("📊 DIAGNOSTIC SUMMARY:")
        for check, result in diagnostics.items():
            status = "✅" if result.get("healthy", False) else "❌"
            logger.info(f"  {status} {check}: {result.get('message', 'Unknown')}")

        return diagnostics

    def _check_port_in_use(self):
        """Check if port is already in use"""
        try:
            import socket

            with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
                result = s.connect_ex(("localhost", self.port))
                return {
                    "healthy": result != 0,
                    "message": f"Port {self.port} is {'in use' if result == 0 else 'available'}",
                }
        except Exception as e:
            return {"healthy": False, "message": f"Error checking port: {e}"}

    def _check_import_issues(self):
        """Check for common import issues"""
        try:
            # Test basic imports
            import sqlite3
            import bcrypt
            import flask
            import jwt

            return {"healthy": True, "message": "All required imports available"}
        except ImportError as e:
            return {"healthy": False, "message": f"Missing import: {e}"}

    def _check_database_issues(self):
        """Check for database connectivity issues"""
        try:
            import sqlite3

            # Check if SQLite database can be created
            test_db_path = "/tmp/atom_test.db"
            conn = sqlite3.connect(test_db_path)
            conn.execute("CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY)")
            conn.execute("DROP TABLE IF EXISTS test")
            conn.close()
            os.unlink(test_db_path)

            return {"healthy": True, "message": "SQLite database operations working"}
        except Exception as e:
            return {"healthy": False, "message": f"Database error: {e}"}

    def _check_blueprint_registration(self):
        """Check blueprint registration issues"""
        try:
            # This would require importing the actual app, but we can check file existence
            blueprint_files = [
                "search_routes.py",
                "calendar_handler.py",
                "task_handler.py",
                "message_handler.py",
                "user_auth_api.py",
            ]

            missing_files = []
            for file in blueprint_files:
                if not os.path.exists(file):
                    missing_files.append(file)

            if missing_files:
                return {
                    "healthy": False,
                    "message": f"Missing blueprint files: {', '.join(missing_files)}",
                }
            else:
                return {"healthy": True, "message": "All blueprint files present"}

        except Exception as e:
            return {"healthy": False, "message": f"Error checking blueprints: {e}"}

    def _check_process_status(self):
        """Check the status of the main application process"""
        if not self.process:
            return {"healthy": False, "message": "No process running"}

        return_code = self.process.poll()
        if return_code is None:
            return {"healthy": True, "message": "Process is running"}
        else:
            return {
                "healthy": False,
                "message": f"Process exited with code: {return_code}",
            }

    def wait_for_startup(self):
        """Wait for application to start up successfully"""
        logger.info(
            f"⏳ Waiting for application to start (timeout: {self.timeout_seconds}s)..."
        )

        start_wait = datetime.now()
        while (datetime.now() - start_wait).seconds < self.timeout_seconds:
            if self.check_health():
                logger.info("🎉 Application started successfully!")
                return True

            # Check if process is still running
            if self.process and self.process.poll() is not None:
                logger.error("💥 Application process died during startup")
                # Get any error output
                stdout, stderr = self.process.communicate()
                if stderr:
                    logger.error(f"Application stderr: {stderr}")
                return False

            time.sleep(self.check_interval)

        # If we get here, the application is stuck
        logger.error("⏰ Application startup timeout - application appears stuck")
        self.diagnose_stuck_issue()
        return False

    def stop(self):
        """Stop the monitoring and application"""
        logger.info("🛑 Stopping application and monitor...")
        self.is_running = False

        if self.process:
            self.process.terminate()
            try:
                self.process.wait(timeout=10)
                logger.info("✅ Application stopped gracefully")
            except subprocess.TimeoutExpired:
                logger.warning("⚠️  Application didn't stop gracefully, forcing...")
                self.process.kill()

        # Kill any remaining processes on the port
        try:
            subprocess.run(["lsof", "-ti", f":{self.port}"], capture_output=True)
            subprocess.run(["pkill", "-f", "python.*main_api_app"], capture_output=True)
        except:
            pass


def main():
    """Main monitoring function"""
    monitor = MainAppMonitor()

    try:
        # Start the application
        if not monitor.start_main_app():
            logger.error("Failed to start application")
            return 1

        # Wait for startup
        if monitor.wait_for_startup():
            logger.info("🚀 ATOM Main Application is running and healthy!")
            logger.info(f"🌐 Access at: http://localhost:{monitor.port}")
            logger.info("Press Ctrl+C to stop the application")

            # Keep monitoring while running
            while monitor.is_running:
                time.sleep(10)
                if not monitor.check_health():
                    logger.error("Application health check failed!")
                    break

        else:
            logger.error("Application failed to start properly")
            return 1

    except KeyboardInterrupt:
        logger.info("Received interrupt signal")
    except Exception as e:
        logger.error(f"Monitor error: {e}")
        return 1
    finally:
        monitor.stop()

    return 0


if __name__ == "__main__":
    sys.exit(main())