Skip to content

Commit 391f9ba

Browse files
authored
Allowing for async checks in fastApi. Cleaning up old code. (#114)
* Allowing for async checks in fastApi. Cleaning up old code. Fixes #106 * Added a few unit tests for fast_api
1 parent 0424734 commit 391f9ba

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

src/dockerflow/checks/registry.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,6 @@
1717
_REGISTERED_CHECKS = {}
1818

1919

20-
def _iscoroutinefunction_or_partial(obj):
21-
"""
22-
Determine if the provided object is a coroutine function or a partial function
23-
that wraps a coroutine function.
24-
25-
This function should be removed when we drop support for Python 3.7, as this is
26-
handled directly by `inspect.iscoroutinefunction` in Python 3.8.
27-
"""
28-
while isinstance(obj, functools.partial):
29-
obj = obj.func
30-
return inspect.iscoroutinefunction(obj)
31-
32-
3320
def register(func=None, name=None):
3421
"""
3522
Register a check callback to be executed from
@@ -43,7 +30,7 @@ def register(func=None, name=None):
4330

4431
logger.debug("Register Dockerflow check %s", name)
4532

46-
if _iscoroutinefunction_or_partial(func):
33+
if inspect.iscoroutinefunction(func):
4734

4835
@functools.wraps(func)
4936
async def decorated_function_asyc(*args, **kwargs):
@@ -116,7 +103,7 @@ class ChecksResults:
116103

117104
async def _run_check_async(check):
118105
name, check_fn = check
119-
if _iscoroutinefunction_or_partial(check_fn):
106+
if inspect.iscoroutinefunction(check_fn):
120107
errors = await check_fn()
121108
else:
122109
loop = asyncio.get_event_loop()

src/dockerflow/fastapi/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ def lbheartbeat():
1111
return {"status": "ok"}
1212

1313

14-
def heartbeat(request: Request, response: Response):
14+
async def heartbeat(request: Request, response: Response):
1515
FAILED_STATUS_CODE = int(
1616
getattr(request.app.state, "DOCKERFLOW_HEARTBEAT_FAILED_STATUS_CODE", "500")
1717
)
1818

19-
check_results = checks.run_checks(
19+
check_results = await checks.run_checks_async(
2020
checks.get_checks().items(),
2121
)
2222

tests/fastapi/test_fastapi.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,53 @@ def return_error():
194194
},
195195
}
196196

197+
def test_heartbeat_sync(client):
198+
@checks.register
199+
def sync_ok():
200+
return []
201+
202+
response = client.get("/__heartbeat__")
203+
assert response.status_code == 200
204+
assert response.json() == {
205+
"status": "ok",
206+
"checks": {"sync_ok": "ok"},
207+
"details": {},
208+
}
209+
210+
211+
def test_heartbeat_async(client):
212+
@checks.register
213+
async def async_ok():
214+
return []
215+
216+
response = client.get("/__heartbeat__")
217+
assert response.status_code == 200
218+
assert response.json() == {
219+
"status": "ok",
220+
"checks": {"async_ok": "ok"},
221+
"details": {},
222+
}
223+
224+
225+
def test_heartbeat_mixed_sync(client):
226+
@checks.register
227+
def sync_ok():
228+
return []
229+
@checks.register
230+
async def async_ok():
231+
return []
232+
233+
response = client.get("/__heartbeat__")
234+
assert response.status_code == 200
235+
assert response.json() == {
236+
"status": "ok",
237+
"checks": {
238+
"sync_ok": "ok",
239+
"async_ok": "ok",
240+
},
241+
"details": {},
242+
}
243+
197244

198245
def test_heartbeat_head(client):
199246
@checks.register

0 commit comments

Comments
 (0)