Temporal.io integration for Django
Project description
django-temporalio
A small Django app that provides helpers for integrating Temporal.io with Django.
Features
- Registry: Provides a registry that holds mappings between queue names and registered activities and workflows.
- Management Commands: Includes management commands to manage Temporal.io workers and sync schedules.
Installation
You can install django_temporalio
using pip:
$ pip install django-temporalio
Add django_temporalio
to your INSTALLED_APPS
:
INSTALLED_APPS = [
...
'django_temporalio.apps.DjangoTemporalioConfig',
...
]
Add the following settings to your settings.py
:
from temporalio.worker import WorkerConfig
DJANGO_TEMPORALIO = {
"CLIENT_CONFIG": {
"target_host": "localhost:7233",
},
"BASE_MODULE": "path.to.module",
"WORKER_CONFIGS": {
"main": WorkerConfig(
task_queue="MAIN_TASK_QUEUE",
...
),
...
},
}
Usage
Activities, workflows and schedules should be placed inside the base module defined by the BASE_MODULE
setting,
preferably outside of any Django application, in order to keep the uses of
the imports_passed_through context manager encapsulated
inside the module, along with Temporal.io related code.
Workflow and Activity Registry
The registry is a singleton that holds mappings between queue names and registered activities and workflows.
You can register activities and workflows using the register
method.
Activities and workflows should be declared in modules matching the following patterns *workflows*.py
and
*activities*.py
respectively.
from temporalio import activity, workflow
from django_temporalio.registry import queue_activities, queue_workflows
@queue_activities.register("HIGH_PRIORITY_TASK_QUEUE", "MAIN_TASK_QUEUE")
@activity.defn
def my_activity():
pass
@queue_workflows.register("HIGH_PRIORITY_TASK_QUEUE", "MAIN_TASK_QUEUE")
@workflow.defn
class MyWorkflow:
pass
Schedule Registry
You can register schedules using the register
method.
Schedules should be declared in schedules.py
module.
from django_temporalio.registry import schedules
from temporalio.client import Schedule
schedules.register("do-cool-stuff-every-hour", Schedule(...))
Management Commands
To see a queue's registered activities and workflows:
$ ./manage.py show_temporalio_queue_registry
To start a worker defined in the settings (for production):
$ ./manage.py start_temporalio_worker <worker_name>
To start a worker for development (starts a worker for each registered queue, WORKER_CONFIGS setting is ignored):
$ ./manage.py start_temporalio_worker --all
To sync schedules with Temporal.io:
$ ./manage.py sync_temporalio_schedules
To see what sync operation would do without actually syncing:
$ ./manage.py sync_temporal_schedules --dry-run
Configuration
You can configure the app using the following settings:
DJANGO_TEMPORALIO: A dictionary containing the following keys:
- 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
temporalio.worker.WorkerConfig
instance. - BASE_MODULE: A python module that holds workflows, activities and schedules, defaults to
None
1.0.0 (2024-05-16)
- Initial release
1.1.0 (2024-05-30)
- add Temporal.io related code encapsulation
1.2.0 (2024-10-17)
- replaced
NAMESPACE
andURL
settings withCLIENT_CONFIG
setting
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_temporalio-1.2.0.tar.gz
.
File metadata
- Download URL: django_temporalio-1.2.0.tar.gz
- Upload date:
- Size: 9.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35b36f1a37fd4cd0a79016e57682e54a1efbb485b398002a28b3c14ad5091cfe |
|
MD5 | 190e58b01b353b3a0bb651641c6fcb07 |
|
BLAKE2b-256 | 91e5a6fc0b2900542c514a2b55d588a9e8cb6472b3a4cbe8103c2abb8e8f1add |
File details
Details for the file django_temporalio-1.2.0-py3-none-any.whl
.
File metadata
- Download URL: django_temporalio-1.2.0-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5aa4e3dd92d30e11b4d58666f99e66df0f46c72559a0b50ba97aec37e2104f16 |
|
MD5 | d80bfb524f4714986ff17a5bac509192 |
|
BLAKE2b-256 | 3ef9b5ac0b83b11cc8de60f495ff9f4cd92ce455cc71cf37ed10923deb8b43df |