-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix custom_converter and infrequent_polling samples (#95)
- Loading branch information
Showing
7 changed files
with
115 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import dataclasses | ||
from typing import Any, Optional, Type | ||
|
||
import temporalio.converter | ||
from temporalio.api.common.v1 import Payload | ||
from temporalio.converter import ( | ||
CompositePayloadConverter, | ||
DefaultPayloadConverter, | ||
EncodingPayloadConverter, | ||
) | ||
|
||
|
||
class GreetingInput: | ||
def __init__(self, name: str) -> None: | ||
self.name = name | ||
|
||
|
||
class GreetingOutput: | ||
def __init__(self, result: str) -> None: | ||
self.result = result | ||
|
||
|
||
class GreetingEncodingPayloadConverter(EncodingPayloadConverter): | ||
@property | ||
def encoding(self) -> str: | ||
return "text/my-greeting-encoding" | ||
|
||
def to_payload(self, value: Any) -> Optional[Payload]: | ||
if isinstance(value, GreetingInput): | ||
return Payload( | ||
metadata={"encoding": self.encoding.encode(), "is_input": b"true"}, | ||
data=value.name.encode(), | ||
) | ||
elif isinstance(value, GreetingOutput): | ||
return Payload( | ||
metadata={"encoding": self.encoding.encode()}, | ||
data=value.result.encode(), | ||
) | ||
else: | ||
return None | ||
|
||
def from_payload(self, payload: Payload, type_hint: Optional[Type] = None) -> Any: | ||
if payload.metadata.get("is_input") == b"true": | ||
# Confirm proper type hint if present | ||
assert not type_hint or type_hint is GreetingInput | ||
return GreetingInput(payload.data.decode()) | ||
else: | ||
assert not type_hint or type_hint is GreetingOutput | ||
return GreetingOutput(payload.data.decode()) | ||
|
||
|
||
class GreetingPayloadConverter(CompositePayloadConverter): | ||
def __init__(self) -> None: | ||
# Just add ours as first before the defaults | ||
super().__init__( | ||
GreetingEncodingPayloadConverter(), | ||
# TODO(cretz): Make this list available without instantiation - https://github.com/temporalio/sdk-python/issues/139 | ||
*DefaultPayloadConverter().converters.values(), | ||
) | ||
|
||
|
||
# Use the default data converter, but change the payload converter. | ||
greeting_data_converter = dataclasses.replace( | ||
temporalio.converter.default(), | ||
payload_converter_class=GreetingPayloadConverter, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from temporalio import workflow | ||
|
||
with workflow.unsafe.imports_passed_through(): | ||
from custom_converter.shared import GreetingInput, GreetingOutput | ||
|
||
|
||
@workflow.defn | ||
class GreetingWorkflow: | ||
@workflow.run | ||
async def run(self, input: GreetingInput) -> GreetingOutput: | ||
return GreetingOutput(f"Hello, {input.name}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import uuid | ||
|
||
from temporalio.client import Client | ||
from temporalio.worker import Worker | ||
|
||
from custom_converter.shared import ( | ||
GreetingInput, | ||
GreetingOutput, | ||
greeting_data_converter, | ||
) | ||
from custom_converter.workflow import GreetingWorkflow | ||
|
||
|
||
async def test_workflow_with_custom_converter(client: Client): | ||
# Replace data converter in client | ||
new_config = client.config() | ||
new_config["data_converter"] = greeting_data_converter | ||
client = Client(**new_config) | ||
task_queue = f"tq-{uuid.uuid4()}" | ||
async with Worker(client, task_queue=task_queue, workflows=[GreetingWorkflow]): | ||
result = await client.execute_workflow( | ||
GreetingWorkflow.run, | ||
GreetingInput("Temporal"), | ||
id=f"wf-{uuid.uuid4()}", | ||
task_queue=task_queue, | ||
) | ||
assert isinstance(result, GreetingOutput) | ||
assert result.result == "Hello, Temporal" |