36 lines
790 B
Python
36 lines
790 B
Python
"""Temporal client helpers."""
|
|
|
|
import asyncio
|
|
|
|
from temporalio.client import Client
|
|
|
|
from app.config.settings import get_settings
|
|
|
|
_client: Client | None = None
|
|
_client_lock = asyncio.Lock()
|
|
|
|
|
|
async def get_temporal_client() -> Client:
|
|
"""Return a cached Temporal client."""
|
|
|
|
global _client
|
|
if _client is not None:
|
|
return _client
|
|
|
|
async with _client_lock:
|
|
if _client is None:
|
|
settings = get_settings()
|
|
_client = await Client.connect(
|
|
settings.temporal_address,
|
|
namespace=settings.temporal_namespace,
|
|
)
|
|
return _client
|
|
|
|
|
|
def set_temporal_client(client: Client | None) -> None:
|
|
"""Override the cached Temporal client, primarily for tests."""
|
|
|
|
global _client
|
|
_client = client
|
|
|