My code works but I want the podman command runs uniquely when the file_queue is empty. But it start before end of data copied into /ARRIVAGE_TEST/ and runs indefinitively. The other script test_gilles_conf.py copy/past files to another directory after there are added to ARRIVAGE_TEST.
import os
import time
from pathlib import Path
from inotify.adapters import Inotify
import subprocess
from collections import deque
from threading import Lock, Thread
WATCH_DIR = Path('/***/ARRIVAGE_TEST')
SCRIPT_PATH = Path('/***/test_gilles_conf.py')
file_queue = deque()
queue_lock = Lock()
last_event_time = {"time": time.time()}
photoprism_process = None
def handle_event(event):
(header, type_names, watch_path, name) = event
if 'IN_CLOSE_WRITE' in type_names:
filepath = Path(watch_path) / name
with queue_lock:
file_queue.append(str(filepath))
return time.time()
return None
def process_files():
global photoprism_process
podman_command_running = False # Flag to track if the Podman command is already running
while True:
with queue_lock:
if file_queue and time.time() - last_event_time["time"] > 5:
if photoprism_process is None or photoprism_process.poll() is not None:
filepath = file_queue.popleft()
try:
env = os.environ.copy()
env["LAST_FILE_PATH"] = filepath
subprocess.run(["python3", str(SCRIPT_PATH)], env=env)
except Exception as e:
print(f"Error running script: {e}")
else:
if not file_queue and time.time() - last_event_time["time"] > 5 and not podman_command_running:
podman_command_running = True # Set the flag to indicate that the Podman command is running
try:
photoprism_process = subprocess.run(["sudo", "podman", "exec", "photoprism-app", "photoprism", "index"])
except Exception as e:
print(f"Error running Podman command: {e}")
finally:
podman_command_running = False # Reset the flag after the command completes
time.sleep(1) # Wait for 1 second before checking again
def watch_directory(directory):
i = Inotify()
i.add_watch(str(directory))
print("Starting file watch...")
processing_thread = Thread(target=process_files, daemon=True)
processing_thread.start()
try:
for event in i.event_gen():
if event is not None:
event_time = handle_event(event)
if event_time is not None:
last_event_time["time"] = event_time
finally:
i.remove_watch(str(directory))
processing_thread.join() # wait for the processing thread to finish
if __name__ == "__main__":
watch_directory(WATCH_DIR)
It's a script to watch and execute another script after receive files into ARRIVAGE_TEST. I want the podman command start after the treatment of all files but it don't work likes i expect it. Can ou help me ?
I try this :
else:
if not file_queue and time.time() - last_event_time["time"] > 5 and not podman_command_running:
podman_command_running = True # Set the flag to indicate that the Podman command is running
try:
photoprism_process = subprocess.run(["sudo", "podman", "exec", "photoprism-app", "photoprism", "index"])
except Exception as e:
print(f"Error running Podman command: {e}")
finally:
podman_command_running = False # Reset the flag after the command completes
time.sleep(1) # Wait for 1 second before checking again
but when podman command finish it always restart it.