anicove / run.py
mwask's picture
server restart
48397fc
Raw
History Blame Contribute Delete
1.17 kB
import os
from api.app import create_app
import threading
import signal
app = create_app()
def _restart_process():
print("Scheduled restart: exiting process to allow external restart.")
os._exit(0)
def _schedule_restart(hours: int):
if hours > 0:
timer = threading.Timer(hours * 3600, _restart_process)
timer.daemon = True
timer.start()
if __name__ == "__main__":
import sys
host = os.environ.get("HOST", "0.0.0.0")
port = int(os.environ.get("PORT", 5000))
debug = os.environ.get("FLASK_DEBUG", "0") == "1"
# Schedule automatic restart after 5 hours if RESTART_AFTER_HOURS not set
restart_hours = int(os.getenv("RESTART_AFTER_HOURS", "5"))
_schedule_restart(restart_hours)
try:
app.run(host=host, port=port, debug=debug, use_reloader=False, threaded=True)
except KeyboardInterrupt:
print("Server gracefully stopped.")
except OSError as e:
if getattr(e, 'winerror', None) == 10038:
print("Server gracefully stopped (socket released).")
else:
raise
except BaseException:
sys.exit(0)