9 parser
= argparse
.ArgumentParser()
10 parser
.add_argument("--delay", action
="store", type=float, default
=3, help="Initial delay before retry, in seconds")
11 parser
.add_argument("--count", action
="store", type=int, default
=5, help="Total number of tries")
12 parser
.add_argument("--factor", action
="store", type=float, default
=2, help="Exponential backoff factor")
13 parser
.add_argument("cmd", nargs
=argparse
.REMAINDER
)
25 args
= get_args().parse_args()
28 print("No command supplied")
33 for n
in iter_range(args
.count
):
35 print("Running %s [try %d/%d]" % (" ".join(args
.cmd
), (n
+1), args
.count
))
36 subprocess
.check_call(args
.cmd
)
37 except subprocess
.CalledProcessError
as e
:
38 retcode
= e
.returncode
40 print("Command succeeded")
45 wait_time
= (n
+1) * args
.delay
47 wait_time
= args
.factor
**n
* args
.delay
48 if n
< args
.count
- 1:
49 print("Command failed, waiting %s seconds to retry" % wait_time
)
52 print("Command failed, out of retries")
57 if __name__
== "__main__":