From 295092dc6787f240a0a1904497901440515c3121 Mon Sep 17 00:00:00 2001 From: Andrew Sutulov Date: Fri, 18 Oct 2024 17:25:38 +0200 Subject: [PATCH] add possibility to override all client connection settings (#5) --- CHANGELOG.md | 4 ++++ README.md | 10 ++++++---- dev/settings.py | 1 - dev/tests/test_init_client.py | 17 ++++++++++++----- dev/tests/test_settings.py | 14 +++++--------- django_temporalio/__init__.py | 2 +- django_temporalio/client.py | 5 +---- django_temporalio/conf.py | 8 +++++--- setup.cfg | 2 +- 9 files changed, 35 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02728a8..3f49349 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,3 +5,7 @@ ## 1.1.0 (2024-05-30) * add Temporal.io related code encapsulation + +## 1.2.0 (2024-10-17) + +* replaced `NAMESPACE` and `URL` settings with `CLIENT_CONFIG` setting diff --git a/README.md b/README.md index bbda0d1..f5ddd3b 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,9 @@ Add the following settings to your `settings.py`: from temporalio.worker import WorkerConfig DJANGO_TEMPORALIO = { - "URL": "localhost:7233", + "CLIENT_CONFIG": { + "target_host": "localhost:7233", + }, "BASE_MODULE": "path.to.module", "WORKER_CONFIGS": { "main": WorkerConfig( @@ -126,8 +128,8 @@ You can configure the app using the following settings: DJANGO_TEMPORALIO: A dictionary containing the following keys: -- URL: The Temporal.io host to connect to, defaults to `http://localhost:7233` -- NAMESPACE: The Temporal.io namespace to use, defaults to `default` +- CLIENT_CONFIG: A dictionary of kwargs that are passed to the `temporalio.client.Client.connect` + method on the client initialization, defaults to `{}` - WORKER_CONFIGS: A dictionary containing worker configurations. - The key is the worker name and the value is a `WorkerConfig` instance. + The key is the worker name and the value is a `temporalio.worker.WorkerConfig` instance. - BASE_MODULE: A python module that holds workflows, activities and schedules, defaults to `None` diff --git a/dev/settings.py b/dev/settings.py index f409e44..10a072c 100644 --- a/dev/settings.py +++ b/dev/settings.py @@ -1,5 +1,4 @@ import os -from enum import StrEnum BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) diff --git a/dev/tests/test_init_client.py b/dev/tests/test_init_client.py index 714c6dd..a5cf32d 100644 --- a/dev/tests/test_init_client.py +++ b/dev/tests/test_init_client.py @@ -1,7 +1,9 @@ from unittest import IsolatedAsyncioTestCase, mock +from django.test import override_settings + from django_temporalio.client import init_client -from django_temporalio.conf import settings +from django_temporalio.conf import SETTINGS_KEY class InitClientTestCase(IsolatedAsyncioTestCase): @@ -10,9 +12,14 @@ class InitClientTestCase(IsolatedAsyncioTestCase): """ async def test_init_client(self): - with mock.patch("django_temporalio.client.Client.connect") as connect_mock: + settings = { + "CLIENT_CONFIG": {"foo": "bar"}, + } + + with ( + mock.patch("django_temporalio.client.Client.connect") as connect_mock, + override_settings(**{SETTINGS_KEY: settings}), + ): await init_client() - connect_mock.assert_called_once_with( - target_host=settings.URL, namespace=settings.NAMESPACE - ) + connect_mock.assert_called_once_with(foo="bar") diff --git a/dev/tests/test_settings.py b/dev/tests/test_settings.py index a0060c8..fc284fa 100644 --- a/dev/tests/test_settings.py +++ b/dev/tests/test_settings.py @@ -17,21 +17,18 @@ class SettingsTestCase(TestCase): def test_default_settings(self): self.assertFalse(hasattr(django_settings, SETTINGS_KEY)) - self.assertEqual(temporalio_settings.URL, DEFAULTS["URL"]) - self.assertEqual(temporalio_settings.NAMESPACE, DEFAULTS["NAMESPACE"]) + self.assertEqual(temporalio_settings.CLIENT_CONFIG, DEFAULTS["CLIENT_CONFIG"]) self.assertEqual(temporalio_settings.WORKER_CONFIGS, DEFAULTS["WORKER_CONFIGS"]) self.assertEqual(temporalio_settings.BASE_MODULE, DEFAULTS["BASE_MODULE"]) def test_user_settings(self): user_settings = { - "URL": "http://temporal:7233", - "NAMESPACE": "main", + "CLIENT_CONFIG": {"target_host": "temporal:7233"}, "WORKER_CONFIGS": {"main": "config"}, "BASE_MODULE": "dev.temporalio", } with override_settings(**{SETTINGS_KEY: user_settings}): - self.assertEqual(temporalio_settings.URL, user_settings["URL"]) - self.assertEqual(temporalio_settings.NAMESPACE, user_settings["NAMESPACE"]) + self.assertEqual(temporalio_settings.CLIENT_CONFIG, user_settings["CLIENT_CONFIG"]) self.assertEqual( temporalio_settings.WORKER_CONFIGS, user_settings["WORKER_CONFIGS"] ) @@ -41,11 +38,10 @@ def test_user_settings(self): def test_fallback_to_defaults(self): user_settings = { - "NAMESPACE": "main", + "CLIENT_CONFIG": {"target_host": "temporal:7233"}, } with override_settings(**{SETTINGS_KEY: user_settings}): - self.assertEqual(temporalio_settings.URL, DEFAULTS["URL"]) - self.assertEqual(temporalio_settings.NAMESPACE, user_settings["NAMESPACE"]) + self.assertEqual(temporalio_settings.CLIENT_CONFIG, user_settings["CLIENT_CONFIG"]) self.assertEqual( temporalio_settings.WORKER_CONFIGS, DEFAULTS["WORKER_CONFIGS"] ) diff --git a/django_temporalio/__init__.py b/django_temporalio/__init__.py index 50a7712..cf35756 100644 --- a/django_temporalio/__init__.py +++ b/django_temporalio/__init__.py @@ -1,6 +1,6 @@ __title__ = "django-temporalio" __description__ = "Temporal.io integration for Django" -__version__ = "1.1.0" +__version__ = "1.2.0" __url__ = "https://github.com/RegioHelden/django-temporalio" __author__ = "RegioHelden GmbH" __author_email__ = "opensource@regiohelden.de" diff --git a/django_temporalio/client.py b/django_temporalio/client.py index 0aafd9e..da515c4 100644 --- a/django_temporalio/client.py +++ b/django_temporalio/client.py @@ -7,7 +7,4 @@ async def init_client(): """ Connect to Temporal.io server and return a client instance. """ - return await Client.connect( - target_host=settings.URL, - namespace=settings.NAMESPACE, - ) + return await Client.connect(**settings.CLIENT_CONFIG) diff --git a/django_temporalio/conf.py b/django_temporalio/conf.py index d566e71..66bb402 100644 --- a/django_temporalio/conf.py +++ b/django_temporalio/conf.py @@ -3,7 +3,10 @@ For example your project's `settings.py` file might look like this: DJANGO_TEMPORALIO = { - 'URL': 'http://localhost:7233', + # params passed to the `temporalio.client.Client.connect' method + 'CLIENT_CONFIG': { + 'target_host': 'localhost:7233', + }, } This module provides the `settings` object, that is used to access @@ -17,8 +20,7 @@ SETTINGS_KEY = "DJANGO_TEMPORALIO" DEFAULTS = { - "URL": "http://localhost:7233", - "NAMESPACE": "default", + "CLIENT_CONFIG": {}, "WORKER_CONFIGS": {}, "BASE_MODULE": None, } diff --git a/setup.cfg b/setup.cfg index 150dd75..588ce0f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 1.1.0 +current_version = 1.2.0 commit = True tag = True