Force startup initialization to happen early.

master
Pacman Ghost 4 years ago
parent 6b44b8fd33
commit 8ec19b2f94
  1. 32
      run_server.py

@ -2,8 +2,13 @@
""" Run the Flask backend server. """
import os
import threading
import urllib.request
import time
import glob
from flask import url_for
# ---------------------------------------------------------------------
# monitor extra files for changes
@ -21,8 +26,33 @@ for fspec in ["config","static","templates"] :
files = glob.glob( fspec )
extra_files.extend( files )
# run the server
# initialize
from asl_articles import app
# FUDGE! Startup can take some time (e.g. because we have to build the search index over a large database),
# and since we do that on first request, it's annoying to have started the server up, if we don't do that
# first request immediately, the server sits there idling, when it could be doing that startup initialization,
# and we then have to wait when we eventually do that first request.
# We fix this by doing the first request ourself here (something harmless).
def _force_init():
time.sleep( 5 )
try:
# figoure out the URL for the request we're going to make
with app.test_request_context() as req:
url = url_for( "ping" )
host = req.request.host_url
# FUDGE! There doesn't seem to be a way to get the port number Flask is listening on :-/
port = app.config.get( "FLASK_PORT_NO", 5000 )
if host.endswith( "/" ):
host = host[:-1]
url = "{}:{}{}".format( host, port, url )
# make the request
_ = urllib.request.urlopen( url ).read()
except Exception as ex: #pylint: disable=broad-except
print( "WARNING: Startup ping failed: {}".format( ex ) )
threading.Thread( target=_force_init ).start()
# run the server
app.run(
host = app.config.get( "FLASK_HOST", "localhost" ),
port = app.config.get( "FLASK_PORT_NO" ),

Loading…
Cancel
Save