From ec2c8462da4880273fc220d85b593609f5d4649e Mon Sep 17 00:00:00 2001 From: Yoann Schneider <yschneider@teklia.com> Date: Wed, 27 Jul 2022 07:30:32 +0000 Subject: [PATCH] Always setup api Client --- arkindex_worker/worker/__init__.py | 3 +++ arkindex_worker/worker/base.py | 9 +++++---- tests/conftest.py | 1 + tests/test_base_worker.py | 7 +++++++ 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/arkindex_worker/worker/__init__.py b/arkindex_worker/worker/__init__.py index b7325d84..53bdffee 100644 --- a/arkindex_worker/worker/__init__.py +++ b/arkindex_worker/worker/__init__.py @@ -138,6 +138,9 @@ class ElementsWorker( def configure(self): # CLI args are stored on the instance so that implementations can access them self.args = self.parser.parse_args() + + super().setup_api_client() + if self.is_read_only: super().configure_for_developers() else: diff --git a/arkindex_worker/worker/base.py b/arkindex_worker/worker/base.py index cc78ce07..b046dc70 100644 --- a/arkindex_worker/worker/base.py +++ b/arkindex_worker/worker/base.py @@ -140,6 +140,11 @@ class BaseWorker(object): or self.worker_run_id is None ) + def setup_api_client(self): + # Build Arkindex API client from environment variables + self.api_client = ArkindexClient(**options_from_env()) + logger.debug(f"Setup Arkindex API client on {self.api_client.document.url}") + def configure_for_developers(self): assert self.is_read_only # Setup logging level if verbose or if ARKINDEX_DEBUG is set to true @@ -174,10 +179,6 @@ class BaseWorker(object): logger.setLevel(logging.DEBUG) logger.debug("Debug output enabled") - # Build Arkindex API client from environment variables - self.api_client = ArkindexClient(**options_from_env()) - logger.debug(f"Setup Arkindex API client on {self.api_client.document.url}") - # Load worker run information worker_run = self.request("RetrieveWorkerRun", id=self.worker_run_id) diff --git a/tests/conftest.py b/tests/conftest.py index ea16385f..35ac31ae 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -239,6 +239,7 @@ def mock_base_worker_with_cache(mocker, monkeypatch, mock_worker_run_api): monkeypatch.setattr(sys, "argv", ["worker"]) worker = BaseWorker(support_cache=True) + worker.setup_api_client() monkeypatch.setenv("PONOS_TASK", "my_task") return worker diff --git a/tests/test_base_worker.py b/tests/test_base_worker.py index 0a6cf496..84571ce7 100644 --- a/tests/test_base_worker.py +++ b/tests/test_base_worker.py @@ -98,6 +98,7 @@ def test_cli_default(mocker, mock_worker_run_api): assert worker.worker_version_id == "12341234-1234-1234-1234-123412341234" assert worker.worker_run_id == "56785678-5678-5678-5678-567856785678" + worker.setup_api_client() worker.configure() assert not worker.args.verbose assert logger.level == logging.NOTSET @@ -118,6 +119,7 @@ def test_cli_arg_verbose_given(mocker, mock_worker_run_api): assert worker.worker_version_id == "12341234-1234-1234-1234-123412341234" assert worker.worker_run_id == "56785678-5678-5678-5678-567856785678" + worker.setup_api_client() worker.configure() assert worker.args.verbose assert logger.level == logging.DEBUG @@ -139,6 +141,7 @@ def test_cli_envvar_debug_given(mocker, monkeypatch, mock_worker_run_api): assert worker.worker_version_id == "12341234-1234-1234-1234-123412341234" assert worker.worker_run_id == "56785678-5678-5678-5678-567856785678" + worker.setup_api_client() worker.configure() assert logger.level == logging.DEBUG assert worker.api_client @@ -212,6 +215,7 @@ def test_configure_worker_run(mocker, monkeypatch, responses): assert worker.worker_version_id == "12341234-1234-1234-1234-123412341234" assert worker.worker_run_id == "56785678-5678-5678-5678-567856785678" + worker.setup_api_client() worker.configure() assert worker.user_configuration == {"a": "b"} @@ -270,6 +274,7 @@ def test_configure_user_configuration_defaults( content_type="application/json", ) + worker.setup_api_client() worker.configure() assert worker.config == {"param_1": "/some/path/file.pth", "param_2": 12} @@ -323,6 +328,7 @@ def test_configure_user_config_debug(mocker, monkeypatch, responses, debug): content_type="application/json", ) worker.args = worker.parser.parse_args() + worker.setup_api_client() worker.configure() assert worker.user_configuration == {"debug": debug} @@ -370,6 +376,7 @@ def test_configure_worker_run_missing_conf(mocker, monkeypatch, responses): content_type="application/json", ) worker.args = worker.parser.parse_args() + worker.setup_api_client() worker.configure() assert worker.user_configuration is None -- GitLab