Commits

Peter Ward committed 6347780 Merge

merged

Comments (0)

Files changed (2)

     User root
     ForwardX11 no
 
+Host scarab
+    User root
+
 Host werty
     User root
 
+#!/usr/bin/env python
+
+import argparse
+from subprocess import call
+import time
+
+parser = argparse.ArgumentParser()
+parser.add_argument(
+    '-m', '--min-delay', type=float,
+    help='Minimum delay between running the command, in seconds.',
+    default=0.0,
+)
+parser.add_argument(
+    '-M', '--max-delay', type=float,
+    help='Maximum delay between running the command, in seconds.',
+    default=float('inf'),
+)
+parser.add_argument(
+    '-b', '--backoff-factor', type=float,
+    help='When the command returns a non-zero exit code, '
+         'multiply the delay by this amount.',
+    required=True,
+)
+parser.add_argument(
+    '-B', '--backon-factor', type=float,
+    help='When the command returns an exit code of zero, '
+         'divide the delay by this amount.',
+    required=True,
+)
+parser.add_argument('-v', '--verbose', action='store_true')
+parser.add_argument('command', nargs='+')
+
+args = parser.parse_args()
+
+def constrain(lower, value, upper):
+    return min(max(lower, value), upper)
+
+current_delay = args.min_delay
+while True:
+    rv = call(args.command)
+    if rv == 0:
+        # command was successful, run it faster
+        if args.verbose:
+            print 'returned', rv, 'backing on',
+        current_delay /= args.backon_factor
+    else:
+        # command was unsuccessful, back off
+        if args.verbose:
+            print 'returned', rv, 'backing off',
+        current_delay *= args.backoff_factor
+    current_delay = constrain(args.min_delay, current_delay, args.max_delay)
+    if args.verbose:
+        print 'to', current_delay
+    time.sleep(current_delay)