From 0c2e1df9a5e33c7c83fbf44052a85a3d996fb59c Mon Sep 17 00:00:00 2001 From: INODE64 Date: Thu, 22 Aug 2024 10:00:37 +0200 Subject: [PATCH 1/3] Include retry support in connectionError --- virtnbdbackup | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/virtnbdbackup b/virtnbdbackup index f8ca2d05..52ce2d8e 100755 --- a/virtnbdbackup +++ b/virtnbdbackup @@ -20,6 +20,7 @@ import sys import signal import logging import argparse +import time from typing import List from datetime import datetime from functools import partial @@ -48,6 +49,8 @@ from libvirtnbdbackup.virt.exceptions import ( ) from libvirtnbdbackup.output.exceptions import OutputException +MAX_RETRIES = 5 +RETRY_DELAY_SECONDS = 5 def main() -> None: """Handle backup operation and settings.""" @@ -324,15 +327,28 @@ def main() -> None: "Libvirt connection error detected (%s), reconnecting", reasonStrings[reason], ) - try: - virtClient = virt.client(args) - except connectionFailed as e: - logging.error("Unrecoverable connection error: %s", e) - sys.exit(1) - domObj = virtClient.getDomain(args.domain) - if not args.offline: - logging.error("Attempting to stop backup task") - virtClient.stopBackup(domObj) + + for attempt in range(MAX_RETRIES): + try: + virtClient = virt.client(args) + + logging.info("Successfully reconnected on attempt %d.", attempt + 1) + + domObj = virtClient.getDomain(args.domain) + + if not args.offline: + logging.error("Attempting to stop backup task") + virtClient.stopBackup(domObj) + + sys.exit(1) + + except connectionFailed as e: + logging.error("Failed to reconnect: %s. Attempt %d/%d.", e, attempt + 1, MAX_RETRIES) + + logging.info("Waiting %d seconds before retrying...", RETRY_DELAY_SECONDS) + time.sleep(RETRY_DELAY_SECONDS) + + logging.error("Unable to reconnect after %d attempts. Exiting...", MAX_RETRIES) sys.exit(1) try: From 19ddc3a786720b0fc0a74ca8cf636c99f2e57f6f Mon Sep 17 00:00:00 2001 From: INODE64 Date: Thu, 22 Aug 2024 10:07:07 +0200 Subject: [PATCH 2/3] Fix idents --- virtnbdbackup | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/virtnbdbackup b/virtnbdbackup index 52ce2d8e..4a2b574e 100755 --- a/virtnbdbackup +++ b/virtnbdbackup @@ -327,19 +327,18 @@ def main() -> None: "Libvirt connection error detected (%s), reconnecting", reasonStrings[reason], ) - for attempt in range(MAX_RETRIES): try: virtClient = virt.client(args) - + logging.info("Successfully reconnected on attempt %d.", attempt + 1) domObj = virtClient.getDomain(args.domain) - + if not args.offline: logging.error("Attempting to stop backup task") virtClient.stopBackup(domObj) - + sys.exit(1) except connectionFailed as e: From 5ee0540cc314574ed2c732bcf0736da254f01f40 Mon Sep 17 00:00:00 2001 From: INODE64 Date: Thu, 22 Aug 2024 10:46:17 +0200 Subject: [PATCH 3/3] reformatted code --- virtnbdbackup | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/virtnbdbackup b/virtnbdbackup index 4a2b574e..f53cac11 100755 --- a/virtnbdbackup +++ b/virtnbdbackup @@ -52,6 +52,7 @@ from libvirtnbdbackup.output.exceptions import OutputException MAX_RETRIES = 5 RETRY_DELAY_SECONDS = 5 + def main() -> None: """Handle backup operation and settings.""" parser = argparse.ArgumentParser( @@ -342,7 +343,12 @@ def main() -> None: sys.exit(1) except connectionFailed as e: - logging.error("Failed to reconnect: %s. Attempt %d/%d.", e, attempt + 1, MAX_RETRIES) + logging.error( + "Failed to reconnect: %s. Attempt %d/%d.", + e, + attempt + 1, + MAX_RETRIES, + ) logging.info("Waiting %d seconds before retrying...", RETRY_DELAY_SECONDS) time.sleep(RETRY_DELAY_SECONDS)