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_login import LoginManager
|
||||
|
||||
|
||||
db = SQLAlchemy()
|
||||
migrate = Migrate()
|
||||
login_manager = LoginManager()
|
||||
@@ -12,8 +13,9 @@ login_manager = LoginManager()
|
||||
def create_app(config_class='config.Config'):
|
||||
app = Flask(__name__)
|
||||
app.config.from_object(config_class)
|
||||
|
||||
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = config_class.SQLALCHEMY_ENGINE_OPTIONS
|
||||
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
|
||||
"pool_pre_ping": True
|
||||
}
|
||||
|
||||
db.init_app(app)
|
||||
migrate.init_app(app, db)
|
||||
|
||||
@@ -28,6 +28,3 @@ class Config:
|
||||
|
||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||
|
||||
SQLALCHEMY_ENGINE_OPTIONS = {
|
||||
"pool_pre_ping": True
|
||||
}
|
||||
|
||||
+57
-28
@@ -1,6 +1,7 @@
|
||||
# image_import_worker.py
|
||||
import time
|
||||
import os
|
||||
from sqlalchemy.exc import OperationalError
|
||||
from app import create_app
|
||||
from app.utils.image_importer import import_images_task
|
||||
|
||||
@@ -19,40 +20,68 @@ def monitor_and_import():
|
||||
while True:
|
||||
if os.path.exists(TRIGGER_FLAG):
|
||||
print("[Image Importer] Trigger flag found. Starting import...")
|
||||
try:
|
||||
result = import_images_task(SOURCE_DIR, DEST_DIR)
|
||||
print(f"[Image Importer] {result}")
|
||||
except Exception as e:
|
||||
print(f"[Image Importer] Error during import: {e}")
|
||||
finally:
|
||||
try:
|
||||
os.remove(TRIGGER_FLAG)
|
||||
print("[Image Importer] Trigger flag cleared.")
|
||||
except Exception as e:
|
||||
print(f"[Image Importer] Failed to remove trigger flag: {e}")
|
||||
max_retries = 3
|
||||
attempt = 0
|
||||
|
||||
while attempt < max_retries:
|
||||
try:
|
||||
result = import_images_task(SOURCE_DIR, DEST_DIR)
|
||||
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:
|
||||
print(f"[Image Importer] Unexpected error during import: {e}")
|
||||
break
|
||||
finally:
|
||||
try:
|
||||
os.remove(TRIGGER_FLAG)
|
||||
print("[Image Importer] Trigger flag cleared.")
|
||||
except Exception as e:
|
||||
print(f"[Image Importer] Failed to remove trigger flag: {e}")
|
||||
|
||||
# Thumbnail generation Check
|
||||
if os.path.exists(THUMBNAIL_FLAG):
|
||||
print("[Image Importer] Thumbnail regeneration triggered.")
|
||||
try:
|
||||
from app.models import ImageRecord
|
||||
from app.utils.image_importer import generate_thumbnail
|
||||
max_retries = 3
|
||||
attempt = 0
|
||||
|
||||
for img in ImageRecord.query.all():
|
||||
try:
|
||||
generate_thumbnail(img.filepath, overwrite=True)
|
||||
print(f"[Thumbnail] Regenerated for {img.filename}")
|
||||
except Exception as e:
|
||||
print(f"[Thumbnail] Failed for {img.filename}: {e}")
|
||||
time.sleep(1)
|
||||
except Exception as e:
|
||||
print(f"[Image Importer] Error during thumbnail regeneration: {e}")
|
||||
finally:
|
||||
while attempt < max_retries:
|
||||
try:
|
||||
os.remove(THUMBNAIL_FLAG)
|
||||
print("[Image Importer] Thumbnail flag cleared.")
|
||||
from app.models import ImageRecord
|
||||
from app.utils.image_importer import generate_thumbnail
|
||||
|
||||
for img in ImageRecord.query.all():
|
||||
try:
|
||||
generate_thumbnail(img.filepath, overwrite=True)
|
||||
print(f"[Thumbnail] Regenerated for {img.filename}")
|
||||
except Exception as e:
|
||||
print(f"[Thumbnail] Failed for {img.filename}: {e}")
|
||||
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:
|
||||
print(f"[Image Importer] Failed to remove thumbnail flag: {e}")
|
||||
print(f"[Image Importer] Unexpected error during thumbnail regen: {e}")
|
||||
break
|
||||
finally:
|
||||
try:
|
||||
os.remove(THUMBNAIL_FLAG)
|
||||
print("[Image Importer] Thumbnail flag cleared.")
|
||||
except Exception as e:
|
||||
print(f"[Image Importer] Failed to remove thumbnail flag: {e}")
|
||||
|
||||
|
||||
time.sleep(CHECK_INTERVAL)
|
||||
|
||||
Reference in New Issue
Block a user