-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathtasks.py
More file actions
133 lines (111 loc) · 5.12 KB
/
tasks.py
File metadata and controls
133 lines (111 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import logging
import sys
from celery import shared_task
from celery.exceptions import Ignore, Reject
from dateutil import parser
from django.core.cache import cache
from fitbit.exceptions import HTTPBadRequest, HTTPTooManyRequests
from . import utils
from .models import UserFitbit, TimeSeriesData, TimeSeriesDataType
logger = logging.getLogger(__name__)
LOCK_EXPIRE = 60 * 5 # Lock expires in 5 minutes
def _hit_rate_limit(exc, task):
# We have hit the rate limit for the user, retry when it's reset,
# according to the reply from the failing API call
logger.debug('Rate limit reached, will try again in %s seconds' %
exc.retry_after_secs)
raise task.retry(exc=exc, countdown=exc.retry_after_secs)
def _generic_task_exception(exc, task_name):
logger.exception("Exception running task %s: %s" % (task_name, exc))
raise Reject(exc, requeue=False)
@shared_task
def subscribe(fitbit_user, subscriber_id):
""" Subscribe the user and retrieve historical data for it """
update_user_timezone.apply_async((fitbit_user,), countdown=1)
for fbuser in UserFitbit.objects.filter(fitbit_user=fitbit_user):
fb = utils.create_fitbit(**fbuser.get_user_data())
try:
fb.subscription(fbuser.user.id, subscriber_id)
except HTTPTooManyRequests:
_hit_rate_limit(sys.exc_info()[1], subscribe)
except Exception:
_generic_task_exception(sys.exc_info()[1], 'subscribe')
# Create tasks for all data in all data types
for i, _type in enumerate(TimeSeriesDataType.objects.all()):
# Delay execution for a few seconds to speed up response Offset each
# call by 5 seconds so they don't bog down the server
get_time_series_data.apply_async(
(fitbit_user, _type.category, _type.resource,),
countdown=10 + (i * 5))
@shared_task
def unsubscribe(*args, **kwargs):
""" Unsubscribe from a user's fitbit data """
fb = utils.create_fitbit(**kwargs)
try:
for sub in fb.list_subscriptions()['apiSubscriptions']:
if sub['ownerId'] == kwargs['user_id']:
fb.subscription(sub['subscriptionId'], sub['subscriberId'],
method="DELETE")
except HTTPTooManyRequests:
_hit_rate_limit(sys.exc_info()[1], unsubscribe)
except Exception:
_generic_task_exception(sys.exc_info()[1], 'unsubscribe')
@shared_task
def get_time_series_data(fitbit_user, cat, resource, date=None):
""" Get the user's time series data """
try:
_type = TimeSeriesDataType.objects.get(category=cat, resource=resource)
except TimeSeriesDataType.DoesNotExist:
logger.exception("The resource %s in category %s doesn't exist" % (
resource, cat))
raise Reject(sys.exc_info()[1], requeue=False)
# Create a lock so we don't try to run the same task multiple times
sdat = date.strftime('%Y-%m-%d') if date else 'ALL'
lock_id = '{0}-lock-{1}-{2}-{3}'.format(__name__, fitbit_user, _type, sdat)
if not cache.add(lock_id, 'true', LOCK_EXPIRE):
logger.debug('Already retrieving %s data for date %s, user %s' % (
_type, fitbit_user, sdat))
raise Ignore()
fbusers = UserFitbit.objects.filter(fitbit_user=fitbit_user)
dates = {'base_date': 'today', 'period': 'max'}
if date:
dates = {'base_date': date, 'end_date': date}
try:
for fbuser in fbusers:
data = utils.get_fitbit_data(fbuser, _type, **dates)
for datum in data:
# Create new record or update existing record
date = parser.parse(datum['dateTime'])
tsd, created = TimeSeriesData.objects.get_or_create(
user=fbuser.user, resource_type=_type, date=date)
tsd.value = datum['value']
tsd.save()
# Release the lock
cache.delete(lock_id)
except HTTPTooManyRequests:
_hit_rate_limit(sys.exc_info()[1], get_time_series_data)
except HTTPBadRequest:
# If the resource is elevation or floors, we are just getting this
# error because the data doesn't exist for this user, so we can ignore
# the error
if not ('elevation' in resource or 'floors' in resource):
exc = sys.exc_info()[1]
logger.exception("Exception updating data: %s" % exc)
raise Reject(exc, requeue=False)
except Exception:
_generic_task_exception(sys.exc_info()[1], 'get_time_series_data')
@shared_task
def update_user_timezone(fitbit_user):
""" Get the user's profile and update the timezone we have on file """
fbusers = UserFitbit.objects.filter(fitbit_user=fitbit_user)
try:
for fbuser in fbusers:
fb = utils.create_fitbit(**fbuser.get_user_data())
profile = fb.user_profile_get()
fbuser.timezone = profile['user']['timezone']
fbuser.save()
utils.check_for_new_token(fbuser, fb.client.token)
except HTTPTooManyRequests:
_hit_rate_limit(sys.exc_info()[1], update_user_timezone)
except Exception:
_generic_task_exception(sys.exc_info()[1], 'update_user_timezone')