Skip to content

Commit

Permalink
Merge pull request #30 from snok/sondrelg/timeout-handling
Browse files Browse the repository at this point in the history
Handle timeouts
  • Loading branch information
sondrelg authored Apr 3, 2022
2 parents fdd5728 + 406def1 commit 46881d5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/ambv/black
rev: 21.12b0
rev: 22.3.0
hooks:
- id: black
args: ['--quiet']
Expand All @@ -24,7 +24,7 @@ repos:
- id: mixed-line-ending
- id: trailing-whitespace
- repo: https://github.com/asottile/pyupgrade
rev: v2.31.0
rev: v2.31.1
hooks:
- id: pyupgrade
args: ['--py3-plus', '--py36-plus', '--py37-plus', '--keep-runtime-typing']
Expand All @@ -40,7 +40,7 @@ repos:
'flake8-type-checking',
]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v0.931'
rev: 'v0.942'
hooks:
- id: mypy
additional_dependencies: [types-dateparser]
28 changes: 20 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from urllib.parse import quote_from_bytes

from dateparser import parse
from httpx import AsyncClient
from httpx import AsyncClient, TimeoutException
from pydantic import BaseModel, conint, validator

if TYPE_CHECKING:
Expand Down Expand Up @@ -128,9 +128,11 @@ async def delete_org_package_versions(
await semaphore.acquire()
try:
response = await http_client.delete(url)
post_deletion_output(response=response, image_name=image_name, version_id=version_id)
except TimeoutException as e:
print(f'Request to delete {image_name.value} timed out with error `{e}`')
finally:
semaphore.release()
post_deletion_output(response=response, image_name=image_name, version_id=version_id)


async def delete_package_versions(
Expand All @@ -148,9 +150,11 @@ async def delete_package_versions(
await semaphore.acquire()
try:
response = await http_client.delete(url)
post_deletion_output(response=response, image_name=image_name, version_id=version_id)
except TimeoutException as e:
print(f'Request to delete {image_name.value} timed out with error `{e}`')
finally:
semaphore.release()
post_deletion_output(response=response, image_name=image_name, version_id=version_id)


class GithubAPI:
Expand Down Expand Up @@ -221,10 +225,7 @@ def parse_image_names(cls, v: str) -> list[ImageName]:

@validator('skip_tags', 'filter_tags', pre=True)
def parse_comma_separate_string_as_list(cls, v: str) -> list[str]:
if not v:
return []
else:
return [i.strip() for i in v.split(',')]
return [] if not v else [i.strip() for i in v.split(',')]

@validator('cut_off', pre=True)
def parse_human_readable_datetime(cls, v: str) -> datetime:
Expand Down Expand Up @@ -330,7 +331,18 @@ async def get_and_delete_old_versions(image_name: ImageName, inputs: Inputs, htt
if not tasks:
print(f'No more versions to delete for {image_name.value}')

await asyncio.gather(*tasks)
results = await asyncio.gather(*tasks, return_exceptions=True)

for item in results:
if isinstance(item, Exception):
try:
raise item
except Exception as e:
# Unhandled errors *shouldn't* occur
print(
f'Unhandled exception raised at runtime: `{e}`. '
f'Please report this at https://github.com/snok/container-retention-policy/issues/new'
)


async def main(
Expand Down

0 comments on commit 46881d5

Please sign in to comment.