Skip to content

Commit

Permalink
Don't queue twice
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-finley committed Apr 28, 2024
1 parent f778def commit cf16a89
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
11 changes: 7 additions & 4 deletions queue_files/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,23 @@ 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):
print(f"Queueing {clean_name}")
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):
Expand Down
2 changes: 1 addition & 1 deletion requeue_failed_files/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions temp.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit cf16a89

Please sign in to comment.