Skip to content

Commit 42505ae

Browse files
authored
Merge pull request #14 from release-engineering/azure_wait_publish
Azure: Wait to publish during publish collision
2 parents 7b5ed6f + ac1310a commit 42505ae

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

Diff for: cloudpub/ms_azure/service.py

+8
Original file line numberDiff line numberDiff line change
@@ -440,10 +440,18 @@ def submit_to_status(self, product_id: str, status: str) -> ConfigureStatus:
440440

441441
return self.configure(resource=submission)
442442

443+
@retry(
444+
wait=wait_fixed(300),
445+
stop=stop_after_delay(max_delay=60 * 60 * 24 * 7), # Give up after retrying for 7 days,
446+
reraise=True,
447+
)
443448
def ensure_can_publish(self, product_id: str) -> None:
444449
"""
445450
Ensure the offer is not already being published.
446451
452+
It will wait for up to 7 days retrying to make sure it's possible to publish before
453+
giving up and raising.
454+
447455
Args:
448456
product_id (str)
449457
The product ID to check the offer's publishing status

Diff for: tests/ms_azure/test_service.py

+47
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from httmock import response
99
from requests import Response
1010
from requests.exceptions import HTTPError
11+
from tenacity.stop import stop_after_attempt
1112

1213
from cloudpub.common import BaseService
1314
from cloudpub.error import InvalidStateError, NotFoundError
@@ -730,6 +731,8 @@ def test_ensure_can_publish_success(
730731
"created": "2024-07-04T22:06:16.2895521Z",
731732
}
732733
mock_getsubst.return_value = ProductSubmission.from_json(submission)
734+
azure_service.ensure_can_publish.retry.sleep = mock.MagicMock() # type: ignore
735+
azure_service.ensure_can_publish.retry.stop = stop_after_attempt(1) # type: ignore
733736

734737
azure_service.ensure_can_publish("ffffffff-ffff-ffff-ffff-ffffffffffff")
735738

@@ -741,6 +744,48 @@ def test_ensure_can_publish_success(
741744
]
742745
)
743746

747+
@pytest.mark.parametrize("target", ["preview", "live"])
748+
@mock.patch("cloudpub.ms_azure.AzureService.get_submission_state")
749+
def test_ensure_can_publish_success_after_retry(
750+
self,
751+
mock_getsubst: mock.MagicMock,
752+
target: str,
753+
azure_service: AzureService,
754+
) -> None:
755+
running = {
756+
"$schema": "https://product-ingestion.azureedge.net/schema/submission/2022-03-01-preview2", # noqa: E501
757+
"id": "submission/ffffffff-ffff-ffff-ffff-ffffffffffff/0",
758+
"product": "product/ffffffff-ffff-ffff-ffff-ffffffffffff",
759+
"target": {"targetType": target},
760+
"lifecycleState": "generallyAvailable",
761+
"status": "running",
762+
"result": "pending",
763+
"created": "2024-07-04T22:06:16.2895521Z",
764+
}
765+
complete = {
766+
"$schema": "https://product-ingestion.azureedge.net/schema/submission/2022-03-01-preview2", # noqa: E501
767+
"id": "submission/ffffffff-ffff-ffff-ffff-ffffffffffff/0",
768+
"product": "product/ffffffff-ffff-ffff-ffff-ffffffffffff",
769+
"target": {"targetType": target},
770+
"lifecycleState": "generallyAvailable",
771+
"status": "completed",
772+
"result": "succeeded",
773+
"created": "2024-07-04T22:06:16.2895521Z",
774+
}
775+
mock_getsubst.side_effect = [
776+
ProductSubmission.from_json(running),
777+
ProductSubmission.from_json(running),
778+
ProductSubmission.from_json(complete),
779+
ProductSubmission.from_json(complete),
780+
]
781+
azure_service.ensure_can_publish.retry.sleep = mock.MagicMock() # type: ignore
782+
azure_service.ensure_can_publish.retry.stop = stop_after_attempt(3) # type: ignore
783+
784+
azure_service.ensure_can_publish("ffffffff-ffff-ffff-ffff-ffffffffffff")
785+
786+
# Calls for "live" and "preview" for 2 times before success == 4
787+
assert mock_getsubst.call_count == 4
788+
744789
@pytest.mark.parametrize("target", ["preview", "live"])
745790
@mock.patch("cloudpub.ms_azure.AzureService.get_submission_state")
746791
def test_ensure_can_publish_raises(
@@ -782,6 +827,8 @@ def test_ensure_can_publish_raises(
782827
err = (
783828
f"The offer ffffffff-ffff-ffff-ffff-ffffffffffff is already being published to {target}"
784829
)
830+
azure_service.ensure_can_publish.retry.sleep = mock.MagicMock() # type: ignore
831+
azure_service.ensure_can_publish.retry.stop = stop_after_attempt(1) # type: ignore
785832

786833
with pytest.raises(RuntimeError, match=err):
787834
azure_service.ensure_can_publish("ffffffff-ffff-ffff-ffff-ffffffffffff")

0 commit comments

Comments
 (0)