"""Logic for Hello. Verify that the application and its dependencies are available. """ from oto import response from sqlalchemy.exc import SQLAlchemyError from product.connectors.mysql import db_session from product.connectors.sentry import sentry_client from product.constants import error from product.models.sql.heartbeat import QUERY_DB_HEARTBEAT def health_check(): """Health check handler. Checks database connection. 200 if OK, otherwise 503 service unavailable. Returns: Response: the system status. """ with db_session() as session: try: session.execute(QUERY_DB_HEARTBEAT) except SQLAlchemyError: if sentry_client: sentry_client.captureException() return response.create_error_response( code=error.INTERNAL_ERROR, message='could not connect to mysql', status=503) return response.Response(message={'status': 'ok'}, status=200)