Fail script on all server errors
Remove local exception handling for IMAP operations to ensure script exits on any server failure (e.g., auth, connection, search errors). This allows systemd OnFailure to notify on fixable issues. Changes: - Remove try-except in check_duplicate, sync_folder, sync_all_folders - Let IMAP exceptions propagate to main() for exit(1)
This commit is contained in:
parent
69ec294a56
commit
05f2e6c870
1 changed files with 28 additions and 42 deletions
|
|
@ -104,13 +104,9 @@ class DestImap(ImapClient):
|
||||||
|
|
||||||
def check_duplicate(self, msg_id: str):
|
def check_duplicate(self, msg_id: str):
|
||||||
"""Check if Message-ID exists in current folder."""
|
"""Check if Message-ID exists in current folder."""
|
||||||
try:
|
|
||||||
results = self._search(f'HEADER Message-ID "{msg_id}"')
|
results = self._search(f'HEADER Message-ID "{msg_id}"')
|
||||||
logging.debug(f"Check duplicate for {msg_id}: found {len(results)} matches")
|
logging.debug(f"Check duplicate for {msg_id}: found {len(results)} matches")
|
||||||
return len(results) > 0
|
return len(results) > 0
|
||||||
except Exception as e:
|
|
||||||
logging.error(f"Error checking duplicate for {msg_id}: {e}")
|
|
||||||
return False
|
|
||||||
|
|
||||||
def _create_folder(self, folder: str):
|
def _create_folder(self, folder: str):
|
||||||
"""Private: Create a folder."""
|
"""Private: Create a folder."""
|
||||||
|
|
@ -179,15 +175,12 @@ class EmailForwarder:
|
||||||
total_forwarded = 0
|
total_forwarded = 0
|
||||||
|
|
||||||
for folder in folders:
|
for folder in folders:
|
||||||
try:
|
|
||||||
uids = self.source.get_new_emails(
|
uids = self.source.get_new_emails(
|
||||||
folder, self.last_run.strftime("%d-%b-%Y")
|
folder, self.last_run.strftime("%d-%b-%Y")
|
||||||
)
|
)
|
||||||
logging.info(f"Found {len(uids)} emails in {folder}")
|
logging.info(f"Found {len(uids)} emails in {folder}")
|
||||||
forwarded = self.sync_folder(folder, uids)
|
forwarded = self.sync_folder(folder, uids)
|
||||||
total_forwarded += forwarded
|
total_forwarded += forwarded
|
||||||
except Exception as e:
|
|
||||||
logging.error(f"Error syncing {folder}: {e}")
|
|
||||||
|
|
||||||
self.source._disconnect()
|
self.source._disconnect()
|
||||||
self.dest._disconnect()
|
self.dest._disconnect()
|
||||||
|
|
@ -198,7 +191,6 @@ class EmailForwarder:
|
||||||
forwarded = 0
|
forwarded = 0
|
||||||
for uid in uids:
|
for uid in uids:
|
||||||
uid_str = uid.decode()
|
uid_str = uid.decode()
|
||||||
try:
|
|
||||||
raw_msg = self.source._fetch(uid_str)
|
raw_msg = self.source._fetch(uid_str)
|
||||||
|
|
||||||
msg = message_from_bytes(raw_msg)
|
msg = message_from_bytes(raw_msg)
|
||||||
|
|
@ -210,10 +202,6 @@ class EmailForwarder:
|
||||||
self.dest.ensure_folder_exists(folder)
|
self.dest.ensure_folder_exists(folder)
|
||||||
if self.dest.check_duplicate(msg_id):
|
if self.dest.check_duplicate(msg_id):
|
||||||
continue
|
continue
|
||||||
if msg_id in self.processed_ids:
|
|
||||||
continue
|
|
||||||
if self.dest.check_duplicate(msg_id):
|
|
||||||
continue
|
|
||||||
|
|
||||||
if not self.dry_run:
|
if not self.dry_run:
|
||||||
self.dest.append_email(folder, raw_msg)
|
self.dest.append_email(folder, raw_msg)
|
||||||
|
|
@ -224,8 +212,6 @@ class EmailForwarder:
|
||||||
logging.info(f"Forwarded {msg_id} from {folder}")
|
logging.info(f"Forwarded {msg_id} from {folder}")
|
||||||
else:
|
else:
|
||||||
logging.info(f"Dry-run: Would forward {msg_id} from {folder}")
|
logging.info(f"Dry-run: Would forward {msg_id} from {folder}")
|
||||||
except Exception as e:
|
|
||||||
logging.error(f"Error processing UID {uid_str}: {e}")
|
|
||||||
return forwarded
|
return forwarded
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue