on master help sign in
[mod] webapp.py: update engines initialization condition

Always call initialize engines except on the first run of werkzeug with the reload feature. the reload feature is activated when: * searx_debug is True (SEARX_DEBUG environment variable or settings.yml) * FLASK_APP=searx/webapp.py FLASK_ENV=development flask run (see https://flask.palletsprojects.com/en/1.1.x/cli/ ) Fix SEARX_DEBUG=0 make docs docs/admin/engines.rst : engines are initialized See https://github.com/searx/searx/issues/2204#issuecomment-701373438

commit6c39917c4d3e3d3d6ced3fc14d1c78842fec3a19 authorAlexandre Flament <alex@al-f.net> dateThu 1 Oct 2020, 14:18 (6 years ago) parent2cafc54
2 files changed+32−4
searx/webapp.py+16−4 searx/webutils.py+16
searx/webapp.py+16−4
@@ -65,7 +65,7 @@ from searx.engines import (
6565from searx.webutils import (
6666 UnicodeWriter, highlight_content, get_resources_directory,
6767 get_static_files, get_result_templates, get_themes,
68− prettify_url, new_hmac
68+ prettify_url, new_hmac, is_flask_run_cmdline
6969)
7070from searx.webadapter import get_search_query_from_webapp, get_selected_categories
7171from searx.utils import html_to_text, gen_useragent, dict_subset, match_language
@@ -114,9 +114,21 @@ app.jinja_env.lstrip_blocks = True
114114app.jinja_env.add_extension('jinja2.ext.loopcontrols')
115115app.secret_key = settings['server']['secret_key']
116116
117−if not searx_debug \
118− or os.environ.get("WERKZEUG_RUN_MAIN") == "true" \
119− or os.environ.get('UWSGI_ORIGINAL_PROC_NAME') is not None:
117+# see https://flask.palletsprojects.com/en/1.1.x/cli/
118+# True if "FLASK_APP=searx/webapp.py FLASK_ENV=development flask run"
119+flask_run_development = \
120+ os.environ.get("FLASK_APP") is not None\
121+ and os.environ.get("FLASK_ENV") == 'development'\
122+ and is_flask_run_cmdline()
123+
124+# True if reload feature is activated of werkzeug, False otherwise (including uwsgi, etc..)
125+# __name__ != "__main__" if searx.webapp is imported (make test, make docs, uwsgi...)
126+# see run() at the end of this file : searx_debug activates the reload feature.
127+werkzeug_reloader = flask_run_development or (searx_debug and __name__ == "__main__")
128+
129+# initialize the engines except on the first run of the werkzeug server.
130+if not werkzeug_reloader\
131+ or (werkzeug_reloader and os.environ.get("WERKZEUG_RUN_MAIN") == "true"):
120132 initialize_engines(settings['engines'])
121133
122134babel = Babel(app)
searx/webutils.py+16
@@ -4,6 +4,7 @@ import csv
44import hashlib
55import hmac
66import re
7+import inspect
78
89from io import StringIO
910from codecs import getincrementalencoder
@@ -125,3 +126,18 @@ def highlight_content(content, query):
125126 content, flags=re.I | re.U)
126127
127128 return content
129+
130+
131+def is_flask_run_cmdline():
132+ """Check if the application was started using "flask run" command line
133+
134+ Inspect the callstack.
135+ See https://github.com/pallets/flask/blob/master/src/flask/__main__.py
136+
137+ Returns:
138+ bool: True if the application was started using "flask run".
139+ """
140+ frames = inspect.stack()
141+ if len(frames) < 2:
142+ return False
143+ return frames[-2].filename.endswith('flask/cli.py')