diff --git a/process_file/main.py b/process_file/main.py index abe6958..3d50cdf 100644 --- a/process_file/main.py +++ b/process_file/main.py @@ -9,13 +9,24 @@ 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") @@ -23,12 +34,15 @@ def run(event, context): 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))