diff --git a/api_server.py b/api_server.py index fd7eb4d..140ec40 100644 --- a/api_server.py +++ b/api_server.py @@ -25,7 +25,6 @@ asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) from starlette.middleware.base import BaseHTTPMiddleware from starlette.background import BackgroundTask import json_repair -import signal import sys import magic @@ -109,24 +108,6 @@ if __name__ == "__main__": # Log uvloop status logger.info(" uvloop: enabled (asyncio speedup active)") - # Add a function to forcefully kill the process - def force_exit(exit_code=1): - """Force exit the process to trigger Docker container restart""" - logger.critical(f"Force exiting process with code {exit_code}") - try: - # Try to kill the process with SIGKILL - os.kill(os.getpid(), signal.SIGKILL) - except: - # If that fails, use os._exit - os._exit(exit_code) - - # Add SIGTERM signal handler for proper termination - def handle_sigterm(signum, frame): - logger.warning("Received SIGTERM signal, shutting down forcefully") - force_exit(0) # Exit with code 0 for graceful shutdown - - signal.signal(signal.SIGTERM, handle_sigterm) - try: uvicorn.run( "api_server:app", @@ -138,10 +119,10 @@ if __name__ == "__main__": except OSError as e: if "[Errno 98] Address already in use" in str(e): logger.critical(f"Port {Config['api_port']} is already in use. Exiting with code 1 to trigger Docker restart.") - force_exit(1) + sys.exit(1) else: logger.critical(f"Failed to start server: {str(e)}") - force_exit(1) + sys.exit(1) async def find_file_id_in_message(message: Message, file_unique_id: str) -> Union[str, None]: """Find file_id by checking all possible media types in message""" diff --git a/telegram_client.py b/telegram_client.py index bd0ddcf..32ff1d5 100644 --- a/telegram_client.py +++ b/telegram_client.py @@ -72,12 +72,12 @@ class TelegramClient: raise def _restart_app(self): - """Restarts the application by forcefully killing the process to trigger Docker container restart""" - logger.warning("connection_handler: restarting application by forceful termination") + """Restarts the application by sending SIGTERM to the process""" + logger.warning("connection_handler: restarting application by sending SIGTERM") try: - # Forcefully kill the process using SIGKILL to ensure immediate termination - logger.critical("connection_handler: sending SIGKILL to self") - os.kill(os.getpid(), signal.SIGKILL) + # Use SIGTERM for proper Docker container restart + logger.critical("connection_handler: sending SIGTERM signal") + os.kill(os.getpid(), signal.SIGTERM) except Exception as e: logger.error(f"connection_handler: error during restart: {str(e)}") # Emergency termination