|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import dataclasses |
| 5 | +from dataclasses import dataclass |
| 6 | +from datetime import timedelta |
| 7 | +from typing import Any, Dict, List, Optional, Union |
| 8 | + |
| 9 | +from temporalio import workflow |
| 10 | + |
| 11 | + |
| 12 | +@dataclass |
| 13 | +class DSLInput: |
| 14 | + root: Statement |
| 15 | + variables: Dict[str, Any] = dataclasses.field(default_factory=dict) |
| 16 | + |
| 17 | + |
| 18 | +@dataclass |
| 19 | +class ActivityStatement: |
| 20 | + activity: ActivityInvocation |
| 21 | + |
| 22 | + |
| 23 | +@dataclass |
| 24 | +class ActivityInvocation: |
| 25 | + name: str |
| 26 | + arguments: List[str] = dataclasses.field(default_factory=list) |
| 27 | + result: Optional[str] = None |
| 28 | + |
| 29 | + |
| 30 | +@dataclass |
| 31 | +class SequenceStatement: |
| 32 | + sequence: Sequence |
| 33 | + |
| 34 | + |
| 35 | +@dataclass |
| 36 | +class Sequence: |
| 37 | + elements: List[Statement] |
| 38 | + |
| 39 | + |
| 40 | +@dataclass |
| 41 | +class ParallelStatement: |
| 42 | + parallel: Parallel |
| 43 | + |
| 44 | + |
| 45 | +@dataclass |
| 46 | +class Parallel: |
| 47 | + branches: List[Statement] |
| 48 | + |
| 49 | + |
| 50 | +Statement = Union[ActivityStatement, SequenceStatement, ParallelStatement] |
| 51 | + |
| 52 | + |
| 53 | +@workflow.defn |
| 54 | +class DSLWorkflow: |
| 55 | + @workflow.run |
| 56 | + async def run(self, input: DSLInput) -> Dict[str, Any]: |
| 57 | + self.variables = dict(input.variables) |
| 58 | + workflow.logger.info("Running DSL workflow") |
| 59 | + await self.execute_statement(input.root) |
| 60 | + workflow.logger.info("DSL workflow completed") |
| 61 | + return self.variables |
| 62 | + |
| 63 | + async def execute_statement(self, stmt: Statement) -> None: |
| 64 | + if isinstance(stmt, ActivityStatement): |
| 65 | + # Invoke activity loading arguments from variables and optionally |
| 66 | + # storing result as a variable |
| 67 | + result = await workflow.execute_activity( |
| 68 | + stmt.activity.name, |
| 69 | + args=[self.variables.get(arg, "") for arg in stmt.activity.arguments], |
| 70 | + start_to_close_timeout=timedelta(minutes=1), |
| 71 | + ) |
| 72 | + if stmt.activity.result: |
| 73 | + self.variables[stmt.activity.result] = result |
| 74 | + elif isinstance(stmt, SequenceStatement): |
| 75 | + # Execute each statement in order |
| 76 | + for elem in stmt.sequence.elements: |
| 77 | + await self.execute_statement(elem) |
| 78 | + elif isinstance(stmt, ParallelStatement): |
| 79 | + # Execute all in parallel. Note, this will raise an exception when |
| 80 | + # the first activity fails and will not cancel the others. We could |
| 81 | + # store tasks and cancel if we wanted. In newer Python versions this |
| 82 | + # would use a TaskGroup instead. |
| 83 | + await asyncio.gather( |
| 84 | + *[self.execute_statement(branch) for branch in stmt.parallel.branches] |
| 85 | + ) |
0 commit comments