Created
February 5, 2010 03:59
-
-
Save gabriel/295488 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def _fetch(self, url, retry_count, **kwargs): | |
| """Calls urlfetch.fetch with retry count""" | |
| try_count = 0 | |
| times = [] | |
| response = None | |
| while try_count < retry_count: | |
| try: | |
| try_count += 1 | |
| # Fetch the url | |
| fetch_start = datetime.now() | |
| response = urlfetch.fetch(url, **kwargs) | |
| times.append(datetime.now() - fetch_start) | |
| # TODO(gabe): Handle PermanentRedirect error messages (no Location header on 301 so need to handle manually) | |
| # Retry on 5xx errors as well as urlfetch exceptions | |
| if int(response.status_code) in xrange(500, 600): | |
| logging.info('Failed with status: %s, retrying' % (response.status_code)) | |
| continue | |
| return response, try_count, times | |
| except Exception, error: | |
| logging.error('Error(%s): %s' % (try_count, error)) | |
| if try_count >= retry_count: | |
| raise | |
| return response, try_count, times |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment