Skip to content

Commit

Permalink
fix failure of caching in places and placeURI
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick.qian committed Feb 22, 2023
1 parent ebe9c16 commit fad36ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
11 changes: 6 additions & 5 deletions source/jormungandr/jormungandr/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,18 @@ def cache_get_key(token):
current_app.config[str('MEMORY_CACHE_CONFIGURATION')].get(str('TIMEOUT_AUTHENTICATION'), 30)
)
@cache.memoize(current_app.config[str('CACHE_CONFIGURATION')].get(str('TIMEOUT_AUTHENTICATION'), 300))
def get_all_available_instances(user, exclude_backend=None):
def get_all_available_instances_names(user, exclude_backend=None):
"""
get the list of instances that a user can use (for the autocomplete apis)
if Jormungandr has no authentication set (or no database), the user can use all the instances
else we use the jormungandr db to fetch the list (based on the user's authorization)
Note: only users with access to free instances can use global /places
"""
if current_app.config.get('PUBLIC', False) or current_app.config.get('DISABLE_DATABASE', False):
from jormungandr import i_manager
from jormungandr import i_manager

return list(i_manager.instances.values())
if current_app.config.get('PUBLIC', False) or current_app.config.get('DISABLE_DATABASE', False):
return [key for key in i_manager.instances]

if not user:
# for not-public navitia a user is mandatory
Expand All @@ -256,7 +256,8 @@ def get_all_available_instances(user, exclude_backend=None):
# only users with access to opendata can use the global /places
abort_request(user=user)

return user.get_all_available_instances(exclude_backend=exclude_backend)
bdd_instances = user.get_all_available_instances(exclude_backend=exclude_backend)
return [instance.name for instance in bdd_instances if instance.name in i_manager.instances]


def get_user(token, abort_if_no_token=True):
Expand Down
9 changes: 6 additions & 3 deletions source/jormungandr/jormungandr/interfaces/v1/Places.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from flask_restful import abort
from flask.globals import g
import flask
from jormungandr.authentication import get_all_available_instances
from jormungandr.authentication import get_all_available_instances_names
from jormungandr.interfaces.v1.decorators import get_serializer
from jormungandr.interfaces.v1.serializer.api import PlacesSerializer, PlacesNearbySerializer
from jormungandr import i_manager, timezone, global_autocomplete, authentication, app
Expand Down Expand Up @@ -207,7 +207,8 @@ def get(self, region=None, lon=None, lat=None):
args["q"] = query_string
response = i_manager.dispatch(args, "places", instance_name=self.region)
else:
available_instances = get_all_available_instances(user, exclude_backend='kraken')
available_instances_name = get_all_available_instances_names(user, exclude_backend='kraken')
available_instances = [i_manager.instances[name] for name in available_instances_name]

# If no instance available most probably due to database error
if (not user) and (not available_instances):
Expand Down Expand Up @@ -284,7 +285,9 @@ def get(self, id, region=None, lon=None, lat=None):
response = i_manager.dispatch(args, "place_uri", instance_name=self.region)
else:
user = authentication.get_user(token=authentication.get_token(), abort_if_no_token=False)
available_instances = get_all_available_instances(user, exclude_backend='kraken')

available_instances_name = get_all_available_instances_names(user, exclude_backend='kraken')
available_instances = [i_manager.instances[name] for name in available_instances_name]

# If no instance available most probably due to database error
if (not user) and (not available_instances):
Expand Down

0 comments on commit fad36ec

Please sign in to comment.