| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # | 2 # |
| 3 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """Wrapper that does auto-retry and stats logging for command invocation. | 7 """Wrapper that does auto-retry and stats logging for command invocation. |
| 8 | 8 |
| 9 Various command line tools in use: gsutil, curl have spurious failure. | 9 Various command line tools in use: gsutil, curl have spurious failure. |
| 10 This wrapper will track stats to an AppEngine based service to | 10 This wrapper will track stats to an AppEngine based service to |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 f = urllib.urlopen(options.logurl, params) | 65 f = urllib.urlopen(options.logurl, params) |
| 66 ret = f.read() | 66 ret = f.read() |
| 67 f.close() | 67 f.close() |
| 68 try: | 68 try: |
| 69 return int(ret) != 0 | 69 return int(ret) != 0 |
| 70 except ValueError: | 70 except ValueError: |
| 71 return 0 | 71 return 0 |
| 72 | 72 |
| 73 | 73 |
| 74 def RunWithTimeout(timeout, func, *args, **kwargs): | 74 def RunWithTimeout(timeout, func, *args, **kwargs): |
| 75 result = None | 75 wrapper = { 'result': None } |
| 76 def CallFunc(): | 76 def CallFunc(): |
| 77 result = func(*args, **kwargs) | 77 wrapper['result'] = func(*args, **kwargs) |
| 78 th = threading.Thread(target=CallFunc) | 78 th = threading.Thread(target=CallFunc) |
| 79 th.start() | 79 th.start() |
| 80 th.join(timeout) | 80 th.join(timeout) |
| 81 return result | 81 return wrapper['result'] |
| 82 | 82 |
| 83 | 83 |
| 84 def main(argv): | 84 def main(argv): |
| 85 parser = optparse.OptionParser() | 85 parser = optparse.OptionParser() |
| 86 parser.add_option('-r', '--retries', dest='retries', | 86 parser.add_option('-r', '--retries', dest='retries', |
| 87 type='int', default=10, | 87 type='int', default=10, |
| 88 help='number of times to retry on failure') | 88 help='number of times to retry on failure') |
| 89 parser.add_option('-u', '--logurl', dest='logurl', | 89 parser.add_option('-u', '--logurl', dest='logurl', |
| 90 default='https://command-wrapper.appspot.com/log', | 90 default='https://command-wrapper.appspot.com/log', |
| 91 help='URL to log invocations/failures to') | 91 help='URL to log invocations/failures to') |
| (...skipping 29 matching lines...) Expand all Loading... |
| 121 print 'Command %s failed with retcode %d, try %d.' % ( | 121 print 'Command %s failed with retcode %d, try %d.' % ( |
| 122 ' '.join(args), p.returncode, r + 1) | 122 ' '.join(args), p.returncode, r + 1) |
| 123 print 'Command %s failed %d retries, giving up.' % ( | 123 print 'Command %s failed %d retries, giving up.' % ( |
| 124 ' '.join(args), options.retries) | 124 ' '.join(args), options.retries) |
| 125 | 125 |
| 126 return p.returncode | 126 return p.returncode |
| 127 | 127 |
| 128 | 128 |
| 129 if __name__ == '__main__': | 129 if __name__ == '__main__': |
| 130 sys.exit(main(sys.argv)) | 130 sys.exit(main(sys.argv)) |
| OLD | NEW |