Skip to content

Commit

Permalink
Add pre_ping to sqlalchemy create_engine option (#400)
Browse files Browse the repository at this point in the history
Basically allows reconnection for lost DB connections.
Configurable with default `False`.

* adding pre_ping to sqlalchemy create_engine optin

* add the engine options as a configurable option

* Addressing review for PR#400

* use default value from os.env

* fixing syntax
  • Loading branch information
epifanio committed Oct 5, 2021
1 parent 1564d90 commit 7562b0f
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ENV LC_ALL="en_US.UTF-8" \
ADMIN_PWD=admin \
ADMIN_EMAIL=admin.istrator@mydomain.com \
SQLALCHEMY_DATABASE_URI='sqlite:////GeoHealthCheck/DB/data.db' \
SQLALCHEMY_ENGINE_OPTION_PRE_PING=False \
SECRET_KEY='d544ccc37dc3ad214c09b1b7faaa64c60351d5c8bb48b342' \
GHC_PROBE_HTTP_TIMEOUT_SECS=30 \
GHC_MINIMAL_RUN_FREQUENCY_MINS=10 \
Expand Down
1 change: 1 addition & 0 deletions GeoHealthCheck/config_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
DEBUG = False
SQLALCHEMY_ECHO = False
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_ENGINE_OPTION_PRE_PING = False
SQLALCHEMY_DATABASE_URI = 'sqlite:///data.db'
# Alternative configuration for PostgreSQL database
# SQLALCHEMY_DATABASE_URI = 'postgresql://user:password@host:port/database'
Expand Down
9 changes: 7 additions & 2 deletions GeoHealthCheck/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,13 @@ def init():
app.config['GHC_SITE_URL'].rstrip('/')

app.secret_key = app.config['SECRET_KEY']

App.db_instance = SQLAlchemy(app)
SQLALCHEMY_ENGINE_OPTIONS = {
'pool_pre_ping': app.config[
'SQLALCHEMY_ENGINE_OPTION_PRE_PING'
]
}
App.db_instance = SQLAlchemy(app,
engine_options=SQLALCHEMY_ENGINE_OPTIONS)
App.babel_instance = Babel(app)

# Plugins (via Docker ENV) must be list, but may have been
Expand Down
3 changes: 2 additions & 1 deletion docker/config_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def str2bool(v):
# makes it easier to override via Docker "environment"
# settings on running GHC Containers.
SQLALCHEMY_DATABASE_URI = os.environ['SQLALCHEMY_DATABASE_URI']

# When True enables 'pre_ping' (optionally reconnect to DB) for the SQLALCHEMY create_engine options
SQLALCHEMY_ENGINE_OPTION_PRE_PING = os.environ['SQLALCHEMY_ENGINE_OPTION_PRE_PING']
# Replace None with 'your secret key string' in quotes
SECRET_KEY = os.environ['SECRET_KEY']

Expand Down
1 change: 1 addition & 0 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ a configuration file in the environment settings that will override settings in
The configuration options are:

- **SQLALCHEMY_DATABASE_URI**: the database configuration. See the SQLAlchemy documentation for more info
- **SQLALCHEMY_ENGINE_OPTION_PRE_PING**: DB Disconnect Handling, emitting a test statement on the SQL connection at the start of each connection pool checkout (default: ``False``)
- **SECRET_KEY**: secret key to set when enabling authentication. Use the output of ``paver create_secret_key`` to set this value
- **GHC_RETENTION_DAYS**: the number of days to keep Run history
- **GHC_PROBE_HTTP_TIMEOUT_SECS**: stop waiting for the first byte of a Probe response after the given number of seconds
Expand Down

0 comments on commit 7562b0f

Please sign in to comment.