وضعیت وظیفه نظرسنجی

فایل‌های داده ساختاریافته آپلود شده، منابع را فوراً ایجاد یا به‌روزرسانی نمی‌کنند. پردازش SDFهای شما و به‌روزرسانی‌های حاصل توسط Display & Video 360 ممکن است از چند ثانیه تا چند ساعت طول بکشد. چندین فایل را می‌توان همزمان آپلود کرد، مشروط بر اینکه حاوی منابع همپوشانی نباشند.

برای اینکه بفهمید آیا وظیفه‌تان تمام شده است یا نه، مرتباً عملیات را با استفاده از sdfuploadtasks.operations.get بازیابی کنید و بررسی کنید که آیا فیلد done True است و وظیفه تمام شده است یا خیر. این فرآیند به عنوان "polling" شناخته می‌شود.

اگر وظیفه به پایان رسیده باشد، فیلد response یا error تنظیم می‌شود. اگر فیلد error پر شده باشد، وظیفه با شکست مواجه شده و جزئیات مربوط به شکست در شیء Status حاصل موجود است. اگر فیلد response پر شده باشد، یک SdfUploadTask به این فیلد اختصاص داده می‌شود. این بدان معناست که Display & Video 360 با موفقیت SDF آپلود شده را پردازش کرده است. فایل‌های نتیجه را از محل ارائه شده در فیلد SdfUploadTask.resourceName دانلود کنید.

یک پیاده‌سازی ناکارآمد از polling که یک گزارش طولانی‌مدت را بررسی می‌کند، مقدار زیادی از سهمیه درخواست API شما را مصرف می‌کند. برای محدود کردن تلاش‌های مجدد و حفظ سهمیه، از روش برگشت نمایی (exponential backoff) استفاده کنید.

در اینجا نحوه‌ی نمونه‌برداری از عملیات آپلود SDF با استفاده از backoff نمایی آمده است:

# 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))