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

Các tệp dữ liệu có cấu trúc được tải lên không tạo hoặc cập nhật tài nguyên ngay lập tức. Display & Video 360 có thể mất từ vài giây đến vài giờ để xử lý tệp SDF và thực hiện các nội dung cập nhật tương ứng. Bạn có thể tải nhiều tệp lên cùng lúc, miễn là các tệp đó không chứa tài nguyên trùng lặp.

Để 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 sdfuploadtasks.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ì SdfUploadTask sẽ được chỉ định cho trường đó. Điều này có nghĩa là Display & Video 360 đã xử lý thành công tệp SDF được tải lên. Tải các tệp kết quả xuống từ vị trí được cung cấp trong trường SdfUploadTask.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ò một thao tác tải SDF lên bằng cách sử dụng thuật toán thời gian đợi lũy thừa:

# Import the necessary libraries
import random
import time

# Provide the name of the sdfuploadtask 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 sdfuploadtasks.operations.get request.
get_request = service.sdfuploadtasks().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 result files can be '
        f'downloaded at {operation["response"]["resourceName"]}.')
    break
  elif time.time() - start_time > max_retry_elapsed_time:
    print("Polling 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))