Skip to content

Commit

Permalink
In fitbit polling, recover from any exception: log it, and reschedule…
Browse files Browse the repository at this point in the history
… the next poll.
  • Loading branch information
caarmen committed May 18, 2023
1 parent 4f589c0 commit 57cb39e
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions withingsslack/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,27 @@ def handle_fail_poll(

def fitbit_poll():
logging.info("fitbit poll")
with SessionLocal() as db:
fitbit_users = db.query(models.FitbitUser).all()
today = datetime.date.today()
for fitbit_user in fitbit_users:
latest_successful_poll = _cache_success.get(fitbit_user.oauth_userid)
if not latest_successful_poll or latest_successful_poll < today:
try:
sleep_data = fitbit_api.get_sleep(
db,
userid=fitbit_user.oauth_userid,
when=today,
)
except UserLoggedOutException:
handle_fail_poll(fitbit_user=fitbit_user, when=today)
else:
handle_success_poll(
fitbit_user=fitbit_user, sleep_data=sleep_data, when=today
)
try:
with SessionLocal() as db:
fitbit_users = db.query(models.FitbitUser).all()
today = datetime.date.today()
for fitbit_user in fitbit_users:
latest_successful_poll = _cache_success.get(fitbit_user.oauth_userid)
if not latest_successful_poll or latest_successful_poll < today:
try:
sleep_data = fitbit_api.get_sleep(
db,
userid=fitbit_user.oauth_userid,
when=today,
)
except UserLoggedOutException:
handle_fail_poll(fitbit_user=fitbit_user, when=today)
else:
handle_success_poll(
fitbit_user=fitbit_user, sleep_data=sleep_data, when=today
)
except Exception:
logging.error("Error polling fitbit", exc_info=True)
schedule_fitbit_poll()


Expand Down

0 comments on commit 57cb39e

Please sign in to comment.