Skip to content

Commit 9cba998

Browse files
committed
change schedules storage to list of tuples
1 parent aff8c46 commit 9cba998

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

taskiq/cli/scheduler/run.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44
from datetime import datetime, timedelta, timezone
55
from logging import basicConfig, getLogger
6-
from typing import Any, Dict, List, Optional, Union
6+
from typing import Any, Dict, List, Optional, Tuple, Union
77
from zoneinfo import ZoneInfo
88

99
import pycron
@@ -59,23 +59,22 @@ async def get_schedules(source: ScheduleSource) -> List[ScheduledTask]:
5959

6060
async def get_all_schedules(
6161
scheduler: TaskiqScheduler,
62-
) -> Dict[ScheduleSource, List[ScheduledTask]]:
62+
) -> List[Tuple[ScheduleSource, List[ScheduledTask]]]:
6363
"""
6464
Task to update all schedules.
6565
6666
This function updates all schedules
67-
from all sources and returns a dict
68-
with source as a key and list of
69-
scheduled tasks as a value.
67+
from all sources and returns a list
68+
of (source, tasks) pairs.
7069
7170
:param scheduler: current scheduler.
72-
:return: dict with source as a key and list of scheduled tasks as a value.
71+
:return: list of (source, tasks) pairs.
7372
"""
7473
logger.debug("Started schedule update.")
7574
schedules: List[List[ScheduledTask]] = await asyncio.gather(
7675
*[get_schedules(source) for source in scheduler.sources],
7776
)
78-
return dict(zip(scheduler.sources, schedules))
77+
return list(zip(scheduler.sources, schedules))
7978

8079

8180
class CronValueError(Exception):
@@ -197,15 +196,15 @@ def __init__(
197196
self.interval_tasks_last_run: dict[ScheduleId, datetime] = {}
198197
self.time_tasks_last_run: dict[ScheduleId, datetime] = {}
199198

200-
self.scheduled_tasks: Dict[ScheduleSource, List[ScheduledTask]] = {}
199+
self.scheduled_tasks: List[Tuple[ScheduleSource, List[ScheduledTask]]] = []
201200
self.scheduled_tasks_updated_at: Optional[datetime] = None
202201
self._update_schedules_task_future: Optional[asyncio.Task[Any]] = None
203202

204203
def _update_schedules_task_future_callback(self, task_: asyncio.Task[Any]) -> None:
205204
self.scheduled_tasks = task_.result()
206205

207206
new_schedules_ids: set[ScheduleId] = set()
208-
for source, task_list in self.scheduled_tasks.items():
207+
for source, task_list in self.scheduled_tasks:
209208
logger.debug("Got %d schedules from source %s.", len(task_list), source)
210209
new_schedules_ids.update({t.schedule_id for t in task_list})
211210

@@ -237,7 +236,7 @@ async def _update_scheduled_tasks(self) -> None:
237236

238237
def _mark_cron_tasks_as_already_run(self) -> None:
239238
current_minute = datetime.now(tz=timezone.utc).replace(second=0, microsecond=0)
240-
for _, task_list in self.scheduled_tasks.items():
239+
for _, task_list in self.scheduled_tasks:
241240
for task in task_list:
242241
if task.cron is not None:
243242
self.cron_tasks_last_run[task.schedule_id] = current_minute
@@ -328,7 +327,7 @@ async def run(
328327
await self._update_scheduled_tasks()
329328
self.scheduled_tasks_updated_at = now
330329

331-
for source, task_list in self.scheduled_tasks.items():
330+
for source, task_list in self.scheduled_tasks:
332331
for task in task_list:
333332
is_ready_to_send: bool = self._is_schedule_ready_to_send(
334333
task=task,

tests/cli/scheduler/test_updater.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ async def test_get_schedules_success() -> None:
5353
schedules = await get_all_schedules(
5454
TaskiqScheduler(InMemoryBroker(), sources),
5555
)
56-
assert schedules == {
57-
sources[0]: schedules1,
58-
sources[1]: schedules2,
59-
}
56+
assert schedules == [
57+
(sources[0], schedules1),
58+
(sources[1], schedules2),
59+
]
6060

6161

6262
async def test_get_schedules_error() -> None:
@@ -77,7 +77,7 @@ async def test_get_schedules_error() -> None:
7777
schedules = await get_all_schedules(
7878
TaskiqScheduler(InMemoryBroker(), [source1, source2]),
7979
)
80-
assert schedules == {
81-
source1: source1.schedules,
82-
source2: [],
83-
}
80+
assert schedules == [
81+
(source1, source1.schedules),
82+
(source2, []),
83+
]

0 commit comments

Comments
 (0)