-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from communitiesuk/fs-1306-healthchecks
Fs 1306 healthchecks
- Loading branch information
Showing
14 changed files
with
336 additions
and
18 deletions.
There are no files selected for viewing
33 changes: 28 additions & 5 deletions
33
.github/workflows/create-tag-workflow.yml → .github/workflows/test-and-tag.yml
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
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 |
---|---|---|
|
@@ -78,6 +78,9 @@ Temporary Items | |
# *.iml | ||
# *.ipr | ||
|
||
# VS Code | ||
.vscode/ | ||
|
||
# CMake | ||
cmake-build-*/ | ||
|
||
|
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
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 |
---|---|---|
@@ -1,7 +1,15 @@ | ||
from fsd_utils import authentication # noqa | ||
from fsd_utils import gunicorn # noqa | ||
from fsd_utils import healthchecks | ||
from fsd_utils import logging # noqa | ||
from fsd_utils.config.configclass import configclass # noqa | ||
from fsd_utils.config.commonconfig import CommonConfig # noqa | ||
from fsd_utils.config.configclass import configclass # noqa | ||
|
||
__all__ = [configclass, logging, gunicorn, authentication, CommonConfig] | ||
__all__ = [ | ||
configclass, | ||
logging, | ||
gunicorn, | ||
authentication, | ||
CommonConfig, | ||
healthchecks, | ||
] |
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
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,2 @@ | ||
from . import checkers # noqa | ||
from . import healthcheck # noqa |
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,35 @@ | ||
from typing import Tuple | ||
|
||
from flask import current_app | ||
|
||
|
||
class CheckerInterface: | ||
def check(self) -> Tuple[bool, str]: | ||
pass | ||
|
||
|
||
class FlaskRunningChecker(CheckerInterface): | ||
def __init__(self): | ||
self.name = "check_running" | ||
|
||
def check(self): | ||
if current_app: | ||
return True, "OK" | ||
else: | ||
return False, "Fail" | ||
|
||
|
||
class DbChecker(CheckerInterface): | ||
def __init__(self, db): | ||
self.db = db | ||
self.name = "check_db" | ||
|
||
def check(self): | ||
from sqlalchemy.exc import SQLAlchemyError | ||
|
||
try: | ||
self.db.session.execute("SELECT 1") | ||
return True, "OK" | ||
except SQLAlchemyError: | ||
current_app.logger.exception("DB Check failed") | ||
return False, "Fail" |
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,35 @@ | ||
from flask import current_app | ||
|
||
|
||
class Healthcheck(object): | ||
def __init__(self, app): | ||
self.flask_app = app | ||
self.flask_app.add_url_rule( | ||
"/healthcheck", view_func=self.healthcheck_view | ||
) | ||
self.checkers = [] | ||
|
||
def healthcheck_view(self): | ||
responseCode = 200 | ||
response = {"checks": []} | ||
for checker in self.checkers: | ||
try: | ||
result = checker.check() | ||
current_app.logger.debug( | ||
f"Check {checker.name} returned {result}" | ||
) | ||
response["checks"].append({checker.name: result[1]}) | ||
if result[0] is False: | ||
responseCode = 500 | ||
except Exception: | ||
response["checks"].append( | ||
{checker.name: "Failed - check logs"} | ||
) | ||
current_app.logger.exception( | ||
f"Check {checker.name} failed with an exception" | ||
) | ||
responseCode = 500 | ||
return response, responseCode | ||
|
||
def add_check(self, checker): | ||
self.checkers.append(checker) |
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,4 @@ | ||
[pytest] | ||
env = | ||
FLASK_ENV=unit_test | ||
FLASK_DEBUG=1 |
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,5 @@ | ||
-e . | ||
|
||
pre-commit | ||
pytest-env>=0.6.2 | ||
sqlalchemy==1.4.39 |
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
Empty file.
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,23 @@ | ||
""" | ||
Contains test configuration. | ||
""" | ||
import pytest | ||
from flask import Flask | ||
|
||
|
||
def create_app(): | ||
app = Flask("test") | ||
return app | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def flask_test_client(): | ||
""" | ||
Creates the test client we will be using to test the responses | ||
from our app, this is a test fixture. | ||
:return: A flask test client. | ||
""" | ||
|
||
with create_app().app_context() as app_context: | ||
with app_context.app.test_client() as test_client: | ||
yield test_client |
Oops, something went wrong.