| ####################################################################################### |
| # Copyright ETSI Contributors and Others. |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| # implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| ####################################################################################### |
| |
| import logging |
| import uuid |
| |
| from temporalio.client import Client |
| |
| |
| class WFTemporal(object): |
| _client = None |
| temporal_api = None |
| |
| def __init__(self, logger_name="temporal.client"): |
| self.logger = logging.getLogger(logger_name) |
| |
| async def execute_workflow( |
| self, task_queue: str, workflow_name: str, workflow_data: any, id: str = None |
| ): |
| handle = await self.start_workflow( |
| task_queue=task_queue, |
| workflow_name=workflow_name, |
| workflow_data=workflow_data, |
| id=id, |
| ) |
| result = await handle.result() |
| self.logger.info(f"Completed workflow {workflow_name}, id {id}") |
| return result |
| |
| async def start_workflow( |
| self, task_queue: str, workflow_name: str, workflow_data: any, id: str = None |
| ): |
| 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 WFTemporal._client.start_workflow( |
| workflow=workflow_name, arg=workflow_data, id=id, task_queue=task_queue |
| ) |
| return handle |