Skip to content

Commit

Permalink
add possibility to override all client connection settings (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
krayevidi authored Oct 18, 2024
1 parent 9fa1b22 commit 295092d
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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`
1 change: 0 additions & 1 deletion dev/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
from enum import StrEnum

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

Expand Down
17 changes: 12 additions & 5 deletions dev/tests/test_init_client.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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")
14 changes: 5 additions & 9 deletions dev/tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
)
Expand All @@ -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"]
)
Expand Down
2 changes: 1 addition & 1 deletion django_temporalio/__init__.py
Original file line number Diff line number Diff line change
@@ -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__ = "[email protected]"
Expand Down
5 changes: 1 addition & 4 deletions django_temporalio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 5 additions & 3 deletions django_temporalio/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,8 +20,7 @@

SETTINGS_KEY = "DJANGO_TEMPORALIO"
DEFAULTS = {
"URL": "http://localhost:7233",
"NAMESPACE": "default",
"CLIENT_CONFIG": {},
"WORKER_CONFIGS": {},
"BASE_MODULE": None,
}
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 1.1.0
current_version = 1.2.0
commit = True
tag = True

Expand Down

0 comments on commit 295092d

Please sign in to comment.