Skip to content

Commit

Permalink
Enable environment provider to run without celery
Browse files Browse the repository at this point in the history
This adds a few flags that can be used to make sure the environment
provider works well when being imported and ran from python instead
of running as a celery task.
  • Loading branch information
t-persson committed Feb 7, 2024
1 parent 976e9b0 commit c5e2a21
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/environment_provider/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@

DEV = os.getenv("DEV", "false").lower() == "true"
ENVIRONMENT = "development" if DEV else "production"
setup_logging("ETOS Environment Provider Worker", VERSION, ENVIRONMENT)
# Disable extra logging, if the environment provider is imported instead of executed via celery
if os.getenv("ENVIRONMENT_PROVIDER_DISABLE_LOGGING", "false") == "false":
setup_logging("ETOS Environment Provider Worker", VERSION, ENVIRONMENT)
# JSONTas would print all passwords as they are encrypted,
# which is not safe, so we disable propagation on the loggers.
# Propagation needs to be set to 0 instead of disabling the
Expand Down
6 changes: 4 additions & 2 deletions src/environment_provider/environment_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ class EnvironmentProvider: # pylint:disable=too-many-instance-attributes
task_track_started = True # Make celery task report 'STARTED' state
lock = Lock()

def __init__(self, suite_id: str, suite_runner_ids: list[str]) -> None:
def __init__(self, suite_id: str, suite_runner_ids: list[str], copy: bool = True) -> None:
"""Initialize ETOS, dataset, provider registry and splitter.
:param suite_id: Suite ID to get an environment for
:param suite_runner_ids: IDs from the suite runner to correlate sub suites.
:param copy: Whether or not to copy the etos config. Set to False if not running celery.
"""
FORMAT_CONFIG.identifier = suite_id
self.logger.info("Initializing EnvironmentProvider task.")
Expand All @@ -89,7 +90,8 @@ def __init__(self, suite_id: str, suite_runner_ids: list[str]) -> None:
# configuration dictionary.
# The impact of not doing this is that the environment provider would re-use
# another workers configuration instead of using its own.
self.etos.config.config = deepcopy(self.etos.config.config)
if copy:
self.etos.config.config = deepcopy(self.etos.config.config)
self.reset()
self.splitter = Splitter(self.etos, {})

Expand Down
4 changes: 3 additions & 1 deletion src/environment_provider_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@

DEV = os.getenv("DEV", "false").lower() == "true"
ENVIRONMENT = "development" if DEV else "production"
setup_logging("ETOS Environment Provider API", VERSION, ENVIRONMENT)
# Disable extra logging, if the environment provider is imported instead of executed via celery
if os.getenv("ENVIRONMENT_PROVIDER_DISABLE_LOGGING", "false") == "false":
setup_logging("ETOS Environment Provider API", VERSION, ENVIRONMENT)

0 comments on commit c5e2a21

Please sign in to comment.