Coverage for opt/mealie/lib/python3.12/site-packages/mealie/services/scheduler/scheduler_service.py: 75%

56 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-11-25 15:48 +0000

1import asyncio 1a

2from datetime import UTC, datetime, timedelta 1a

3from pathlib import Path 1a

4 

5from mealie.core import root_logger 1a

6from mealie.core.config import get_app_settings 1a

7from mealie.services.scheduler.runner import repeat_every 1a

8 

9from .scheduler_registry import SchedulerRegistry 1a

10 

11logger = root_logger.get_logger() 1a

12 

13CWD = Path(__file__).parent 1a

14 

15MINUTES_DAY = 1440 1a

16MINUTES_5 = 5 1a

17MINUTES_HOUR = 60 1a

18 

19 

20class SchedulerService: 1a

21 @staticmethod 1a

22 async def start(): 1a

23 await run_minutely() 1a

24 await run_hourly() 1a

25 

26 # Wait to trigger our daily run until our given "daily time", so having asyncio handle it. 

27 asyncio.create_task(schedule_daily()) 1a

28 

29 

30async def schedule_daily(): 1a

31 now = datetime.now(UTC) 1a

32 daily_schedule_time = get_app_settings().DAILY_SCHEDULE_TIME_UTC 1a

33 logger.debug(f"Current time is {now} and DAILY_SCHEDULE_TIME (in UTC) is {daily_schedule_time}") 1a

34 

35 next_schedule = now.replace( 1a

36 hour=daily_schedule_time.hour, minute=daily_schedule_time.minute, second=0, microsecond=0 

37 ) 

38 delta = next_schedule - now 1a

39 if delta < timedelta(0): 39 ↛ 40line 39 didn't jump to line 40 because the condition on line 39 was never true1a

40 next_schedule = next_schedule + timedelta(days=1) 

41 delta = next_schedule - now 

42 

43 hours_until, seconds_reminder = divmod(delta.total_seconds(), 3600) 1a

44 minutes_until, seconds_reminder = divmod(seconds_reminder, 60) 1a

45 seconds_until = round(seconds_reminder) 1a

46 logger.debug("Time left: %02d:%02d:%02d", hours_until, minutes_until, seconds_until) 1a

47 

48 target_time = next_schedule.replace(microsecond=0, second=0) 1a

49 logger.info("Daily tasks scheduled for %s", str(target_time)) 1a

50 

51 wait_seconds = delta.total_seconds() 1a

52 await asyncio.sleep(wait_seconds) 1a

53 await run_daily() 

54 

55 

56def _scheduled_task_wrapper(callable): 1a

57 try: 1bcd

58 callable() 1bcd

59 except Exception as e: 

60 logger.error("Error in scheduled task func='%s': exception='%s'", callable.__name__, e) 

61 

62 

63@repeat_every(minutes=MINUTES_DAY, wait_first=False, logger=logger) 1a

64def run_daily(): 1a

65 logger.debug("Running daily callbacks") 

66 for func in SchedulerRegistry._daily: 

67 _scheduled_task_wrapper(func) 

68 

69 

70@repeat_every(minutes=MINUTES_HOUR, wait_first=True, logger=logger) 1a

71def run_hourly(): 1a

72 logger.debug("Running hourly callbacks") 

73 for func in SchedulerRegistry._hourly: 

74 _scheduled_task_wrapper(func) 

75 

76 

77@repeat_every(minutes=MINUTES_5, wait_first=True, logger=logger) 1a

78def run_minutely(): 1a

79 logger.debug("Running minutely callbacks") 1bcd

80 for func in SchedulerRegistry._minutely: 1bcd

81 _scheduled_task_wrapper(func) 1bcd