Skip to content

Commit

Permalink
Merge pull request #21 from packethost/add-retry-loop
Browse files Browse the repository at this point in the history
cli: add top-level retry loop.
  • Loading branch information
zevweiss authored May 20, 2020
2 parents 6356d40 + 257d1fc commit 3da6ef9
Showing 1 changed file with 39 additions and 1 deletion.
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

0 comments on commit 3da6ef9

Please sign in to comment.