Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Commit

Permalink
512 db redis health (#686)
Browse files Browse the repository at this point in the history
* catches exceptions from db/redis and provides better errors on startup

* move health endpoint changes to separate branch

* update changelog

* catch appropriate redis err, stop app upon db and cache connection failure

* adds check for cache enabled
  • Loading branch information
eastandwestwind authored Jun 22, 2022
1 parent c222ce0 commit 106f29d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ The types of changes are:
* Added the ability to delete a datastore from the frontend [#683] https://github.com/ethyca/fidesops/pull/683
* Added the ability to disable/enable a datastore from the frontend [#693] https://github.com/ethyca/fidesops/pull/693
* Adds Postgres and Redis health checks to health endpoint [#690](https://github.com/ethyca/fidesops/pull/690)
* Added health checks and better error messages on app startup for both db and cache [#686](https://github.com/ethyca/fidesops/pull/686)

### Changed

Expand Down
18 changes: 16 additions & 2 deletions src/fidesops/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from fastapi import FastAPI, Request, Response
from fastapi.staticfiles import StaticFiles
from fideslog.sdk.python.event import AnalyticsEvent
from redis.exceptions import ResponseError
from starlette.background import BackgroundTask
from starlette.middleware.cors import CORSMiddleware

Expand All @@ -18,12 +19,13 @@
from fidesops.api.v1.api import api_router
from fidesops.api.v1.exception_handlers import ExceptionHandlers
from fidesops.api.v1.urn_registry import V1_URL_PREFIX
from fidesops.common_exceptions import FunctionalityNotConfigured
from fidesops.common_exceptions import FunctionalityNotConfigured, RedisConnectionError
from fidesops.core.config import config
from fidesops.db.database import init_db
from fidesops.schemas.analytics import Event, ExtraData
from fidesops.tasks.scheduled.scheduler import scheduler
from fidesops.tasks.scheduled.tasks import initiate_scheduled_request_intake
from fidesops.util.cache import get_cache
from fidesops.util.logger import get_fides_log_record_factory

logging.basicConfig(level=config.security.LOG_LEVEL)
Expand Down Expand Up @@ -142,7 +144,19 @@ def start_webserver() -> None:

if config.database.ENABLED:
logger.info("Running any pending DB migrations...")
init_db(config.database.SQLALCHEMY_DATABASE_URI)
try:
init_db(config.database.SQLALCHEMY_DATABASE_URI)
except Exception as error: # pylint: disable=broad-except
logger.error(f"Connection to database failed: {error}")
return

if config.redis.ENABLED:
logger.info("Running Redis connection test...")
try:
get_cache()
except (RedisConnectionError, ResponseError) as e:
logger.error(f"Connection to cache failed: {e}")
return

scheduler.start()

Expand Down

0 comments on commit 106f29d

Please sign in to comment.