-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
exclude examples from build and add fastapi example
- Loading branch information
1 parent
0f25ed7
commit 67e16f7
Showing
2 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import random | ||
|
||
import requests | ||
from fastapi import APIRouter, status | ||
from fastapi.responses import PlainTextResponse, Response | ||
|
||
from retry_later import retry_later | ||
|
||
routers = APIRouter() | ||
|
||
|
||
@routers.get( | ||
"/health", | ||
response_model=str, | ||
description="health check", | ||
name="health", | ||
response_class=PlainTextResponse, | ||
) | ||
async def health() -> Response: | ||
doesnt_exist_url: str = "https://spacehook-4-o4569495.deta.app/api/hook/doesnt-exist" | ||
exists_url: str = "https://spacehook-4-o4569495.deta.app/api/hook/ok" | ||
url_choices_tuple = (doesnt_exist_url, exists_url) | ||
await call_hook_from_choice(url_choices_tuple) | ||
return Response( | ||
content="OK", | ||
status_code=status.HTTP_200_OK, | ||
headers={"content-Type": "text/plain"}, | ||
) | ||
|
||
|
||
@retry_later(exceptions=ConnectionError, max_retries=10) | ||
async def call_hook_from_choice(url_choices: tuple[str]): | ||
response = requests.get(random.choice(url_choices)) | ||
if response.status_code == 404: | ||
raise ConnectionError(f"Could not connect. Response: {response.status_code}") | ||
else: | ||
print("Called endpoint successfully!") | ||
|
||
|
||
"""Console Logs: | ||
INFO: 127.0.0.1:56600 - "GET /api/v1/health HTTP/1.1" 200 OK | ||
ERROR:root:[retry later] Retrying in 5s due to Could not connect. Response: 404 | ||
ERROR:root:[retry later] Retrying in 5s due to Could not connect. Response: 404 | ||
Called endpoint successfully! | ||
""" | ||
|
||
""" | ||
You can see that the retry happens after the /health endpoint response is sent. | ||
It doesn't wait for call_hook_from_choice to execute. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters