Skip to content

Commit

Permalink
Change retry time to 5-10 minutes after the hour
Browse files Browse the repository at this point in the history
randomly picks a time between 5 and 10 minutes after the hour to retry, preventing any issues caused by a small difference between the computer's time and Fitbit's server's time.
  • Loading branch information
lightmaster authored Nov 30, 2017
1 parent 6afb33b commit b9f84d8
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from oauth2client.client import OAuth2Credentials
from googleapiclient.errors import HttpError

from random import randint
from app import DATE_FORMAT

class Remote:
Expand Down Expand Up @@ -53,12 +54,14 @@ def ReadFromFitbit(self, api_call, *args, **kwargs):
try:
resp = api_call(*args,**kwargs)
except HTTPTooManyRequests as e:
# retry between 5-10 minutes after the hour
seconds_till_retry = e.retry_after_secs + randint(300,600)
print('')
print('-------------------- Fitbit API rate limit reached -------------------')
retry_time = datetime.now()+timedelta(seconds=e.retry_after_secs)
retry_time = datetime.now()+timedelta(seconds=seconds_till_retry)
print('Will retry at {}'.format(retry_time.strftime('%H:%M:%S')))
print('')
time.sleep(e.retry_after_secs)
time.sleep(seconds_till_retry)
resp = self.ReadFromFitbit(api_call,*args,**kwargs)
return resp

Expand Down

0 comments on commit b9f84d8

Please sign in to comment.