Skip to content

Commit

Permalink
Quit on startup if error fetching metadata, closes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jul 29, 2021
1 parent d8cdfd5 commit 24479bc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
10 changes: 7 additions & 3 deletions datasette_remote_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import random


async def update_remote_with_time_limit(datasette, timelimit=0.2):
async def update_remote_with_time_limit(datasette, timelimit=None):
# Update datasette._remote_metadata from URL - returns when done OR
# when it hits time limit. If the time limit is hit it will still
# perform the update once the request has completed
Expand All @@ -28,20 +28,24 @@ async def update_remote():
fetch_url,
headers=dict(headers, **{"Cache-Control": "no-cache"}),
)
response.raise_for_status()
metadata = parse_metadata(response.content)
datasette._remote_metadata = metadata
datasette._remote_metadata_last_updated = time.monotonic()

try:
await asyncio.wait_for(asyncio.shield(update_remote()), timeout=timelimit)
if timelimit is not None:
await asyncio.wait_for(asyncio.shield(update_remote()), timeout=timelimit)
else:
await update_remote()
except asyncio.exceptions.TimeoutError:
pass


@hookimpl
def startup(datasette):
async def inner():
await update_remote_with_time_limit(datasette, timelimit=0.2)
await update_remote_with_time_limit(datasette)

return inner

Expand Down
13 changes: 13 additions & 0 deletions tests/test_remote_metadata.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from time import sleep
from datasette.app import Datasette
import httpx
import pytest

TEST_URL = "https://www.example.com/metadata.yml"
Expand Down Expand Up @@ -100,3 +101,15 @@ async def test_cachebust(httpx_mock, already_has_querystring):
assert "?foo=bar&0." in url
else:
assert "?0." in url


@pytest.mark.asyncio
async def test_fails_if_error_on_startup(httpx_mock):
httpx_mock.add_response(status_code=404)
datasette = Datasette(
[],
memory=True,
metadata={"plugins": {"datasette-remote-metadata": {"url": TEST_URL}}},
)
with pytest.raises(httpx.HTTPStatusError):
await datasette.invoke_startup()

0 comments on commit 24479bc

Please sign in to comment.