"""An offline retry demonstration. It makes no network requests."""
import argparse
import time


class TemporaryServiceError(Exception):
    pass


def run_demo(failures, wait=time.sleep):
    for attempt in range(1, 4):
        print(f"Attempt {attempt} of 3")
        try:
            if attempt <= failures:
                raise TemporaryServiceError("Simulated temporary read failure")
            print("Read succeeded. No external action was performed.")
            return True
        except TemporaryServiceError:
            if attempt == 3:
                print("Stopped after 3 attempts. Review the failure before running again.")
                return False
            delay = 0.1 * (2 ** (attempt - 1))
            print(f"Waiting {delay:.1f} seconds before retrying the simulated read.")
            wait(delay)
    return False


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--failures", type=int, choices=range(4), default=2)
    args = parser.parse_args()
    raise SystemExit(0 if run_demo(args.failures) else 1)
