|
| 1 | +import asyncio |
| 2 | +import dataclasses |
| 3 | +import logging |
| 4 | +from datetime import datetime, timedelta |
| 5 | +from ipaddress import IPv4Address |
| 6 | +from typing import List |
| 7 | + |
| 8 | +from temporalio import activity, workflow |
| 9 | +from temporalio.client import Client |
| 10 | +from temporalio.worker import Worker |
| 11 | +from temporalio.worker.workflow_sandbox import ( |
| 12 | + SandboxedWorkflowRunner, |
| 13 | + SandboxRestrictions, |
| 14 | +) |
| 15 | + |
| 16 | +# We always want to pass through external modules to the sandbox that we know |
| 17 | +# are safe for workflow use |
| 18 | +with workflow.unsafe.imports_passed_through(): |
| 19 | + from pydantic import BaseModel |
| 20 | + |
| 21 | + from pydantic_converter_v1.converter import pydantic_data_converter |
| 22 | + |
| 23 | + |
| 24 | +class MyPydanticModel(BaseModel): |
| 25 | + some_ip: IPv4Address |
| 26 | + some_date: datetime |
| 27 | + |
| 28 | + |
| 29 | +@activity.defn |
| 30 | +async def my_activity(models: List[MyPydanticModel]) -> List[MyPydanticModel]: |
| 31 | + activity.logger.info("Got models in activity: %s" % models) |
| 32 | + return models |
| 33 | + |
| 34 | + |
| 35 | +@workflow.defn |
| 36 | +class MyWorkflow: |
| 37 | + @workflow.run |
| 38 | + async def run(self, models: List[MyPydanticModel]) -> List[MyPydanticModel]: |
| 39 | + workflow.logger.info("Got models in workflow: %s" % models) |
| 40 | + return await workflow.execute_activity( |
| 41 | + my_activity, models, start_to_close_timeout=timedelta(minutes=1) |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +# Due to known issues with Pydantic's use of issubclass and our inability to |
| 46 | +# override the check in sandbox, Pydantic will think datetime is actually date |
| 47 | +# in the sandbox. At the expense of protecting against datetime.now() use in |
| 48 | +# workflows, we're going to remove datetime module restrictions. See sdk-python |
| 49 | +# README's discussion of known sandbox issues for more details. |
| 50 | +def new_sandbox_runner() -> SandboxedWorkflowRunner: |
| 51 | + # TODO(cretz): Use with_child_unrestricted when https://github.com/temporalio/sdk-python/issues/254 |
| 52 | + # is fixed and released |
| 53 | + invalid_module_member_children = dict( |
| 54 | + SandboxRestrictions.invalid_module_members_default.children |
| 55 | + ) |
| 56 | + del invalid_module_member_children["datetime"] |
| 57 | + return SandboxedWorkflowRunner( |
| 58 | + restrictions=dataclasses.replace( |
| 59 | + SandboxRestrictions.default, |
| 60 | + invalid_module_members=dataclasses.replace( |
| 61 | + SandboxRestrictions.invalid_module_members_default, |
| 62 | + children=invalid_module_member_children, |
| 63 | + ), |
| 64 | + ) |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +interrupt_event = asyncio.Event() |
| 69 | + |
| 70 | + |
| 71 | +async def main(): |
| 72 | + logging.basicConfig(level=logging.INFO) |
| 73 | + # Connect client using the Pydantic converter |
| 74 | + client = await Client.connect( |
| 75 | + "localhost:7233", data_converter=pydantic_data_converter |
| 76 | + ) |
| 77 | + |
| 78 | + # Run a worker for the workflow |
| 79 | + async with Worker( |
| 80 | + client, |
| 81 | + task_queue="pydantic_converter-task-queue", |
| 82 | + workflows=[MyWorkflow], |
| 83 | + activities=[my_activity], |
| 84 | + workflow_runner=new_sandbox_runner(), |
| 85 | + ): |
| 86 | + # Wait until interrupted |
| 87 | + print("Worker started, ctrl+c to exit") |
| 88 | + await interrupt_event.wait() |
| 89 | + print("Shutting down") |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + loop = asyncio.new_event_loop() |
| 94 | + try: |
| 95 | + loop.run_until_complete(main()) |
| 96 | + except KeyboardInterrupt: |
| 97 | + interrupt_event.set() |
| 98 | + loop.run_until_complete(loop.shutdown_asyncgens()) |
0 commit comments