File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change 1+ import signal
2+ import sys
3+ from flask import Blueprint , request , jsonify
4+ from submodule .rs_logger import log
5+
6+ shutdown_api = Blueprint ('shutdown_api' , __name__ )
7+
8+ def shutdown_server ():
9+ """Function to gracefully shut down the server."""
10+ log ("Shutting down server..." )
11+ func = request .environ .get ('werkzeug.server.shutdown' )
12+ if func is not None :
13+ func ()
14+ else :
15+ log ("Server shutdown function not found." , level = "ERROR" )
16+ sys .exit (0 )
17+
18+ def signal_handler (signal_received , frame ):
19+ """Handles signals for graceful shutdown."""
20+ log (f"Signal { signal_received } received, initiating shutdown..." )
21+ shutdown_server ()
22+
23+ @shutdown_api .route ('/shutdown' , methods = ['POST' ])
24+ def trigger_shutdown ():
25+ """API endpoint to trigger a server shutdown."""
26+ log ("Shutdown API called by user request." )
27+ shutdown_server ()
28+ return jsonify ({"message" : "Server is shutting down..." }), 200
29+
30+ # Register the signal handler for SIGINT and SIGTERM
31+ signal .signal (signal .SIGINT , signal_handler )
32+ signal .signal (signal .SIGTERM , signal_handler )
You can’t perform that action at this time.
0 commit comments