From cf16a893396ad98a839b82913d2565aa0bd5887f Mon Sep 17 00:00:00 2001 From: Greg Finley Date: Sun, 28 Apr 2024 15:26:29 -0700 Subject: [PATCH] Don't queue twice --- queue_files/main.py | 11 +++++++---- requeue_failed_files/main.py | 2 +- temp.py | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 temp.py diff --git a/queue_files/main.py b/queue_files/main.py index 1ea7172..3f55750 100644 --- a/queue_files/main.py +++ b/queue_files/main.py @@ -48,7 +48,7 @@ def run(event, context): gcs_bucket = gcs_client.get_bucket("greg-finley-dropbox-backup") futures = [] - for i, entry in enumerate(dropbox_result.entries): + for entry in dropbox_result.entries: print(entry) clean_name = entry.path_display.removeprefix("/") if isinstance(entry, dropbox.files.FileMetadata): @@ -56,12 +56,15 @@ def run(event, context): query = """ INSERT INTO dropbox (desktop_path, filename, status) VALUES (%s, SPLIT_PART(%s, '/', -1), 'pending') - ON CONFLICT (desktop_path) DO NOTHING """ with conn.cursor() as cursor: - cursor.execute(query, (clean_name, clean_name)) - print(f"Queued {clean_name}") + try: + cursor.execute(query, (clean_name, clean_name)) + except psycopg.errors.UniqueViolation as err: + print(f"Already queued {clean_name}") + continue + print(f"Queued {clean_name}") future = publisher.publish(TOPIC_NAME, clean_name.encode("utf-8")) futures.append(future) elif isinstance(entry, dropbox.files.DeletedMetadata): diff --git a/requeue_failed_files/main.py b/requeue_failed_files/main.py index 7827679..f06db8e 100644 --- a/requeue_failed_files/main.py +++ b/requeue_failed_files/main.py @@ -29,7 +29,7 @@ def run(request): publisher = pubsub_v1.PublisherClient() futures = [] - for i, path in enumerate(desktop_paths): + for path in desktop_paths: print(path) future = publisher.publish(TOPIC_NAME, path.encode("utf-8")) futures.append(future) diff --git a/temp.py b/temp.py new file mode 100644 index 0000000..d2163f3 --- /dev/null +++ b/temp.py @@ -0,0 +1,19 @@ +import os + +import psycopg + +conn = psycopg.connect(os.environ["NEON_DATABASE_URL"]) +conn.autocommit = True + +query = """ + INSERT INTO dropbox (desktop_path, filename, status) + VALUES (%s, SPLIT_PART(%s, '/', -1), 'pending') + """ + +clean_name = "Camera Uploads/2024-04-14 17.13.46.mov" +with conn.cursor() as cursor: + try: + cursor.execute(query, (clean_name, clean_name)) + print(f"Queued {clean_name}") + except psycopg.errors.UniqueViolation as err: + print("Already queued")