Skip to content

Commit

Permalink
Log any exception that may occur in an iteration of processing daily …
Browse files Browse the repository at this point in the history
…activities.
  • Loading branch information
caarmen committed Aug 20, 2024
1 parent 250b0f8 commit 05d2f42
Showing 1 changed file with 37 additions and 34 deletions.
71 changes: 37 additions & 34 deletions slackhealthbot/tasks/post_daily_activities_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,46 @@ async def post_daily_activities(

async def task():
while True:
logger.info("Processing daily activities")
now = dt.datetime.now()
try:
logger.info("Processing daily activities")
now = dt.datetime.now()

next_task_datetime = now.replace(
hour=post_time.hour,
minute=post_time.minute,
second=post_time.second,
microsecond=post_time.microsecond,
)
if next_task_datetime < now:
next_task_datetime += dt.timedelta(days=1)
time_until_next_task_datetime_s = (next_task_datetime - now).seconds
logger.info(
f"Sleeping {time_until_next_task_datetime_s} seconds until next daily summary at {next_task_datetime}"
)
# Add a few seconds to the sleep duration.
# This will avoid the following error where our loop was called
# just before (<500ms before) the scheduled time of 21:00, at 20:59:59,549.
# We finished the processing quickly (before 20:59:59,778), and
# scheduled it for the "next 21:00", which was just a few milliseconds in the future.
# Logs:
"""
2024-08-18 14:26:08,928 Sleeping 23631 seconds until next daily summary at 2024-08-18 21:00:00
next_task_datetime = now.replace(
hour=post_time.hour,
minute=post_time.minute,
second=post_time.second,
microsecond=post_time.microsecond,
)
if next_task_datetime < now:
next_task_datetime += dt.timedelta(days=1)
time_until_next_task_datetime_s = (next_task_datetime - now).seconds
logger.info(
f"Sleeping {time_until_next_task_datetime_s} seconds until next daily summary at {next_task_datetime}"
)
# Add a few seconds to the sleep duration.
# This will avoid the following error where our loop was called
# just before (<500ms before) the scheduled time of 21:00, at 20:59:59,549.
# We finished the processing quickly (before 20:59:59,778), and
# scheduled it for the "next 21:00", which was just a few milliseconds in the future.
# Logs:
"""
2024-08-18 14:26:08,928 Sleeping 23631 seconds until next daily summary at 2024-08-18 21:00:00
2024-08-18 21:00:00,195 Sleeping 86399 seconds until next daily summary at 2024-08-19 21:00:00
2024-08-18 21:00:00,195 Sleeping 86399 seconds until next daily summary at 2024-08-19 21:00:00
2024-08-19 20:59:59,549 Sleeping 0 seconds until next daily summary at 2024-08-19 21:00:00
2024-08-19 20:59:59,778 Sleeping 0 seconds until next daily summary at 2024-08-19 21:00:00
2024-08-19 21:00:00,025 Sleeping 86399 seconds until next daily summary at 2024-08-20 21:00:00
"""
await asyncio.sleep(time_until_next_task_datetime_s + 3)
2024-08-19 20:59:59,549 Sleeping 0 seconds until next daily summary at 2024-08-19 21:00:00
2024-08-19 20:59:59,778 Sleeping 0 seconds until next daily summary at 2024-08-19 21:00:00
2024-08-19 21:00:00,025 Sleeping 86399 seconds until next daily summary at 2024-08-20 21:00:00
"""
await asyncio.sleep(time_until_next_task_datetime_s + 3)

async with local_fitbit_repo_factory() as local_fitbit_repo:
await usecase_process_daily_activities.do(
local_fitbit_repo=local_fitbit_repo,
type_ids=activity_type_ids,
slack_repo=slack_repo,
)
async with local_fitbit_repo_factory() as local_fitbit_repo:
await usecase_process_daily_activities.do(
local_fitbit_repo=local_fitbit_repo,
type_ids=activity_type_ids,
slack_repo=slack_repo,
)
except Exception:
logging.error("Error processing daily activities", exc_info=True)

return asyncio.create_task(task())

0 comments on commit 05d2f42

Please sign in to comment.