employeeing try and retry logic to worker to prevent crashes

This commit is contained in:
Bryan Van Deusen
2025-08-05 10:21:49 -04:00
parent 68cff4e028
commit b646c130d9
+60 -30
View File
@@ -12,6 +12,9 @@ CHECK_INTERVAL = 10 # seconds
app = create_app() app = create_app()
from sqlalchemy.exc import OperationalError
import time
def monitor_and_import(): def monitor_and_import():
with app.app_context(): with app.app_context():
print("[Image Importer] Monitoring for trigger flag...") print("[Image Importer] Monitoring for trigger flag...")
@@ -19,43 +22,70 @@ 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...")
try: max_retries = 3
result = import_images_task(SOURCE_DIR, DEST_DIR) attempt = 0
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}")
# Thumbnail generation Check 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 regeneration
if os.path.exists(THUMBNAIL_FLAG): if os.path.exists(THUMBNAIL_FLAG):
print("[Image Importer] Thumbnail regeneration triggered.") print("[Image Importer] Thumbnail regeneration triggered.")
try: max_retries = 3
from app.models import ImageRecord attempt = 0
from app.utils.image_importer import generate_thumbnail
for img in ImageRecord.query.all(): while attempt < max_retries:
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:
try: try:
os.remove(THUMBNAIL_FLAG) from app.models import ImageRecord
print("[Image Importer] Thumbnail flag cleared.") 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: 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)
if __name__ == '__main__': if __name__ == '__main__':
monitor_and_import() monitor_and_import()