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
+36 -6
View File
@@ -12,6 +12,9 @@ CHECK_INTERVAL = 10 # seconds
app = create_app()
from sqlalchemy.exc import OperationalError
import time
def monitor_and_import():
with app.app_context():
print("[Image Importer] Monitoring for trigger flag...")
@@ -19,11 +22,26 @@ def monitor_and_import():
while True:
if os.path.exists(TRIGGER_FLAG):
print("[Image Importer] Trigger flag found. Starting import...")
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] Error during import: {e}")
print(f"[Image Importer] Unexpected error during import: {e}")
break
finally:
try:
os.remove(TRIGGER_FLAG)
@@ -31,9 +49,13 @@ def monitor_and_import():
except Exception as e:
print(f"[Image Importer] Failed to remove trigger flag: {e}")
# Thumbnail generation Check
# Thumbnail regeneration
if os.path.exists(THUMBNAIL_FLAG):
print("[Image Importer] Thumbnail regeneration triggered.")
max_retries = 3
attempt = 0
while attempt < max_retries:
try:
from app.models import ImageRecord
from app.utils.image_importer import generate_thumbnail
@@ -44,9 +66,19 @@ def monitor_and_import():
print(f"[Thumbnail] Regenerated for {img.filename}")
except Exception as 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:
print(f"[Image Importer] Error during thumbnail regeneration: {e}")
print(f"[Image Importer] Unexpected error during thumbnail regen: {e}")
break
finally:
try:
os.remove(THUMBNAIL_FLAG)
@@ -55,7 +87,5 @@ def monitor_and_import():
print(f"[Image Importer] Failed to remove thumbnail flag: {e}")
time.sleep(CHECK_INTERVAL)
if __name__ == '__main__':
monitor_and_import()