system/flow_wrapper.py
gitea_admin_user 6ce236ba8b
All checks were successful
CI Workflow / Testing the Flow (push) Successful in 7s
CI Workflow / Containerize the Flow (push) Successful in 14s
Upload files to "/"
2025-04-09 20:08:58 +00:00

86 lines
2.9 KiB
Python

import asyncio
import os
import re
import sys
import logging
from temporalio.client import Client
from temporalio.worker import Worker
# Ensure the generated workflow module is in the Python path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s - %(message)s",
)
logger = logging.getLogger(__name__)
# Retrieve environment variables
REPO_NAME = os.getenv('REPO_NAME')
BRANCH_NAME = os.getenv('BRANCH_NAME')
COMMIT_ID = os.getenv('VERSION')
NAMESPACE = os.getenv('NAMESPACE')
FLOWX_ENGINE_ADDRESS = os.getenv('FLOWX_ENGINE_ADDRESS')
if not BRANCH_NAME or not COMMIT_ID or not BRANCH_NAME or not NAMESPACE or not FLOWX_ENGINE_ADDRESS:
raise ValueError("Environment variables BRANCH_NAME, VERSION, BRANCH_NAME, NAMESPACE and FLOWX_ENGINE_ADDRESS must be set.")
# Shorten the commit ID to the first 10 characters
COMMIT_ID_SHORT = COMMIT_ID[:10]
# Sanitize flow name and commit ID to create a valid task queue name
def sanitize_name(name):
# Replace non-alphanumeric characters or invalid start with underscores
sanitized = re.sub(r'\W|^(?=\d)', '_', name)
# Replace multiple consecutive underscores with a single underscore
sanitized = re.sub(r'_+', '_', sanitized)
# Remove trailing underscores
return sanitized.strip('_')
FLOW_NAME = REPO_NAME + "_" + BRANCH_NAME
flow_name_safe = sanitize_name(FLOW_NAME)
commit_id_safe = sanitize_name(COMMIT_ID_SHORT)
# Construct the task queue name
# TASK_QUEUE = f"{flow_name_safe}_{commit_id_safe}"
TASK_QUEUE = flow_name_safe
# Import the default workflow module
workflow_module_name = "workflow" # Hardcoded to the default module name 'workflow.py'
try:
workflow_module = __import__(workflow_module_name)
except ImportError as e:
raise ImportError(f"Failed to import workflow module '{workflow_module_name}': {e}")
# Get the workflow class
# Assuming the workflow class is named as <FlowName>Workflow, e.g., HybridWorkflow
# workflow_class_name = f"{flow_name_safe}_{commit_id_safe}"
workflow_class_name = flow_name_safe
workflow_class = getattr(workflow_module, workflow_class_name, None)
if not workflow_class:
raise AttributeError(f"Workflow class '{workflow_class_name}' not found in module '{workflow_module_name}'.")
async def main():
"""
Initialize and run the worker with the activity.
"""
try:
client = await Client.connect(FLOWX_ENGINE_ADDRESS, namespace=NAMESPACE)
# No activities are registered since they are in separate containers
worker = Worker(
client,
task_queue=TASK_QUEUE,
workflows=[workflow_class],
)
logger.info("Worker starting for %s, listening to task queue: %s", workflow_class_name, TASK_QUEUE)
await worker.run()
except Exception as e:
logger.critical("Worker failed to start: %s", e)
raise
if __name__ == "__main__":
asyncio.run(main())