Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include retry support in connectionError #206

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions virtnbdbackup
Original file line number Diff line number Diff line change
@@ -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,9 @@ 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 +328,32 @@ 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: