xdevplatform/twitter-python-ads-sdk

503 error when queuing an async stats job once every few tries

Closed this issue · 1 comments

When queuing an async stats job I get a 503 with a message: 'Service unavailable due to request timeout; please try the request again later'. This error occurs after running my program several times. I cant reproduce the error consistently. My program is calling this queue_async_stats_job in batches of 20 line item ids, for over 1000 line item ids. The call to this method generally succeeds, except after running it several times in a row.The interval between start_time and end_time is exactly 20 days. When queuing an async stats job does succeed I am able to call async_stats_job_result on the queued jobs, retrieve the url and call async_stats_job_data to get the data.

Why does calling queue_async_stats_job fail irregularly and how can I prevent it from failing?

queued_job = Analytics.queue_async_stats_job(account, chunk, metrics, start_time=start_time, end_time=end_time, entity=entity_type, granularity=GRANULARITY.DAY)
File "/home/ubuntu/.local/lib/python3.6/site-packages/twitter_ads/resource.py", line 294, in queue_async_stats_job
response = Request(account.client, 'post', resource, params=params).perform()
File "/home/ubuntu/.local/lib/python3.6/site-packages/twitter_ads/http.py", line 69, in perform
raise Error.from_response(response)
twitter_ads.error.ServiceUnavailable: <ServiceUnavailable object at 0x7fbb41fece88 code=503 details=[{'code': 'SERVICE_UNAVAILABLE', 'message': 'Service unavailable due to request timeout; please try the request again later'}]>

Hi @ViRaL95, generally speaking, you may see those random 503 errors in case of resource exhaustion. If you're using the latest SDK version, you can set request retry options like this:

client = Client(
    CONSUMER_KEY,
    CONSUMER_SECRET,
    ACCESS_TOKEN,
    ACCESS_TOKEN_SECRET,
    options={
        'handle_rate_limit': True,
        'retry_max': 3,
        'retry_delay': 5000,
        'retry_on_status': [500, 503]
    })

See the details in this release note:
https://github.com/twitterdev/twitter-python-ads-sdk/wiki/Release-Notes#v530

or, of course, you can manually catch the error (try-except) and have your own retry logic as you need.