66import sys
77import logging
88import re
9+ import shutil
10+ import time
11+ import requests
12+ import json
13+ import traceback
914
1015from utils .api_client import download_file_from_url , download_and_extract_zip
1116from utils .java_manager import JavaManager
@@ -371,6 +376,7 @@ def _get_start_command(self):
371376 return None , None
372377
373378 # Prepare environment for startup (injecting JAVA_HOME and PATH)
379+ logging .info (f"Handler: Preparing environment. Java Path: { java_path } " )
374380 custom_env = os .environ .copy ()
375381 if java_path != "java" :
376382 # Extract installation root (assuming path/to/java-21/bin/java.exe)
@@ -380,15 +386,17 @@ def _get_start_command(self):
380386 # Prepend to PATH so scripts find this 'java' first
381387 path_sep = ';' if sys .platform == "win32" else ':'
382388 custom_env ["PATH" ] = f"{ java_bin_dir } { path_sep } { custom_env .get ('PATH' , '' )} "
383- logger .info (f"Injecting Java environment: JAVA_HOME={ java_root } " )
389+ logging .info (f"Handler: Injected Environment - JAVA_HOME={ java_root } " )
384390
385391 run_script = None
386392
387393 # Universal check for startup scripts
388394 if sys .platform == "win32" :
389395 script_path = os .path .join (self .server_path , 'run.bat' )
396+ logging .info (f"Handler: Checking for run.bat at { script_path } " )
390397 if os .path .exists (script_path ):
391398 run_script = script_path
399+ logging .info ("Handler: run.bat FOUND" )
392400 else : # For macOS and Linux
393401 script_path = os .path .join (self .server_path , 'run.sh' )
394402 if os .path .exists (script_path ):
@@ -464,20 +472,38 @@ def _get_start_command(self):
464472 return command , custom_env
465473
466474 def _run_server (self , command , env ):
475+ logging .info (f"Handler: Background thread started. Command: { command } " )
467476 stdout_thread = None
468477 stderr_thread = None
469478 try :
470- self .server_process = subprocess .Popen (command , cwd = self .server_path , stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , text = True , bufsize = 1 , universal_newlines = True , creationflags = subprocess .CREATE_NO_WINDOW if sys .platform == "win32" else 0 , env = env )
479+ logging .info (f"Handler: Launching process in { self .server_path } ..." )
480+ self .server_process = subprocess .Popen (
481+ command ,
482+ cwd = self .server_path ,
483+ stdout = subprocess .PIPE ,
484+ stderr = subprocess .PIPE ,
485+ stdin = subprocess .PIPE ,
486+ text = True ,
487+ bufsize = 1 ,
488+ universal_newlines = True ,
489+ creationflags = subprocess .CREATE_NO_WINDOW if sys .platform == "win32" else 0 ,
490+ env = env
491+ )
492+ logging .info (f"Handler: Process spawned with PID { self .server_process .pid } " )
471493
472494 stdout_thread = threading .Thread (target = self ._read_output , args = (self .server_process .stdout , "normal" ), daemon = True )
473495 stderr_thread = threading .Thread (target = self ._read_output , args = (self .server_process .stderr , "error" ), daemon = True )
474496 stdout_thread .start ()
475497 stderr_thread .start ()
476498
477499 self .server_process .wait ()
500+ logging .info (f"Handler: Process exited with code { self .server_process .returncode } " )
478501 except FileNotFoundError :
502+ logging .error ("Handler: Java or Script NOT FOUND during spawn" )
479503 self ._log ("Error: 'java' command not found. Is Java installed and in your PATH?\n " , "error" )
480504 except Exception as e :
505+ error_details = traceback .format_exc ()
506+ logging .error (f"Handler: CRASH in _run_server thread:\n { error_details } " )
481507 self ._log (f"Server start failed: { e } \n " , "error" )
482508 finally :
483509 # Esperar a que los hilos de salida terminen de procesar los últimos logs
0 commit comments