X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_common%2Fwftemporal.py;h=90b64979a8753196db5d5e146c8a7652e44e7cc7;hb=7e637e8308b384e30756a3e2ebaef8a4c1117c9d;hp=0f7d4212684915d038297cda98b32f62f85e4ae1;hpb=9468ea357b051daa2b173958ab32a5b2ec12fcd7;p=osm%2Fcommon.git diff --git a/osm_common/wftemporal.py b/osm_common/wftemporal.py index 0f7d421..90b6497 100644 --- a/osm_common/wftemporal.py +++ b/osm_common/wftemporal.py @@ -22,11 +22,11 @@ from temporalio.client import Client class WFTemporal(object): - clients = {} + _client = None + temporal_api = None - def __init__(self, temporal_api=None, logger_name="temporal.client"): + def __init__(self, logger_name="temporal.client"): self.logger = logging.getLogger(logger_name) - self.temporal_api = temporal_api async def execute_workflow( self, task_queue: str, workflow_name: str, workflow_data: any, id: str = None @@ -44,24 +44,16 @@ class WFTemporal(object): async def start_workflow( self, task_queue: str, workflow_name: str, workflow_data: any, id: str = None ): - client = await self.get_client() + if WFTemporal._client is None: + self.logger.debug( + f"No cached client found, connecting to {WFTemporal.temporal_api}" + ) + WFTemporal._client = await Client.connect(WFTemporal.temporal_api) + if id is None: id = str(uuid.uuid4()) self.logger.info(f"Starting workflow {workflow_name}, id {id}") - handle = await client.start_workflow( + handle = await WFTemporal._client.start_workflow( workflow=workflow_name, arg=workflow_data, id=id, task_queue=task_queue ) return handle - - async def get_client(self): - if self.temporal_api in WFTemporal.clients: - client = WFTemporal.clients[self.temporal_api] - else: - self.logger.debug( - f"No cached client found, connecting to {self.temporal_api}" - ) - client = await Client.connect(self.temporal_api) - WFTemporal.clients[self.temporal_api] = client - - self.logger.debug(f"Using client {client} for {self.temporal_api}") - return client