Skip to content

Commit

Permalink
Set state to failed if so
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-finley committed Aug 29, 2024
1 parent a2964f3 commit 015dd2b
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions process_file/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,40 @@
gcs_bucket = gcs_client.get_bucket("greg-finley-dropbox-backup")
secret_client = secretmanager.SecretManagerServiceClient()

conn = psycopg.connect(os.environ["NEON_DATABASE_URL"])
conn.autocommit = True


def run(event, context):
filename = base64.b64decode(event["data"]).decode("utf-8")
try:
main(filename)
except Exception as e:
update_status(filename, "failed")
raise e


def main(filename):
dropbox_access_token = secret_client.access_secret_version(
name="projects/greg-finley/secrets/DROPBOX_ACCESS_TOKEN/versions/latest"
).payload.data.decode("utf-8")
dbx = dropbox.Dropbox(dropbox_access_token)
filename = base64.b64decode(event["data"]).decode("utf-8")
print(f"Processing {filename}...")
dropbox_file = dbx.files_download("/" + filename)
content_type = dropbox_file[1].headers.get("Content-Type")
gcs_file = gcs_bucket.blob(filename)
gcs_file.upload_from_string(
dropbox_file[1].content, content_type=content_type, timeout=400
)
update_status(filename, "done")
print(f"Uploaded {filename} - {content_type}")


def update_status(filename, status):
query = """
INSERT INTO dropbox (desktop_path, filename, status)
VALUES (%s, SPLIT_PART(%s, '/', -1), 'done')
ON CONFLICT (desktop_path) DO UPDATE SET status = 'done'
VALUES (%s, SPLIT_PART(%s, '/', -1), %s)
ON CONFLICT (desktop_path) DO UPDATE SET status = %s
"""
with psycopg.connect(os.environ["NEON_DATABASE_URL"]) as conn:
with conn.cursor() as cursor:
cursor.execute(query, (filename, filename))
print(f"Uploaded {filename} - {content_type}")
with conn.cursor() as cursor:
cursor.execute(query, (filename, filename, status, status))

0 comments on commit 015dd2b

Please sign in to comment.