Skip to content

Commit

Permalink
better redis connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Golfari committed Mar 3, 2024
1 parent e75276b commit 28b2855
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
33 changes: 24 additions & 9 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,27 @@ SECURITY_PASSWORD_SALT=my_precious_two
SEND_FILE_MAX_AGE_DEFAULT=31556926 # In production, set to a high number, like 31556926
PYTHONUNBUFFERED=1 # Force the stdout and stderr streams to be unbuffered.

#database
# database
# MYSQLHOST: if using a docker container becomes the container name, if empty default = db)
# MYSQLPORT: if empty default = 3306
# DATABASE: if empty default = airscore
# MYSQLUSER: if empty default = root
# MYSQLPASSWORD: if empty default = airscore
MYSQLHOST=
MYSQLPORT=
DATABASE=
MYSQLUSER=
MYSQLPASSWORD=
DATABASE_URL=

# redis
# REDIS_HOST: if using a docker container becomes the container name, if empty default = redis)
# REDIS_PORT: if empty default = 6379
# REDIS_SSL: if empty default = False
# REDIS_SSL_CERT_REQS: if empty default = None
REDIS_HOST=
REDIS_PORT=
REDIS_SSL=
REDIS_SSL_CERT_REQS=

#mail
MAIL_SERVER=smtp.example.com
Expand All @@ -30,13 +45,13 @@ XCONTEST_PASS=xcpassword
TELEGRAM_API=123456789
TELEGRAM_CHANNEL=987654321

# it is unlikely that any of the below settings need to be changed unless you are using another web server etc.
#docker containers
REDIS_CONTAINER = redis
WEB_SERVER_CONTAINER = nginx
FLASK_CONTAINER = airscore_prod
FLASK_PORT = 5000
RQ_QUEUE = airscore-jobs
# it is unlikely that any of the below settings need to be changed unless you are using external services etc.

# docker containers
WEB_SERVER_CONTAINER=nginx
FLASK_CONTAINER=airscore_prod
FLASK_PORT=5000
RQ_QUEUE=airscore-jobs

#app
FLASK_APP=autoapp.py
Expand Down
8 changes: 6 additions & 2 deletions airscore/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ def create_app(config_object="airscore.settings"):
app = Flask(__name__.split(".")[0]) # , debug=True)
app.config.from_object(config_object)
app.config.update(SESSION_COOKIE_SAMESITE='Lax')
# app.config["REDIS_URL"] = app.config["REDIS_URL"]
app.redis = Redis(host=app.config["REDIS_CONTAINER"], port=6379)
app.redis = Redis(
host=app.config["REDIS_HOST"],
port=app.config["REDIS_PORT"],
ssl=app.config["REDIS_SSL"],
ssl_cert_reqs=app.config["REDIS_SSL_CERT_REQS"]
)
app.task_queue = rq.Queue(app.config["RQ_QUEUE"], connection=app.redis)
register_extensions(app)
register_blueprints(app)
Expand Down
5 changes: 4 additions & 1 deletion airscore/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
DEBUG_TB_INTERCEPT_REDIRECTS = False
CACHE_TYPE = "redis" # Can be "memcached", "redis", etc.
SQLALCHEMY_TRACK_MODIFICATIONS = False
REDIS_HOST = env.str("REDIS_HOST", default='redis')
REDIS_URL = env.str("REDIS_URL") or 'redis://'
REDIS_CONTAINER = env.str("REDIS_CONTAINER")
REDIS_PORT = env.int("REDIS_PORT", default=6379)
REDIS_SSL = env.bool("REDIS_SSL", default=False)
REDIS_SSL_CERT_REQS = env.str("REDIS_SSL_CERT_REQS") or None
WEB_SERVER_CONTAINER = env.str("WEB_SERVER_CONTAINER")
FLASK_CONTAINER = env.str("FLASK_CONTAINER")
FLASK_PORT = env.str("FLASK_PORT")
Expand Down
9 changes: 6 additions & 3 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ services:
- "2992:2992"
environment:
PYTHONUNBUFFERED: ${PYTHONUNBUFFERED}
REDIS_URL: redis://${REDIS_CONTAINER}:6379
REDIS_CONTAINER: ${REDIS_CONTAINER}
WEB_SERVER_CONTAINER: ${WEB_SERVER_CONTAINER}
REDIS_HOST: ${REDIS_HOST:-redis}
REDIS_URL: redis://${REDIS_HOST:-redis}
REDIS_PORT: ${REDIS_PORT:-6379}
REDIS_SSL: ${REDIS_SSL:-0}
REDIS_SSL_CERT_REQS: ${REDIS_SSL_CERT_REQS}
FLASK_CONTAINER: flask_dev
FLASK_PORT: ${FLASK_PORT}
FLASK_ENV: development
Expand All @@ -51,7 +54,7 @@ services:

redis:
image: redis
container_name: ${REDIS_CONTAINER}
container_name: ${REDIS_HOST:-redis}

volumes:
node-modules:
Expand Down

0 comments on commit 28b2855

Please sign in to comment.