v0.17.3 - Updated the way Gunicorn restarts the server
This commit is contained in:
46
app.py
46
app.py
@@ -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,34 +206,34 @@ 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
|
||||||
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print("\n🔄 Server restart requested by admin...")
|
import signal
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
# Return response first
|
# Check if running under Gunicorn
|
||||||
response = jsonify({'success': True, 'message': 'Server restarting...'})
|
if 'gunicorn' in os.environ.get('SERVER_SOFTWARE', ''):
|
||||||
|
# Gunicorn: Send HUP to master for graceful reload
|
||||||
# Schedule restart after response is sent
|
master_pid = os.getppid()
|
||||||
def restart():
|
os.kill(master_pid, signal.SIGHUP)
|
||||||
import time
|
return jsonify({'success': True, 'message': 'Server reloading...'})
|
||||||
time.sleep(0.5) # Give time for response to send
|
else:
|
||||||
|
# Flask dev server: Restart process
|
||||||
|
def restart():
|
||||||
|
import time
|
||||||
|
time.sleep(0.5)
|
||||||
|
if os.name == 'nt': # Windows
|
||||||
|
os.execv(sys.executable, ['python'] + sys.argv)
|
||||||
|
else: # Linux/Mac
|
||||||
|
os.execv(sys.executable, [sys.executable] + sys.argv)
|
||||||
|
|
||||||
|
from threading import Thread
|
||||||
|
Thread(target=restart).start()
|
||||||
|
return jsonify({'success': True, 'message': 'Server restarting...'})
|
||||||
|
|
||||||
if os.name == 'nt': # Windows
|
|
||||||
os.execv(sys.executable, ['python'] + sys.argv)
|
|
||||||
else: # Linux/Mac
|
|
||||||
os.execv(sys.executable, [sys.executable] + sys.argv)
|
|
||||||
|
|
||||||
from threading import Thread
|
|
||||||
Thread(target=restart).start()
|
|
||||||
|
|
||||||
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)}'})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user