File size: 1,900 Bytes
15bdce2
a9b4f5e
 
 
 
15bdce2
a9b4f5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15bdce2
a9b4f5e
15bdce2
a9b4f5e
 
15bdce2
a9b4f5e
 
 
15bdce2
a9b4f5e
 
 
15bdce2
a9b4f5e
 
15bdce2
a9b4f5e
 
 
15bdce2
 
a9b4f5e
 
 
 
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
import os
import shutil
from web_app import WebChatbotApp
import traceback
import sys
import logging
import signal

# Configuración del registro
logging.basicConfig(
    filename='app.log',  # Nombre del archivo de registro
    level=logging.DEBUG,  # Nivel de registro
    format='%(asctime)s - %(levelname)s - %(message)s',
    encoding='utf-8'
)

def clean_pycache():
    """Eliminar el directorio __pycache__ si existe."""
    pycache_dir = "__pycache__"
    if os.path.exists(pycache_dir):
        shutil.rmtree(pycache_dir)
        logging.info(f"Limpieza de {pycache_dir} completada")

def setup_directories():
    """Crear directorios necesarios si no existen."""
    directories = ['temp', 'sessions', 'logs', 'flask_session']
    for directory in directories:
        os.makedirs(directory, exist_ok=True)
        logging.info(f"Directorio {directory} verificado")

def signal_handler(signum, frame):
    logging.info("Señal de terminación recibida. Cerrando servidor...")
    sys.exit(0)

if __name__ == "__main__":
    try:
        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)
        
        # Limpiar caché y crear directorios
        clean_pycache()
        setup_directories()
        
        # Iniciar aplicación
        logging.info("Iniciando aplicación...")
        app = WebChatbotApp()
        
        port = int(os.environ.get('PORT', 5000))
        host = os.environ.get('HOST', '127.0.0.1')
        
        # Iniciar servidor
        logging.info(f"Iniciando servidor en {host}:{port}...")
        app.run(host=host, port=port, debug=False, use_tunnel=True)
        
    except Exception as e:
        logging.error(f"Error crítico al iniciar la aplicación: {str(e)}")
        logging.error(f"Detalles del error: {traceback.format_exc()}")
        print(f"\n❌ Error crítico: {str(e)}")
        sys.exit(1)