Cancels the given BatchJob.

  Args:
    client: an instantiated AdWordsClient used to cancel the BatchJob.
    batch_job: a BatchJob to be canceled.
    max_poll_attempts: an int defining the number of times the BatchJob will be
      checked to determine whether it has been canceled.

def CancelBatchJob(client, batch_job, max_poll_attempts=MAX_POLL_ATTEMPTS):
  """Cancels the given BatchJob.

  Args:
    client: an instantiated AdWordsClient used to cancel the BatchJob.
    batch_job: a BatchJob to be canceled.
    max_poll_attempts: an int defining the number of times the BatchJob will be
      checked to determine whether it has been canceled.
  """
  batch_job_service = client.GetService('BatchJobService', 'v201809')
  batch_job['status'] = 'CANCELING'

  operation = {
      'operator': 'SET',
      'operand': batch_job
  }

  batch_job_service.mutate([operation])

  # Verify that the Batch Job cancels.
  poll_attempt = 0

  while (poll_attempt in range(max_poll_attempts) and
         batch_job['status'] != 'CANCELED'):
    sleep_interval = (30 * (2 ** poll_attempt) +
                      (random.randint(0, 10000) / 1000))
    print ('Batch Job not finished canceling, sleeping for %s seconds.'
           % sleep_interval)
    time.sleep(sleep_interval)
    batch_job = GetBatchJob(client, batch_job['id'])
    poll_attempt += 1

  if batch_job['status'] == 'CANCELED':
    print ('Batch Job with ID "%d" has been successfully canceled.' %
           batch_job['id'])
  else:
    print ('Batch Job with ID "%d" failed to cancel after polling %d times.'
           % (batch_job['id'], max_poll_attempts))