attempts to eliminate database connection errors
This commit is contained in:
+4
-2
@@ -5,6 +5,7 @@ from flask_sqlalchemy import SQLAlchemy
|
|||||||
from flask_migrate import Migrate
|
from flask_migrate import Migrate
|
||||||
from flask_login import LoginManager
|
from flask_login import LoginManager
|
||||||
|
|
||||||
|
|
||||||
db = SQLAlchemy()
|
db = SQLAlchemy()
|
||||||
migrate = Migrate()
|
migrate = Migrate()
|
||||||
login_manager = LoginManager()
|
login_manager = LoginManager()
|
||||||
@@ -12,8 +13,9 @@ login_manager = LoginManager()
|
|||||||
def create_app(config_class='config.Config'):
|
def create_app(config_class='config.Config'):
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config.from_object(config_class)
|
app.config.from_object(config_class)
|
||||||
|
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
|
||||||
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = config_class.SQLALCHEMY_ENGINE_OPTIONS
|
"pool_pre_ping": True
|
||||||
|
}
|
||||||
|
|
||||||
db.init_app(app)
|
db.init_app(app)
|
||||||
migrate.init_app(app, db)
|
migrate.init_app(app, db)
|
||||||
|
|||||||
@@ -28,6 +28,3 @@ class Config:
|
|||||||
|
|
||||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||||
|
|
||||||
SQLALCHEMY_ENGINE_OPTIONS = {
|
|
||||||
"pool_pre_ping": True
|
|
||||||
}
|
|
||||||
|
|||||||
+33
-4
@@ -1,6 +1,7 @@
|
|||||||
# image_import_worker.py
|
# image_import_worker.py
|
||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
|
from sqlalchemy.exc import OperationalError
|
||||||
from app import create_app
|
from app import create_app
|
||||||
from app.utils.image_importer import import_images_task
|
from app.utils.image_importer import import_images_task
|
||||||
|
|
||||||
@@ -19,11 +20,26 @@ def monitor_and_import():
|
|||||||
while True:
|
while True:
|
||||||
if os.path.exists(TRIGGER_FLAG):
|
if os.path.exists(TRIGGER_FLAG):
|
||||||
print("[Image Importer] Trigger flag found. Starting import...")
|
print("[Image Importer] Trigger flag found. Starting import...")
|
||||||
|
max_retries = 3
|
||||||
|
attempt = 0
|
||||||
|
|
||||||
|
while attempt < max_retries:
|
||||||
try:
|
try:
|
||||||
result = import_images_task(SOURCE_DIR, DEST_DIR)
|
result = import_images_task(SOURCE_DIR, DEST_DIR)
|
||||||
print(f"[Image Importer] {result}")
|
print(f"[Image Importer] {result}")
|
||||||
|
break # Exit retry loop if successful
|
||||||
|
except OperationalError as e:
|
||||||
|
attempt += 1
|
||||||
|
print(f"[Image Importer] Database error during import (attempt {attempt}): {e}")
|
||||||
|
if attempt >= max_retries:
|
||||||
|
print("[Image Importer] Max retries reached. Import failed.")
|
||||||
|
else:
|
||||||
|
sleep_time = 2 ** attempt
|
||||||
|
print(f"[Image Importer] Retrying in {sleep_time} seconds...")
|
||||||
|
time.sleep(sleep_time)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[Image Importer] Error during import: {e}")
|
print(f"[Image Importer] Unexpected error during import: {e}")
|
||||||
|
break
|
||||||
finally:
|
finally:
|
||||||
try:
|
try:
|
||||||
os.remove(TRIGGER_FLAG)
|
os.remove(TRIGGER_FLAG)
|
||||||
@@ -31,9 +47,12 @@ def monitor_and_import():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[Image Importer] Failed to remove trigger flag: {e}")
|
print(f"[Image Importer] Failed to remove trigger flag: {e}")
|
||||||
|
|
||||||
# Thumbnail generation Check
|
|
||||||
if os.path.exists(THUMBNAIL_FLAG):
|
if os.path.exists(THUMBNAIL_FLAG):
|
||||||
print("[Image Importer] Thumbnail regeneration triggered.")
|
print("[Image Importer] Thumbnail regeneration triggered.")
|
||||||
|
max_retries = 3
|
||||||
|
attempt = 0
|
||||||
|
|
||||||
|
while attempt < max_retries:
|
||||||
try:
|
try:
|
||||||
from app.models import ImageRecord
|
from app.models import ImageRecord
|
||||||
from app.utils.image_importer import generate_thumbnail
|
from app.utils.image_importer import generate_thumbnail
|
||||||
@@ -44,9 +63,19 @@ def monitor_and_import():
|
|||||||
print(f"[Thumbnail] Regenerated for {img.filename}")
|
print(f"[Thumbnail] Regenerated for {img.filename}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[Thumbnail] Failed for {img.filename}: {e}")
|
print(f"[Thumbnail] Failed for {img.filename}: {e}")
|
||||||
time.sleep(1)
|
break # Success
|
||||||
|
except OperationalError as e:
|
||||||
|
attempt += 1
|
||||||
|
print(f"[Image Importer] Database error during thumbnail regen (attempt {attempt}): {e}")
|
||||||
|
if attempt >= max_retries:
|
||||||
|
print("[Image Importer] Max retries reached. Thumbnail regeneration failed.")
|
||||||
|
else:
|
||||||
|
sleep_time = 2 ** attempt
|
||||||
|
print(f"[Image Importer] Retrying in {sleep_time} seconds...")
|
||||||
|
time.sleep(sleep_time)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[Image Importer] Error during thumbnail regeneration: {e}")
|
print(f"[Image Importer] Unexpected error during thumbnail regen: {e}")
|
||||||
|
break
|
||||||
finally:
|
finally:
|
||||||
try:
|
try:
|
||||||
os.remove(THUMBNAIL_FLAG)
|
os.remove(THUMBNAIL_FLAG)
|
||||||
|
|||||||
Reference in New Issue
Block a user