33import sys
44from datetime import datetime , timedelta
55from logging import basicConfig , getLevelName , getLogger
6- from typing import Any , Dict , List , Optional , Set
6+ from typing import Any , Dict , List , Optional , Set , Tuple
77
88import pytz
99from pycron import is_now
@@ -55,7 +55,7 @@ async def get_schedules(source: ScheduleSource) -> List[ScheduledTask]:
5555
5656async def get_all_schedules (
5757 scheduler : TaskiqScheduler ,
58- ) -> Dict [ ScheduleSource , List [ScheduledTask ]]:
58+ ) -> List [ Tuple [ ScheduleSource , List [ScheduledTask ] ]]:
5959 """
6060 Task to update all schedules.
6161
@@ -71,7 +71,7 @@ async def get_all_schedules(
7171 schedules = await asyncio .gather (
7272 * [get_schedules (source ) for source in scheduler .sources ],
7373 )
74- return dict (zip (scheduler .sources , schedules ))
74+ return list (zip (scheduler .sources , schedules ))
7575
7676
7777def get_task_delay (task : ScheduledTask ) -> Optional [int ]:
@@ -162,15 +162,22 @@ async def run_scheduler_loop( # noqa: C901
162162 current_minute = datetime .now (tz = pytz .UTC ).minute
163163 while True :
164164 now = datetime .now (tz = pytz .UTC )
165+ # If minute changed, we need to clear
166+ # ran_cron_jobs set and update current minute.
165167 if now .minute != current_minute :
166168 current_minute = now .minute
167169 ran_cron_jobs .clear ()
170+ # If interval is not None, we need to
171+ # calculate next run time using it.
168172 if interval is not None :
169173 next_run = now + interval
174+ # otherwise we need assume that
175+ # we will run it at the start of the next minute.
176+ # as crontab does.
170177 else :
171178 next_run = (now + timedelta (minutes = 1 )).replace (second = 1 , microsecond = 0 )
172179 scheduled_tasks = await get_all_schedules (scheduler )
173- for source , task_list in scheduled_tasks . items () :
180+ for source , task_list in scheduled_tasks :
174181 logger .debug ("Got %d schedules from source %s." , len (task_list ), source )
175182 for task in task_list :
176183 try :
0 commit comments