From 627b701a59b43071035653bf7a90a819fbde4398 Mon Sep 17 00:00:00 2001 From: Kevin Pham <161087761+accupham@users.noreply.github.com> Date: Sat, 5 Oct 2024 13:37:36 -0500 Subject: [PATCH 1/2] Update hello_search_attributes.py with new typed search attribute syntax Old example used deprecated syntax --- hello/hello_search_attributes.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/hello/hello_search_attributes.py b/hello/hello_search_attributes.py index 8b504ea6..da3989e5 100644 --- a/hello/hello_search_attributes.py +++ b/hello/hello_search_attributes.py @@ -3,6 +3,7 @@ from temporalio import workflow from temporalio.client import Client from temporalio.worker import Worker +from temporalio.common import SearchAttributeKey @workflow.defn @@ -11,7 +12,9 @@ class GreetingWorkflow: async def run(self) -> None: # Wait a couple seconds, then alter the keyword search attribute await asyncio.sleep(2) - workflow.upsert_search_attributes({"CustomKeywordField": ["new-value"]}) + workflow.upsert_search_attributes([ + SearchAttributeKey.for_keyword("CustomKeywordField").value_set("new-value") + ]) async def main(): @@ -28,23 +31,26 @@ async def main(): # While the worker is running, use the client to start the workflow. # Note, in many production setups, the client would be in a completely # separate process from the worker. + custom_keyword_field = SearchAttributeKey.for_keyword("CustomKeywordField") + handle = await client.start_workflow( GreetingWorkflow.run, id="hello-search-attributes-workflow-id", task_queue="hello-search-attributes-task-queue", # Start with default set of search attributes - search_attributes={"CustomKeywordField": ["old-value"]}, + search_attributes=custom_keyword_field.value_set("old-value"), ) # Show search attributes before and after a few seconds + custom_keyword_field = SearchAttributeKey.for_keyword("CustomKeywordField") print( "First search attribute values: ", - (await handle.describe()).search_attributes.get("CustomKeywordField"), + (await handle.describe()).typed_search_attributes.get(custom_keyword_field), ) await asyncio.sleep(3) print( "Second search attribute values: ", - (await handle.describe()).search_attributes.get("CustomKeywordField"), + (await handle.describe()).typed_search_attributes.get(custom_keyword_field), ) From 856db1ffde676c0370c0cca7b94d80e52f9175ce Mon Sep 17 00:00:00 2001 From: Kevin Pham <161087761+accupham@users.noreply.github.com> Date: Wed, 16 Oct 2024 12:19:22 -0700 Subject: [PATCH 2/2] Update hello_search_attributes.py define at top of workflow for reuse. --- hello/hello_search_attributes.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/hello/hello_search_attributes.py b/hello/hello_search_attributes.py index da3989e5..14e5f39b 100644 --- a/hello/hello_search_attributes.py +++ b/hello/hello_search_attributes.py @@ -5,6 +5,9 @@ from temporalio.worker import Worker from temporalio.common import SearchAttributeKey +# define our custom search attribute to modify later in the workflow +custom_keyword_field = SearchAttributeKey.for_keyword("CustomKeywordField") + @workflow.defn class GreetingWorkflow: @@ -13,7 +16,7 @@ async def run(self) -> None: # Wait a couple seconds, then alter the keyword search attribute await asyncio.sleep(2) workflow.upsert_search_attributes([ - SearchAttributeKey.for_keyword("CustomKeywordField").value_set("new-value") + custom_keyword_field.value_set("new-value") ]) @@ -31,7 +34,6 @@ async def main(): # While the worker is running, use the client to start the workflow. # Note, in many production setups, the client would be in a completely # separate process from the worker. - custom_keyword_field = SearchAttributeKey.for_keyword("CustomKeywordField") handle = await client.start_workflow( GreetingWorkflow.run, @@ -42,7 +44,6 @@ async def main(): ) # Show search attributes before and after a few seconds - custom_keyword_field = SearchAttributeKey.for_keyword("CustomKeywordField") print( "First search attribute values: ", (await handle.describe()).typed_search_attributes.get(custom_keyword_field),