v0.17.3 - Updated the way Gunicorn restarts the server

This commit is contained in:
Javier
2026-02-08 02:08:58 -06:00
parent b41e64be64
commit ea8551043f

26
app.py
View File

@@ -28,7 +28,7 @@ app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hours=1)
# 1. Define the version # 1. Define the version
APP_VERSION = '0.17.2' # Bumped version for modular architecture APP_VERSION = '0.17.3' # Bumped version for modular architecture
# 2. Inject it into all templates automatically # 2. Inject it into all templates automatically
@app.context_processor @app.context_processor
@@ -206,24 +206,25 @@ def deactivate_module(module_key):
@app.route('/admin/restart', methods=['POST']) @app.route('/admin/restart', methods=['POST'])
@login_required @login_required
def restart_server(): def restart_server():
"""Restart the Flask server"""
if session.get('role') not in ['owner', 'admin']: if session.get('role') not in ['owner', 'admin']:
return jsonify({'success': False, 'message': 'Access denied'}), 403 return jsonify({'success': False, 'message': 'Access denied'}), 403
try:
import signal
import os import os
import sys import sys
try: # Check if running under Gunicorn
print("\n🔄 Server restart requested by admin...") if 'gunicorn' in os.environ.get('SERVER_SOFTWARE', ''):
# Gunicorn: Send HUP to master for graceful reload
# Return response first master_pid = os.getppid()
response = jsonify({'success': True, 'message': 'Server restarting...'}) os.kill(master_pid, signal.SIGHUP)
return jsonify({'success': True, 'message': 'Server reloading...'})
# Schedule restart after response is sent else:
# Flask dev server: Restart process
def restart(): def restart():
import time import time
time.sleep(0.5) # Give time for response to send time.sleep(0.5)
if os.name == 'nt': # Windows if os.name == 'nt': # Windows
os.execv(sys.executable, ['python'] + sys.argv) os.execv(sys.executable, ['python'] + sys.argv)
else: # Linux/Mac else: # Linux/Mac
@@ -231,8 +232,7 @@ def restart_server():
from threading import Thread from threading import Thread
Thread(target=restart).start() Thread(target=restart).start()
return jsonify({'success': True, 'message': 'Server restarting...'})
return response
except Exception as e: except Exception as e:
return jsonify({'success': False, 'message': f'Restart failed: {str(e)}'}) return jsonify({'success': False, 'message': f'Restart failed: {str(e)}'})