Trạng thái của nhiệm vụ thăm dò

Tệp dữ liệu có cấu trúc không được tạo ngay lập tức. Display & Video 360 có thể mất từ vài giây đến vài giờ để tạo tệp SDF.

Để xác định xem tác vụ của bạn đã hoàn tất hay chưa, hãy thường xuyên truy xuất thao tác bằng cách sử dụng sdfdownloadtasks.operations.get và kiểm tra xem trường done có phải là True hay không và tác vụ đã hoàn tất hay chưa. Quá trình này được gọi là "bỏ phiếu".

Nếu tác vụ đã hoàn tất, thì trường response hoặc error sẽ được đặt. Nếu trường error được điền sẵn, thì tác vụ không thành công và thông tin chi tiết về lỗi có trong đối tượng Status thu được. Nếu trường response được điền sẵn, thì Display & Video 360 đã tạo thành công các tệp SDF. Tải các tệp kết quả xuống từ vị trí được cung cấp trong response.resourceName.

Một quy trình triển khai thăm dò ý kiến không hiệu quả kiểm tra báo cáo chạy trong thời gian dài sẽ tiêu tốn nhiều hạn mức yêu cầu API của bạn. Để giới hạn số lần thử lại và tiết kiệm hạn mức, hãy sử dụng thuật toán thời gian đợi luỹ thừa.

Sau đây là cách thăm dò hoạt động tải SDF xuống bằng cách sử dụng thuật toán thời gian đợi lũy thừa:

# Provide the name of the sdfdownloadtask operation.
operation_name = operation-name

# Set the following values that control retry behavior while the operation
# is running.
# Minimum amount of time between polling requests. Defaults to 5 seconds.
min_retry_interval = 5
# Maximum amount of time between polling requests. Defaults to 5 minutes.
max_retry_interval = 5 * 60
# Maximum amount of time to spend polling. Defaults to 5 hours.
max_retry_elapsed_time = 5 * 60 * 60

# Configure the sdfdownloadtasks.operations.get request.
get_request = service.sdfdownloadtasks().operations().get(operation_name)

sleep = 0

start_time = time.time()
while True:
  # Get current status of the report.
  operation = get_request.execute()

  if "done" in operation:
    if "error" in operation:
      print(
        f'The operation finished in error with code '
        f'{operation["error"]["code"]}: {operation["error"]["message"]}')
    else:
      print(
        f'The operation completed successfully. The resulting files can be '
        f'downloaded at {operation["response"]["resourceName"]}.')
    break
  elif time.time() - start_time > max_retry_elapsed_time:
    print("SDF generation deadline exceeded.")
    break

  sleep = next_sleep_interval(sleep)
  print(
    f'Operation {operation_name} is still running, sleeping for '
    f'{sleep} seconds.')
  time.sleep(sleep)

def next_sleep_interval(previous_sleep_interval):
  """Calculates the next sleep interval based on the previous."""
  min_interval = previous_sleep_interval or min_retry_interval
  max_interval = previous_sleep_interval * 3 or min_retry_interval
  return min(max_retry_interval, random.randint(min_interval, max_interval))