diff --git a/.env.demo b/.env.demo index b6bd568b7..29bc72f83 100644 --- a/.env.demo +++ b/.env.demo @@ -40,7 +40,6 @@ LAYMAN_SKIP_REDIS_LOADING=false LAYMAN_SERVER_NAME=layman:8000 # internal settings -LAYMAN_PRELOAD_MODULES=false LAYMAN_TIMGEN_URL=http://hslayers:8080/ LAYMAN_CLIENT_URL=http://layman_client:3000/client/ diff --git a/.env.dev b/.env.dev index f7ec2ebc1..d6ee789bc 100644 --- a/.env.dev +++ b/.env.dev @@ -41,7 +41,6 @@ LAYMAN_SKIP_REDIS_LOADING=false LAYMAN_SERVER_NAME=layman_dev:8000 # internal settings -LAYMAN_PRELOAD_MODULES=true LAYMAN_TIMGEN_URL=http://hslayers:8080/ LAYMAN_CLIENT_URL=http://layman_client:3000/client/ diff --git a/.env.test b/.env.test index 29848ea9c..99918a892 100644 --- a/.env.test +++ b/.env.test @@ -41,7 +41,6 @@ LAYMAN_SKIP_REDIS_LOADING=false LAYMAN_SERVER_NAME=layman_test_run_1:8000 # internal settings -LAYMAN_PRELOAD_MODULES=false LAYMAN_TIMGEN_URL=http://hslayers:8080/ LAYMAN_CLIENT_URL=http://layman_client_test:3000/client/ diff --git a/docker-compose.demo.yml b/docker-compose.demo.yml index 4be639252..d9edfaa7d 100644 --- a/docker-compose.demo.yml +++ b/docker-compose.demo.yml @@ -10,7 +10,7 @@ services: - UID_GID image: layman:latest user: ${UID_GID} - command: bash -c "python3 src/wait_for_deps.py && cd src && gunicorn -w 1 -b 0.0.0.0:8000 layman:app" + command: bash -c "python3 src/layman_flush_redis.py && python3 src/wait_for_deps.py && cd src && gunicorn -w 1 -b 0.0.0.0:8000 layman:app" env_file: - .env # ports: diff --git a/src/layman/__init__.py b/src/layman/__init__.py index 2a78324bc..fc57a64e9 100644 --- a/src/layman/__init__.py +++ b/src/layman/__init__.py @@ -8,10 +8,6 @@ app = Flask(__name__) app.secret_key = os.environ['FLASK_SECRET_KEY'] -if os.getenv('LAYMAN_SKIP_REDIS_LOADING', 'false').lower() != 'true': - app.logger.info(f'Flush Redis database.') - settings.LAYMAN_REDIS.flushdb() - from .http import LaymanError from .make_celery import make_celery @@ -26,8 +22,10 @@ app.register_blueprint(current_user_bp, url_prefix='/rest/current-user') # load UUIDs only once -if os.getenv('LAYMAN_SKIP_REDIS_LOADING', 'false').lower() != 'true': - os.environ['LAYMAN_SKIP_REDIS_LOADING'] = 'true' +REDIS_LOADED_KEY = f"{__name__}:REDIS_LOADED" +if settings.LAYMAN_REDIS.get(REDIS_LOADED_KEY) is None: + settings.LAYMAN_REDIS.set(REDIS_LOADED_KEY, 'true') + app.logger.info(f'Loading Redis database') with app.app_context(): from .uuid import import_uuids_to_redis import_uuids_to_redis() @@ -35,16 +33,6 @@ import_authn_to_redis() -if settings.LAYMAN_PRELOAD_MODULES: - # because Flask with reloader reloads when *.pyc files is created - app.logger.info(f'Preload auth* modules.') - with app.app_context(): - from .authn.util import get_authn_modules - get_authn_modules() - from .authz.util import get_authz_module - get_authz_module() - - @app.route('/') def index(): return redirect(settings.LAYMAN_CLIENT_PUBLIC_URL) diff --git a/src/layman/authn/oauth2/util.py b/src/layman/authn/oauth2/util.py index 232c9e8fe..a3ab71d7d 100644 --- a/src/layman/authn/oauth2/util.py +++ b/src/layman/authn/oauth2/util.py @@ -101,12 +101,6 @@ def _get_provider_modules(): return current_app.config[key] -if current_app and current_app.config['DEBUG']: - # because Flask with reloader reloads when *.pyc files is created - current_app.logger.info(f'Preload oauth2 provider modules.') - _get_provider_modules() - - def _get_provider_module(): key = FLASK_PROVIDER_KEY return g.get(key) diff --git a/src/layman_flush_redis.py b/src/layman_flush_redis.py new file mode 100644 index 000000000..a1511cd18 --- /dev/null +++ b/src/layman_flush_redis.py @@ -0,0 +1,25 @@ +import traceback + +import importlib +import os +import sys +import time + + +settings = importlib.import_module(os.environ['LAYMAN_SETTINGS_MODULE']) + +ATTEMPT_INTERVAL = 2 +MAX_ATTEMPTS = 60 + + +def main(): + + if os.getenv('LAYMAN_SKIP_REDIS_LOADING', 'false').lower() != 'true': + print('Flushing Redis DB') + settings.LAYMAN_REDIS.flushdb() + else: + print('Not flushing Redis DB Redis DB') + + +if __name__ == "__main__": + main() diff --git a/src/layman_settings_demo.py b/src/layman_settings_demo.py index 8554d8762..1b8c10777 100644 --- a/src/layman_settings_demo.py +++ b/src/layman_settings_demo.py @@ -123,7 +123,6 @@ LAYMAN_REDIS = redis.Redis.from_url(LAYMAN_REDIS_URL, encoding="utf-8", decode_responses=True) -LAYMAN_PRELOAD_MODULES = os.getenv('LAYMAN_PRELOAD_MODULES', 'false').lower() == 'true' LAYMAN_TIMGEN_URL = os.environ['LAYMAN_TIMGEN_URL'] LAYMAN_CLIENT_URL = os.environ['LAYMAN_CLIENT_URL'] LAYMAN_CLIENT_PUBLIC_URL = os.getenv('LAYMAN_CLIENT_PUBLIC_URL', None) diff --git a/src/layman_settings_dev.py b/src/layman_settings_dev.py index 8554d8762..1b8c10777 100644 --- a/src/layman_settings_dev.py +++ b/src/layman_settings_dev.py @@ -123,7 +123,6 @@ LAYMAN_REDIS = redis.Redis.from_url(LAYMAN_REDIS_URL, encoding="utf-8", decode_responses=True) -LAYMAN_PRELOAD_MODULES = os.getenv('LAYMAN_PRELOAD_MODULES', 'false').lower() == 'true' LAYMAN_TIMGEN_URL = os.environ['LAYMAN_TIMGEN_URL'] LAYMAN_CLIENT_URL = os.environ['LAYMAN_CLIENT_URL'] LAYMAN_CLIENT_PUBLIC_URL = os.getenv('LAYMAN_CLIENT_PUBLIC_URL', None) diff --git a/src/layman_settings_test.py b/src/layman_settings_test.py index 8554d8762..1b8c10777 100644 --- a/src/layman_settings_test.py +++ b/src/layman_settings_test.py @@ -123,7 +123,6 @@ LAYMAN_REDIS = redis.Redis.from_url(LAYMAN_REDIS_URL, encoding="utf-8", decode_responses=True) -LAYMAN_PRELOAD_MODULES = os.getenv('LAYMAN_PRELOAD_MODULES', 'false').lower() == 'true' LAYMAN_TIMGEN_URL = os.environ['LAYMAN_TIMGEN_URL'] LAYMAN_CLIENT_URL = os.environ['LAYMAN_CLIENT_URL'] LAYMAN_CLIENT_PUBLIC_URL = os.getenv('LAYMAN_CLIENT_PUBLIC_URL', None) diff --git a/start.sh b/start.sh index 2c36a4fae..10d94a1e6 100644 --- a/start.sh +++ b/start.sh @@ -2,4 +2,9 @@ bash src/clear-python-cache.sh -python3 src/wait_for_deps.py && flask run --host=0.0.0.0 --port=8000 "$@" \ No newline at end of file + +# when using non-root user in docker container, flask reloader is not working +#python3 src/wait_for_deps.py && flask run --host=0.0.0.0 --port=8000 "$@" + +# therefore use watchmedo +python3 src/layman_flush_redis.py && python3 src/wait_for_deps.py && watchmedo auto-restart -d ./src -p '*.py' --recursive -- flask run --host=0.0.0.0 --port=8000 --no-reload \ No newline at end of file