61 lines
2.3 KiB
Python
61 lines
2.3 KiB
Python
# image_import_worker.py
|
|
import time
|
|
import os
|
|
from app import create_app
|
|
from app.utils.image_importer import import_images_task
|
|
|
|
THUMBNAIL_FLAG = "/import/thumbnail.flag"
|
|
TRIGGER_FLAG = "/import/trigger.flag"
|
|
SOURCE_DIR = "/import"
|
|
DEST_DIR = "/images"
|
|
CHECK_INTERVAL = 10 # seconds
|
|
|
|
app = create_app()
|
|
|
|
def monitor_and_import():
|
|
with app.app_context():
|
|
print("[Image Importer] Monitoring for trigger flag...")
|
|
|
|
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}")
|
|
|
|
# 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
|
|
|
|
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}")
|
|
except Exception as e:
|
|
print(f"[Image Importer] Error during thumbnail regeneration: {e}")
|
|
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__':
|
|
monitor_and_import()
|