From 325f3311a5f869d1361f5579a3f3d6ee7bb3cd0c Mon Sep 17 00:00:00 2001 From: bwaide Date: Tue, 11 Feb 2025 11:55:22 +0100 Subject: [PATCH] Bug fix in reconnection handling --- .gitignore | 1 + migrate.py | 21 +++++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index ea1eb0b..63d0e54 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ __pycache__/ migration.log statistics.json venv/ +migration_checkpoint.json diff --git a/migrate.py b/migrate.py index 4e44f33..8a6e623 100644 --- a/migrate.py +++ b/migrate.py @@ -438,7 +438,7 @@ def migrate_all(source_conn, dest_conn, simulation=False): # --- Force a reconnection every 500 emails --- if current_email > 0 and current_email % RECONNECT_INTERVAL == 0: logger.debug("Forcing periodic reconnection of IMAP connections...") - reconnect(source_folder) + source_conn, dest_conn = reconnect(source_folder) # --------------------------------------------------------- # **Reduce IMAP request rate** (prevent timeouts/rate limiting) @@ -448,7 +448,7 @@ def migrate_all(source_conn, dest_conn, simulation=False): except imaplib.IMAP4.abort as e: logger.error(f"IMAP connection lost during migration (mail #{num}). Error: {e}") - reconnect(source_folder) + source_conn, dest_conn = reconnect(source_folder) progress_bar.close() @@ -473,11 +473,20 @@ def migrate_all(source_conn, dest_conn, simulation=False): return total_emails, total_size_mb, skipped_emails def reconnect(source_folder): - reconnect_imap(DEST_IMAP_SERVER, DEST_EMAIL, DEST_PASSWORD) - source_conn = reconnect_imap(SOURCE_IMAP_SERVER, SOURCE_EMAIL, SOURCE_PASSWORD) + """ + Reconnects both the source and destination IMAP connections and re-selects + the specified source folder. + + Returns: + new_source_conn, new_dest_conn: The new connection objects. + """ + new_dest_conn = reconnect_imap(DEST_IMAP_SERVER, DEST_EMAIL, DEST_PASSWORD) + new_source_conn = reconnect_imap(SOURCE_IMAP_SERVER, SOURCE_EMAIL, SOURCE_PASSWORD) + new_source_conn.select(source_folder, readonly=True) - # Re-select the source folder after reconnecting. - source_conn.select(source_folder, readonly=True) + time.sleep(0.5) # wait a few milliseconds + + return new_source_conn, new_dest_conn # ------------------------------- # Now update your get_folder_mapping_info to use the consolidated function.