refactor api_server and TelegramClient: remove force_exit function and replace with sys.exit for process termination, update restart logic to use SIGTERM instead of SIGKILL for graceful shutdown
This commit is contained in:
+2
-21
@@ -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"""
|
||||
|
||||
Reference in New Issue
Block a user