Skip to content

Commit

Permalink
update-status: only replace the status file if something has changed
Browse files Browse the repository at this point in the history
Before uploading the status file we make a cached request for the old status
content and if there is no difference we don't upload anything.

This reduces the amount of write API calls and the amount of useless
packages.msys2.org refreshes a bit.
  • Loading branch information
lazka committed Aug 16, 2023
1 parent 1f1faba commit a3a5c1d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
4 changes: 2 additions & 2 deletions msys2_autobuild/gh.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ def asset_is_complete(asset: GitReleaseAsset) -> bool:
return asset.state == "uploaded"


def download_text_asset(asset: GitReleaseAsset) -> str:
def download_text_asset(asset: GitReleaseAsset, cache=False) -> str:
assert asset_is_complete(asset)
session = get_requests_session(nocache=True)
session = get_requests_session(nocache=not cache)
with session.get(asset.browser_download_url, timeout=REQUESTS_TIMEOUT) as r:
r.raise_for_status()
return r.text
Expand Down
39 changes: 27 additions & 12 deletions msys2_autobuild/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from .config import (REQUESTS_TIMEOUT, ArchType, BuildType, Config,
build_type_is_src, get_all_build_types)
from .gh import (CachedAssets, download_text_asset, get_asset_filename,
get_current_repo, get_release, make_writable)
get_current_repo, get_release, make_writable,
asset_is_complete)
from .utils import get_requests_session, queue_website_update


Expand Down Expand Up @@ -382,22 +383,36 @@ def update_status(pkgs: List[Package]) -> None:

content = json.dumps(status_object, indent=2).encode()

# If multiple jobs update this at the same time things can fail
# If multiple jobs update this at the same time things can fail,
# assume the other one went through and just ignore all errors
try:
asset = None
asset_name = "status.json"
for asset in release.assets:
if asset.name == asset_name:
break

do_replace = True

# Avoid uploading the same file twice, to reduce API write calls
if asset is not None and asset_is_complete(asset) and asset.size == len(content):
old_content = download_text_asset(asset, cache=True)
if old_content == content.decode():
do_replace = False

if do_replace:
if asset is not None:
with make_writable(asset):
asset.delete_asset()
break
with io.BytesIO(content) as fileobj:
with make_writable(release):
new_asset = release.upload_asset_from_memory( # type: ignore
fileobj, len(content), asset_name)
except (GithubException, requests.RequestException) as e:
print(e)
return

print(f"Uploaded status file for {len(packages)} packages: {new_asset.browser_download_url}")
with io.BytesIO(content) as fileobj:
with make_writable(release):
new_asset = release.upload_asset_from_memory( # type: ignore
fileobj, len(content), asset_name)

queue_website_update()
print(f"Uploaded status file for {len(packages)} packages: {new_asset.browser_download_url}")
queue_website_update()
else:
print("Status unchanged")
except (GithubException, requests.RequestException) as e:
print(e)

0 comments on commit a3a5c1d

Please sign in to comment.