From 8c982666d65b5199b9f4b0a1bf81d27aa24073da Mon Sep 17 00:00:00 2001 From: Karl Date: Fri, 18 Jul 2025 08:49:26 +0100 Subject: [PATCH] redis fix --- app.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index be7fd16..16a0f13 100644 --- a/app.py +++ b/app.py @@ -6,6 +6,8 @@ import requests.auth import os import base64 from typing import Dict, Any, Tuple, Union +import sys +import redis from lib.datetime import filter_accounts_next_30_days, filter_accounts_expired from lib.reqs import (get_urls, get_user_accounts, add_user_account, @@ -30,10 +32,22 @@ if os.environ.get("FLASK_ENV") == "production": else: app.config.from_object(DevelopmentConfig) -cache = Cache(app, config={ - "CACHE_TYPE": "redis", - "CACHE_REDIS_URL": app.config.get("REDIS_URL", "redis://localhost:6379/0") -}) +# Check for Redis availability and configure cache +redis_url = app.config.get("REDIS_URL", "redis://localhost:6379/0") +cache_config = {"CACHE_TYPE": "redis", "CACHE_REDIS_URL": redis_url} +try: + # Use a short timeout to prevent hanging + r = redis.from_url(redis_url, socket_connect_timeout=1) + r.ping() +except redis.exceptions.ConnectionError as e: + print( + f"WARNING: Redis connection failed: {e}. Falling back to SimpleCache. " + "This is not recommended for production with multiple workers.", + file=sys.stderr, + ) + cache_config = {"CACHE_TYPE": "SimpleCache"} + +cache = Cache(app, config=cache_config) if app.config.get("OCR_ENABLED") and OCR_AVAILABLE: ocr = PaddleOCR(use_angle_cls=True, lang='en') @@ -44,7 +58,6 @@ app.config["SESSION_COOKIE_SECURE"] = not app.config["DEBUG"] app.config['SESSION_COOKIE_HTTPONLY'] = True app.config['SESSION_COOKIE_SAMESITE'] = 'Lax' app.config['PERMANENT_SESSION_LIFETIME'] = 60 * 60 * 24 * 365 # 1 year -cache.clear() def get_version() -> str: """Retrieves the application version from the VERSION file.