Skip to content

Commit

Permalink
Fix #1489: fix crash when bundles does not exist yet (#1491)
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem authored Sep 9, 2024
1 parent da7af1c commit 01398f4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
12 changes: 4 additions & 8 deletions commands/build_bundles.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ def fetch_all_changesets(client):


@retry_timeout
def get_modified_timestamp(url):
def get_modified_timestamp(url) -> int:
"""
Return URL modified date as epoch millisecond.
"""
resp = requests.get(url)
if not resp.ok:
return None
filename = url.split("/")[-1]
print(f"No previous '{filename}' bundle found") # happens on first run.
return -1
dts = resp.headers["Last-Modified"]
dt = parsedate_to_datetime(dts)
epoch_msec = int(dt.timestamp() * 1000)
Expand Down Expand Up @@ -193,9 +195,6 @@ def build_bundles(event, context):
existing_bundle_timestamp = get_modified_timestamp(
f"{base_url}{DESTINATION_FOLDER}/changesets.zip"
)
if existing_bundle_timestamp is None:
print("No previous 'changesets.zip' bundle found") # Should only happen once.
existing_bundle_timestamp = -1
print(f"'changesets.zip' was published at {existing_bundle_timestamp}")
if BUILD_ALL or (existing_bundle_timestamp < highest_timestamp):
write_zip(
Expand All @@ -216,9 +215,6 @@ def build_bundles(event, context):
existing_bundle_timestamp = get_modified_timestamp(
f"{base_url}{DESTINATION_FOLDER}/startup.zip"
)
if existing_bundle_timestamp is None:
print("No previous 'startup.zip' bundle found") # Should only happen once.
existing_bundle_timestamp = -1
print(f"'startup.zip' was published at {existing_bundle_timestamp}")
if BUILD_ALL or existing_bundle_timestamp < highest_timestamp:
write_zip(
Expand Down
15 changes: 13 additions & 2 deletions tests/test_build_bundles.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def test_get_modified_timestamp_missing():
url = "http://example.com/file"
responses.add(responses.GET, url, status=404)
timestamp = get_modified_timestamp(url)
assert timestamp is None
assert timestamp == -1


def test_write_zip(tmpdir):
Expand Down Expand Up @@ -201,7 +201,17 @@ def test_build_bundles(mock_fetch_all_changesets, mock_write_zip, mock_sync_clou
"metadata": {"id": "collection5", "bucket": "bucket5", "flags": ["startup"]},
"timestamp": 1720004688000 + 10,
},
{ # collection newly marked as bundled
"changes": [{"id": "record5"}],
"metadata": {"id": "collection6", "bucket": "bucket6", "attachment": {"bundle": True}},
"timestamp": 1720004688000 + 10,
},
]
responses.add(
responses.GET,
f"{server_url}/attachments/bundles/bucket6--collection6.zip",
status=404,
)

build_bundles(event, context={})

Expand All @@ -221,7 +231,7 @@ def test_build_bundles(mock_fetch_all_changesets, mock_write_zip, mock_sync_clou
# Assert the second call (changesets.zip)
changesets_zip_path, changesets_zip_files = calls[1][0]
assert changesets_zip_path == "changesets.zip"
assert len(changesets_zip_files) == 6
assert len(changesets_zip_files) == 7
assert changesets_zip_files[0][0] == "bucket0--collection0.json"
assert changesets_zip_files[1][0] == "bucket1--collection1.json"
assert changesets_zip_files[2][0] == "bucket2--collection2.json"
Expand All @@ -247,6 +257,7 @@ def test_build_bundles(mock_fetch_all_changesets, mock_write_zip, mock_sync_clou
"bucket2--collection2.zip",
"bucket3--collection3.zip",
"bucket5--collection5.zip",
"bucket6--collection6.zip",
],
)

Expand Down

0 comments on commit 01398f4

Please sign in to comment.