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

cli: add top-level retry loop. #21

Merged
merged 1 commit into from
May 20, 2020
Merged
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
40 changes: 39 additions & 1 deletion packetnetworking/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
import time
import json
import click
import logging
Expand Down Expand Up @@ -35,10 +36,18 @@
+ "(otherwise uses ones from /etc/resolv.conf)"
),
)
@click.option("-n", "--max-attempts", default=10, help="retry up to N times on failure")
@click.option("-v", "--verbose", count=True, help="Provide more detailed output")
@click.option("-q", "--quiet", is_flag=True, help="Silences all output")
def cli(
metadata_file, metadata_url, operating_system, rootfs, resolvers, verbose, quiet
metadata_file,
metadata_url,
operating_system,
rootfs,
resolvers,
max_attempts,
verbose,
quiet,
):
level = logging.WARNING
if verbose:
Expand Down Expand Up @@ -68,6 +77,35 @@ def cli(
)
)

attempt = 1
while True:
try:
try_run(
metadata_file,
metadata_url,
operating_system,
rootfs,
resolvers,
verbose,
quiet,
)
break
except Exception as exc:
if attempt == max_attempts:
raise
attempt += 1
delay = 2 ** min(attempt, 7)
log.error(
"Caught unexpected exception ('{}'), retrying in {} seconds...".format(
exc, delay
)
)
time.sleep(delay)


def try_run(
metadata_file, metadata_url, operating_system, rootfs, resolvers, verbose, quiet
):
builder = packetnetworking.Builder()
if metadata_file:
builder.set_metadata(json.load(metadata_file))
Expand Down