39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
# image_import_loop.py
|
|
import time
|
|
import os
|
|
from app import create_app
|
|
from app.utils.image_importer import import_images_task
|
|
|
|
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}")
|
|
else:
|
|
print("[Image Importer] No trigger flag. Sleeping...")
|
|
|
|
time.sleep(CHECK_INTERVAL)
|
|
|
|
if __name__ == '__main__':
|
|
monitor_and_import()
|