Skip to content

Commit

Permalink
Re-enable hot reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
jirik committed Oct 31, 2019
1 parent 6332618 commit eaacac8
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 30 deletions.
1 change: 0 additions & 1 deletion .env.demo
Original file line number Diff line number Diff line change
Expand Up @@ -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/

Expand Down
1 change: 0 additions & 1 deletion .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -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/

Expand Down
1 change: 0 additions & 1 deletion .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -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/

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.demo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 4 additions & 16 deletions src/layman/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,25 +22,17 @@
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()
from .authn.redis import import_authn_to_redis
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)
Expand Down
6 changes: 0 additions & 6 deletions src/layman/authn/oauth2/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions src/layman_flush_redis.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 0 additions & 1 deletion src/layman_settings_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/layman_settings_dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/layman_settings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$@"

# 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

0 comments on commit eaacac8

Please sign in to comment.