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
|
||||
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
|
||||
@app.context_processor
|
||||
@@ -206,34 +206,34 @@ def deactivate_module(module_key):
|
||||
@app.route('/admin/restart', methods=['POST'])
|
||||
@login_required
|
||||
def restart_server():
|
||||
"""Restart the Flask server"""
|
||||
if session.get('role') not in ['owner', 'admin']:
|
||||
return jsonify({'success': False, 'message': 'Access denied'}), 403
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
try:
|
||||
print("\n🔄 Server restart requested by admin...")
|
||||
import signal
|
||||
import os
|
||||
import sys
|
||||
|
||||
# Return response first
|
||||
response = jsonify({'success': True, 'message': 'Server restarting...'})
|
||||
|
||||
# Schedule restart after response is sent
|
||||
def restart():
|
||||
import time
|
||||
time.sleep(0.5) # Give time for response to send
|
||||
# Check if running under Gunicorn
|
||||
if 'gunicorn' in os.environ.get('SERVER_SOFTWARE', ''):
|
||||
# Gunicorn: Send HUP to master for graceful reload
|
||||
master_pid = os.getppid()
|
||||
os.kill(master_pid, signal.SIGHUP)
|
||||
return jsonify({'success': True, 'message': 'Server reloading...'})
|
||||
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:
|
||||
return jsonify({'success': False, 'message': f'Restart failed: {str(e)}'})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user