Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit044b1de

Browse files
authored
Add workflow.instance() API for obtaining current workflow instance (temporalio#739)
1 parent150878f commit044b1de

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

‎temporalio/worker/_workflow_instance.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,9 @@ def workflow_get_update_validator(self, name: Optional[str]) -> Optional[Callabl
10231023
defworkflow_info(self)->temporalio.workflow.Info:
10241024
returnself._outbound.info()
10251025

1026+
defworkflow_instance(self)->Any:
1027+
returnself._object
1028+
10261029
defworkflow_is_continue_as_new_suggested(self)->bool:
10271030
returnself._continue_as_new_suggested
10281031

‎temporalio/workflow.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,9 @@ def workflow_get_update_validator(
625625
@abstractmethod
626626
defworkflow_info(self)->Info: ...
627627

628+
@abstractmethod
629+
defworkflow_instance(self)->Any: ...
630+
628631
@abstractmethod
629632
defworkflow_is_continue_as_new_suggested(self)->bool: ...
630633

@@ -818,6 +821,15 @@ def info() -> Info:
818821
return_Runtime.current().workflow_info()
819822

820823

824+
definstance()->Any:
825+
"""Current workflow's instance.
826+
827+
Returns:
828+
The currently running workflow instance.
829+
"""
830+
return_Runtime.current().workflow_instance()
831+
832+
821833
defmemo()->Mapping[str,Any]:
822834
"""Current workflow's memo values, converted without type hints.
823835

‎tests/worker/test_interceptor.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,44 @@ def pop_trace(name: str, filter: Optional[Callable[[Any], bool]] = None) -> Any:
283283

284284
# Confirm no unexpected traces
285285
assertnotinterceptor_traces
286+
287+
288+
classWorkflowInstanceAccessInterceptor(Interceptor):
289+
defworkflow_interceptor_class(
290+
self,input:WorkflowInterceptorClassInput
291+
)->Optional[Type[WorkflowInboundInterceptor]]:
292+
returnWorkflowInstanceAccessInboundInterceptor
293+
294+
295+
classWorkflowInstanceAccessInboundInterceptor(WorkflowInboundInterceptor):
296+
asyncdefexecute_workflow(self,input:ExecuteWorkflowInput)->int:
297+
# Return integer difference between ids of workflow instance obtained from workflow run method and
298+
# from workflow.instance(). They should be the same, so the difference should be 0.
299+
from_workflow_instance_api=workflow.instance()
300+
assertfrom_workflow_instance_apiisnotNone
301+
id_from_workflow_instance_api=id(from_workflow_instance_api)
302+
id_from_workflow_run_method=awaitsuper().execute_workflow(input)
303+
returnid_from_workflow_run_method-id_from_workflow_instance_api
304+
305+
306+
@workflow.defn
307+
classWorkflowInstanceAccessWorkflow:
308+
@workflow.run
309+
asyncdefrun(self)->int:
310+
returnid(self)
311+
312+
313+
asyncdeftest_workflow_instance_access_from_interceptor(client:Client):
314+
task_queue=f"task_queue_{uuid.uuid4()}"
315+
asyncwithWorker(
316+
client,
317+
task_queue=task_queue,
318+
workflows=[WorkflowInstanceAccessWorkflow],
319+
interceptors=[WorkflowInstanceAccessInterceptor()],
320+
):
321+
difference=awaitclient.execute_workflow(
322+
WorkflowInstanceAccessWorkflow.run,
323+
id=f"workflow_{uuid.uuid4()}",
324+
task_queue=task_queue,
325+
)
326+
assertdifference==0

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp