Skip to content

Commit 9348922

Browse files
committed
udpate with comments
1 parent 8d9925e commit 9348922

File tree

2 files changed

+30
-51
lines changed

2 files changed

+30
-51
lines changed

azure/durable_functions/models/DurableOrchestrationClient.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -813,21 +813,27 @@ async def restart(self, instance_id: str,
813813
str
814814
The instance ID of the restarted orchestration.
815815
"""
816-
status = await self.get_status(instance_id, show_input=True)
816+
restart_with_new_instance_id_str = str(restart_with_new_instance_id).lower()
817+
request_url = f"{self._orchestration_bindings.rpc_base_url}instances/{instance_id}/" \
818+
f"restart?restartWithNewInstanceId={restart_with_new_instance_id_str}"
819+
response = await self._post_async_request(
820+
request_url,
821+
None,
822+
function_invocation_id=self._function_invocation_id)
823+
switch_statement = {
824+
202: lambda: None, # instance is restarted
825+
410: lambda: None, # instance completed
826+
404: lambda: f"No instance with ID '{instance_id}' found.",
827+
}
817828

818-
if not status or status.name is None:
819-
raise Exception(
820-
f"An orchestration with the instanceId {instance_id} was not found.")
829+
has_error_message = switch_statement.get(
830+
response[0],
831+
lambda: f"The operation failed with an unexpected status code {response[0]}")
832+
error_message = has_error_message()
833+
if error_message:
834+
raise Exception(error_message)
821835

822-
if restart_with_new_instance_id:
823-
return await self.start_new(
824-
orchestration_function_name=status.name,
825-
client_input=status.input_)
826-
else:
827-
return await self.start_new(
828-
orchestration_function_name=status.name,
829-
instance_id=status.instance_id,
830-
client_input=status.input_)
836+
return response[1] if response[1] else instance_id
831837

832838
async def resume(self, instance_id: str, reason: str) -> None:
833839
"""Resume the specified orchestration instance.

tests/models/test_DurableOrchestrationClient.py

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -748,27 +748,14 @@ async def test_post_500_resume(binding_string):
748748

749749
@pytest.mark.asyncio
750750
async def test_restart_with_new_instance_id(binding_string):
751-
"""Test restart creates a new instance with a new ID by default."""
752-
orchestrator_name = "MyOrchestrator"
753-
original_input = {"key": "value"}
751+
"""Test restart calls the HTTP restart endpoint with restartWithNewInstanceId=true."""
754752
new_instance_id = "new-instance-id-1234"
755753

756-
get_mock = MockRequest(
757-
expected_url=f"{RPC_BASE_URL}instances/{TEST_INSTANCE_ID}?showInput=True",
758-
response=[200, dict(
759-
name=orchestrator_name,
760-
instanceId=TEST_INSTANCE_ID,
761-
createdTime=TEST_CREATED_TIME,
762-
lastUpdatedTime=TEST_LAST_UPDATED_TIME,
763-
runtimeStatus="Completed",
764-
input=original_input)])
765-
766754
post_mock = MockRequest(
767-
expected_url=f"{RPC_BASE_URL}orchestrators/{orchestrator_name}",
768-
response=[202, {"id": new_instance_id}])
755+
expected_url=f"{RPC_BASE_URL}instances/{TEST_INSTANCE_ID}/restart?restartWithNewInstanceId=true",
756+
response=[202, new_instance_id])
769757

770758
client = DurableOrchestrationClient(binding_string)
771-
client._get_async_request = get_mock.get
772759
client._post_async_request = post_mock.post
773760

774761
result = await client.restart(TEST_INSTANCE_ID)
@@ -777,26 +764,12 @@ async def test_restart_with_new_instance_id(binding_string):
777764

778765
@pytest.mark.asyncio
779766
async def test_restart_with_same_instance_id(binding_string):
780-
"""Test restart reuses the original instance ID when restartWithNewInstanceId is False."""
781-
orchestrator_name = "MyOrchestrator"
782-
original_input = {"key": "value"}
783-
784-
get_mock = MockRequest(
785-
expected_url=f"{RPC_BASE_URL}instances/{TEST_INSTANCE_ID}?showInput=True",
786-
response=[200, dict(
787-
name=orchestrator_name,
788-
instanceId=TEST_INSTANCE_ID,
789-
createdTime=TEST_CREATED_TIME,
790-
lastUpdatedTime=TEST_LAST_UPDATED_TIME,
791-
runtimeStatus="Completed",
792-
input=original_input)])
793-
767+
"""Test restart calls the HTTP restart endpoint with restartWithNewInstanceId=false."""
794768
post_mock = MockRequest(
795-
expected_url=f"{RPC_BASE_URL}orchestrators/{orchestrator_name}/{TEST_INSTANCE_ID}",
796-
response=[202, {"id": TEST_INSTANCE_ID}])
769+
expected_url=f"{RPC_BASE_URL}instances/{TEST_INSTANCE_ID}/restart?restartWithNewInstanceId=false",
770+
response=[202, TEST_INSTANCE_ID])
797771

798772
client = DurableOrchestrationClient(binding_string)
799-
client._get_async_request = get_mock.get
800773
client._post_async_request = post_mock.post
801774

802775
result = await client.restart(TEST_INSTANCE_ID, restart_with_new_instance_id=False)
@@ -806,16 +779,16 @@ async def test_restart_with_same_instance_id(binding_string):
806779
@pytest.mark.asyncio
807780
async def test_restart_instance_not_found(binding_string):
808781
"""Test restart raises exception when instance is not found."""
809-
get_mock = MockRequest(
810-
expected_url=f"{RPC_BASE_URL}instances/{TEST_INSTANCE_ID}?showInput=True",
811-
response=[404, dict(createdTime=None, lastUpdatedTime=None)])
782+
post_mock = MockRequest(
783+
expected_url=f"{RPC_BASE_URL}instances/{TEST_INSTANCE_ID}/restart?restartWithNewInstanceId=true",
784+
response=[404, None])
812785

813786
client = DurableOrchestrationClient(binding_string)
814-
client._get_async_request = get_mock.get
787+
client._post_async_request = post_mock.post
815788

816789
with pytest.raises(Exception) as ex:
817790
await client.restart(TEST_INSTANCE_ID)
818-
assert f"instanceId {TEST_INSTANCE_ID} was not found" in str(ex.value)
791+
assert f"No instance with ID '{TEST_INSTANCE_ID}' found." in str(ex.value)
819792
# Tests for function_invocation_id parameter
820793
def test_client_stores_function_invocation_id(binding_string):
821794
"""Test that the client stores the function_invocation_id parameter."""

0 commit comments

Comments
 (0)