Skip to content

Commit ada16fe

Browse files
authored
Create shutdown.py
1 parent 3161ebb commit ada16fe

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

backend/api/shutdown.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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)

0 commit comments

Comments
 (0)