|
| 1 | +""" |
| 2 | +Custom Temporal Activities Template |
| 3 | +==================================== |
| 4 | +This file is for defining custom Temporal activities that can be executed |
| 5 | +by your workflow. Activities are used for: |
| 6 | +- External API calls |
| 7 | +- Database operations |
| 8 | +- File I/O operations |
| 9 | +- Heavy computations |
| 10 | +- Any non-deterministic operations |
| 11 | + |
| 12 | +IMPORTANT: All activities should have appropriate timeouts! |
| 13 | +Default recommendation: start_to_close_timeout=timedelta(minutes=10) |
| 14 | +""" |
| 15 | + |
| 16 | +from datetime import timedelta |
| 17 | +from typing import Any, Dict |
| 18 | + |
| 19 | +from pydantic import BaseModel |
| 20 | +from temporalio import activity |
| 21 | +from temporalio.common import RetryPolicy |
| 22 | + |
| 23 | +from agentex.lib.utils.logging import make_logger |
| 24 | + |
| 25 | +logger = make_logger(__name__) |
| 26 | + |
| 27 | + |
| 28 | +# Example activity parameter models |
| 29 | +class ExampleActivityParams(BaseModel): |
| 30 | + """Parameters for the example activity""" |
| 31 | + data: Dict[str, Any] |
| 32 | + task_id: str |
| 33 | + |
| 34 | + |
| 35 | +# Example custom activity |
| 36 | +@activity.defn(name="example_custom_activity") |
| 37 | +async def example_custom_activity(params: ExampleActivityParams) -> Dict[str, Any]: |
| 38 | + """ |
| 39 | + Example custom activity that demonstrates best practices. |
| 40 | + |
| 41 | + When calling this activity from your workflow, use: |
| 42 | + ```python |
| 43 | + result = await workflow.execute_activity( |
| 44 | + "example_custom_activity", |
| 45 | + ExampleActivityParams(data={"key": "value"}, task_id=task_id), |
| 46 | + start_to_close_timeout=timedelta(minutes=10), # Recommended: 10 minute timeout |
| 47 | + heartbeat_timeout=timedelta(minutes=1), # Optional: heartbeat every minute |
| 48 | + retry_policy=RetryPolicy(maximum_attempts=3) # Optional: retry up to 3 times |
| 49 | + ) |
| 50 | + ``` |
| 51 | + """ |
| 52 | + logger.info(f"Processing activity for task {params.task_id} with data: {params.data}") |
| 53 | + |
| 54 | + # Your activity logic here |
| 55 | + # This could be: |
| 56 | + # - API calls |
| 57 | + # - Database operations |
| 58 | + # - File processing |
| 59 | + # - ML model inference |
| 60 | + # - etc. |
| 61 | + |
| 62 | + result = { |
| 63 | + "status": "success", |
| 64 | + "processed_data": params.data, |
| 65 | + "task_id": params.task_id |
| 66 | + } |
| 67 | + |
| 68 | + return result |
| 69 | + |
| 70 | + |
| 71 | +# Add more custom activities below as needed |
| 72 | +# Remember to: |
| 73 | +# 1. Use appropriate timeouts (default: 10 minutes) |
| 74 | +# 2. Define clear parameter models with Pydantic |
| 75 | +# 3. Handle errors appropriately |
| 76 | +# 4. Use logging for debugging |
| 77 | +# 5. Keep activities focused on a single responsibility |
0 commit comments