1+ import simplejson as json
12import traceback
23
34from datetime import datetime
5+ from typing import Any
46from google .protobuf import timestamp_pb2 , wrappers_pb2
57
68from durabletask .protos .orchestrator_service_pb2 import *
79
10+ # TODO: The new_xxx_event methods are only used by test code and should be moved elsewhere
11+
812
913def new_orchestrator_started_event (timestamp : datetime | None = None ) -> HistoryEvent :
1014 ts = timestamp_pb2 .Timestamp ()
@@ -13,19 +17,15 @@ def new_orchestrator_started_event(timestamp: datetime | None = None) -> History
1317 return HistoryEvent (eventId = - 1 , timestamp = ts , orchestratorStarted = OrchestratorStartedEvent ())
1418
1519
16- def new_execution_started_event (name : str , instance_id : str , input : str | None = None ) -> HistoryEvent :
17- input_ : wrappers_pb2 .StringValue | None = None
18- if input is not None :
19- input_ = wrappers_pb2 .StringValue (value = input )
20-
20+ def new_execution_started_event (name : str , instance_id : str , encoded_input : str | None = None ) -> HistoryEvent :
2121 return HistoryEvent (
2222 eventId = - 1 ,
2323 timestamp = timestamp_pb2 .Timestamp (),
2424 executionStarted = ExecutionStartedEvent (
25- name = name , input = input_ , orchestrationInstance = OrchestrationInstance (instanceId = instance_id )))
25+ name = name , input = get_string_value ( encoded_input ) , orchestrationInstance = OrchestrationInstance (instanceId = instance_id )))
2626
2727
28- def new_timer_created_event (timer_id : int , fire_at : datetime ):
28+ def new_timer_created_event (timer_id : int , fire_at : datetime ) -> HistoryEvent :
2929 ts = timestamp_pb2 .Timestamp ()
3030 ts .FromDatetime (fire_at )
3131 return HistoryEvent (
@@ -35,7 +35,7 @@ def new_timer_created_event(timer_id: int, fire_at: datetime):
3535 )
3636
3737
38- def new_timer_fired_event (timer_id : int , fire_at : datetime ):
38+ def new_timer_fired_event (timer_id : int , fire_at : datetime ) -> HistoryEvent :
3939 ts = timestamp_pb2 .Timestamp ()
4040 ts .FromDatetime (fire_at )
4141 return HistoryEvent (
@@ -45,6 +45,30 @@ def new_timer_fired_event(timer_id: int, fire_at: datetime):
4545 )
4646
4747
48+ def new_task_scheduled_event (event_id : int , name : str , encoded_input : str | None = None ) -> HistoryEvent :
49+ return HistoryEvent (
50+ eventId = event_id ,
51+ timestamp = timestamp_pb2 .Timestamp (),
52+ taskScheduled = TaskScheduledEvent (name = name , input = get_string_value (encoded_input ))
53+ )
54+
55+
56+ def new_task_completed_event (event_id : int , encoded_output : str | None = None ) -> HistoryEvent :
57+ return HistoryEvent (
58+ eventId = - 1 ,
59+ timestamp = timestamp_pb2 .Timestamp (),
60+ taskCompleted = TaskCompletedEvent (taskScheduledId = event_id , result = get_string_value (encoded_output ))
61+ )
62+
63+
64+ def new_task_failed_event (event_id : int , ex : Exception ) -> HistoryEvent :
65+ return HistoryEvent (
66+ eventId = - 1 ,
67+ timestamp = timestamp_pb2 .Timestamp (),
68+ taskFailed = TaskFailedEvent (taskScheduledId = event_id , failureDetails = new_failure_details (ex ))
69+ )
70+
71+
4872def new_failure_details (ex : Exception ) -> TaskFailureDetails :
4973 return TaskFailureDetails (
5074 errorType = type (ex ).__name__ ,
@@ -53,19 +77,22 @@ def new_failure_details(ex: Exception) -> TaskFailureDetails:
5377 )
5478
5579
80+ def get_string_value (val : str | None ) -> wrappers_pb2 .StringValue | None :
81+ if val is None :
82+ return None
83+ else :
84+ return wrappers_pb2 .StringValue (value = val )
85+
86+
5687def new_complete_orchestration_action (
5788 id : int ,
5889 status : OrchestrationStatus ,
5990 result : str | None = None ,
6091 failure_details : TaskFailureDetails | None = None ) -> OrchestratorAction :
6192
62- result_pb : wrappers_pb2 .StringValue | None = None
63- if result is not None :
64- result_pb = wrappers_pb2 .StringValue (value = result )
65-
6693 completeOrchestrationAction = CompleteOrchestrationAction (
6794 orchestrationStatus = status ,
68- result = result_pb ,
95+ result = get_string_value ( result ) ,
6996 failureDetails = failure_details )
7097
7198 # TODO: CarryoverEvents
@@ -77,3 +104,21 @@ def new_create_timer_action(id: int, fire_at: datetime) -> OrchestratorAction:
77104 timestamp = timestamp_pb2 .Timestamp ()
78105 timestamp .FromDatetime (fire_at )
79106 return OrchestratorAction (id = id , createTimer = CreateTimerAction (fireAt = timestamp ))
107+
108+
109+ def new_schedule_task_action (id : int , name : str , input : Any ) -> OrchestratorAction :
110+ encoded_input = json .dumps (input ) if input is not None else None
111+ return OrchestratorAction (id = id , scheduleTask = ScheduleTaskAction (
112+ name = name ,
113+ input = get_string_value (encoded_input )
114+ ))
115+
116+
117+ def new_timestamp (dt : datetime ) -> timestamp_pb2 .Timestamp :
118+ ts = timestamp_pb2 .Timestamp ()
119+ ts .FromDatetime (dt )
120+ return ts
121+
122+
123+ def is_empty (v : wrappers_pb2 .StringValue ):
124+ return v is None or v .value == ''
0 commit comments