blob: 90b64979a8753196db5d5e146c8a7652e44e7cc7 [file] [log] [blame]
Mark Beierl9468ea32023-02-24 21:23:48 +00001#######################################################################################
2# Copyright ETSI Contributors and Others.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13# implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#######################################################################################
17
18import logging
19import uuid
20
21from temporalio.client import Client
22
23
24class WFTemporal(object):
Mark Beierl6f146502023-03-23 19:19:08 +000025 _client = None
26 temporal_api = None
Mark Beierl9468ea32023-02-24 21:23:48 +000027
Mark Beierl6f146502023-03-23 19:19:08 +000028 def __init__(self, logger_name="temporal.client"):
Mark Beierl9468ea32023-02-24 21:23:48 +000029 self.logger = logging.getLogger(logger_name)
Mark Beierl9468ea32023-02-24 21:23:48 +000030
31 async def execute_workflow(
32 self, task_queue: str, workflow_name: str, workflow_data: any, id: str = None
33 ):
34 handle = await self.start_workflow(
35 task_queue=task_queue,
36 workflow_name=workflow_name,
37 workflow_data=workflow_data,
38 id=id,
39 )
40 result = await handle.result()
41 self.logger.info(f"Completed workflow {workflow_name}, id {id}")
42 return result
43
44 async def start_workflow(
45 self, task_queue: str, workflow_name: str, workflow_data: any, id: str = None
46 ):
Mark Beierl6f146502023-03-23 19:19:08 +000047 if WFTemporal._client is None:
48 self.logger.debug(
49 f"No cached client found, connecting to {WFTemporal.temporal_api}"
50 )
51 WFTemporal._client = await Client.connect(WFTemporal.temporal_api)
52
Mark Beierl9468ea32023-02-24 21:23:48 +000053 if id is None:
54 id = str(uuid.uuid4())
55 self.logger.info(f"Starting workflow {workflow_name}, id {id}")
Mark Beierl6f146502023-03-23 19:19:08 +000056 handle = await WFTemporal._client.start_workflow(
Mark Beierl9468ea32023-02-24 21:23:48 +000057 workflow=workflow_name, arg=workflow_data, id=id, task_queue=task_queue
58 )
59 return handle