Skip to content

Commit

Permalink
Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-NRCan committed Oct 30, 2024
1 parent 4449331 commit 75283f2
Showing 1 changed file with 41 additions and 42 deletions.
83 changes: 41 additions & 42 deletions pygeoapi/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,17 +234,12 @@ def pre_load_colls(func):
def inner(*args, **kwargs):
cls = args[0]

# Validation on the method name for the provided class instance on this decoratino function

Check failure on line 237 in pygeoapi/api/__init__.py

View workflow job for this annotation

GitHub Actions / flake8_py3

pygeoapi/api/__init__.py#L237

[E501] line too long
if hasattr(cls, 'reload_resources_if_necessary'):
# Validate the resources are up to date
cls.reload_resources_if_necessary()

else:
cls = args[1]

if hasattr(cls, 'reload_resources_if_necessary'):
# Validate the resources are up to date
cls.reload_resources_if_necessary()

# Continue
return func(*args, **kwargs)

return inner
Expand Down Expand Up @@ -712,41 +707,24 @@ def __init__(self, config, openapi):
self.tpl_config = deepcopy(self.config)
self.tpl_config['server']['url'] = self.base_url

# Now that basic configuration is read, call the load ressources.
# Now that basic configuration is read, call the load_resources function. # noqa
# This call enables the api engine to load resources dynamically.
# That is, resources which could be coming from other sources than
# the yaml file itself. Indeed, the yaml file could be empty of
# resources and all read dynamically from somewhere else
# (e.g. a database).
# That way, it's a little easier to manage a dynamic ensemble of
# resoures, especially on pygeoapi distributed environments.
# This pattern allows for loading resources coming from another
# source (e.g. a database) rather than from the yaml file.
# This, along with the @pre_load_colls wrapper also enables
# resources management on multiple distributed pygeoapi instances.
self.load_resources()

self.manager = get_manager(self.config)
LOGGER.info('Process manager plugin loaded')

def load_resources(self):
"""
Calls on_load_resources and reassigns the resources configuration.
"""

# Call on_load_resources sending the current resources configuration.
self.config['resources'] = self.on_load_resources(self.config['resources']) # noqa

# Copy over for the template config (this is something that got added
# after a rebase of pending PR.. to be investigated..)
self.tpl_config['resources'] = deepcopy(self.config['resources'])

# Keep track of UTC date of last load
self.last_loaded_resources = datetime.now(timezone.utc)

def on_load_resources(self, resources):
def on_load_resources(self, resources: dict) -> dict:
"""
Overridable function to load (or reload) the available resources
dynamically.
By default, this function simply returns the resources as-is. This is
the original behavior of the API; expecting resources to be
already configured correctly per the yaml config file.
Overridable function to load the available resources dynamically.
By default, this function simply returns the provided resources
as-is. This is the original behavior of the API; expecting
resources to be already configured correctly per the yaml config
file.
:param resources: the resources as currently configured
(self.config['resources'])
Expand All @@ -756,19 +734,40 @@ def on_load_resources(self, resources):
# By default, return the same resources object, unchanged.
return resources

def on_load_resources_check(self, last_loaded_resources):
def on_load_resources_check(self, last_loaded_resources: datetime) -> bool: # noqa
"""
Overridable function to check if the resources should be reloaded.
As this implementation depends on your messaging broker, by default,
pygeoapi doesn't support that and returns False.
This implementation depends on your environment and messaging broker.
Natively, the resources used by the pygeoapi instance are strictly
the ones from the configuration file. It doesn't support resources
changing on-the-fly. Therefore, False is returned here and they
are never reloaded.
"""

# By default, return False to not reload the resources.
return False

def reload_resources_if_necessary(self):
def load_resources(self) -> None:
"""
Calls on_load_resources and reassigns the resources configuration.
"""

# Call on_load_resources sending the current resources configuration.
self.config['resources'] = self.on_load_resources(self.config['resources']) # noqa

# Copy over for the template config (this is something that got added
# after a rebase of pending PR.. to be investigated if still
# necessary to do so..)
# self.tpl_config['resources'] = deepcopy(self.config['resources'])

# Keep track of UTC date of last time resources were loaded
self.last_loaded_resources = datetime.now(timezone.utc)

def reload_resources_if_necessary(self) -> None:
"""
This function reloads the resources if necessary, by calling
'on_load_resources_check' and then calling 'load_resources' if
necessary.
Checks if the resources should be reloaded by calling overridable
function 'on_load_resources_check' and then, when necessary, calling
'load_resources'.
"""

# If the resources should be reloaded
Expand Down

0 comments on commit 75283f2

Please sign in to comment.