Created
          August 27, 2014 03:49 
        
      - 
      
 - 
        
Save feczo/bfd9991e4f84dffbc661 to your computer and use it in GitHub Desktop.  
    Execute BigQuery request with retry
  
        
  
    
      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 ExecuteRequestWithRetries(request, num_tries=5): | |
| """Executes a request and retries certain failures. | |
| Failures are retried if they are in the list of errors to retry. | |
| List includes system errors(500, 503) and an | |
| authentication error(401) | |
| Args: | |
| request: The request to issue to big query. It must be an object with | |
| an execute method. | |
| num_tries: The number of times to attempt the request. | |
| Returns: | |
| The results of the request. | |
| """ | |
| for _ in xrange(num_tries -1): | |
| try: | |
| return request.execute() | |
| except HttpError as e: | |
| if e.resp['status'] not in [401, 500, 503]: | |
| raise | |
| return request.execute() | |
| bigquery_service = build('bigquery', 'v2', http=http) | |
| jobCollection = bigquery_service.jobs() | |
| current_job = ExecuteRequestWithRetries( | |
| jobCollection.get(projectId = PROJECT_NUMBER, jobId = bq_job_id)) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment