|
| 1 | +import dataclasses |
| 2 | +from typing import Any, List, Optional |
| 3 | + |
| 4 | +from redis.asyncio import ConnectionPool, Redis |
| 5 | +from taskiq import ScheduleSource |
| 6 | +from taskiq.abc.serializer import TaskiqSerializer |
| 7 | +from taskiq.scheduler.scheduled_task import ScheduledTask |
| 8 | + |
| 9 | +from taskiq_redis.serializer import PickleSerializer |
| 10 | + |
| 11 | + |
| 12 | +class RedisScheduleSource(ScheduleSource): |
| 13 | + """ |
| 14 | + Source of schedules for redis. |
| 15 | +
|
| 16 | + This class allows you to store schedules in redis. |
| 17 | + Also it supports dynamic schedules. |
| 18 | +
|
| 19 | + :param url: url to redis. |
| 20 | + :param prefix: prefix for redis schedule keys. |
| 21 | + :param buffer_size: buffer size for redis scan. |
| 22 | + This is how many keys will be fetched at once. |
| 23 | + :param max_connection_pool_size: maximum number of connections in pool. |
| 24 | + :param serializer: serializer for data. |
| 25 | + :param connection_kwargs: additional arguments for aio-redis ConnectionPool. |
| 26 | + """ |
| 27 | + |
| 28 | + def __init__( |
| 29 | + self, |
| 30 | + url: str, |
| 31 | + prefix: str = "schedule", |
| 32 | + buffer_size: int = 50, |
| 33 | + max_connection_pool_size: Optional[int] = None, |
| 34 | + serializer: Optional[TaskiqSerializer] = None, |
| 35 | + **connection_kwargs: Any, |
| 36 | + ) -> None: |
| 37 | + self.prefix = prefix |
| 38 | + self.connection_pool: ConnectionPool = ConnectionPool.from_url( |
| 39 | + url=url, |
| 40 | + max_connections=max_connection_pool_size, |
| 41 | + **connection_kwargs, |
| 42 | + ) |
| 43 | + self.buffer_size = buffer_size |
| 44 | + if serializer is None: |
| 45 | + serializer = PickleSerializer() |
| 46 | + self.serializer = serializer |
| 47 | + |
| 48 | + async def delete_schedule(self, schedule_id: str) -> None: |
| 49 | + """Remove schedule by id.""" |
| 50 | + async with Redis(connection_pool=self.connection_pool) as redis: |
| 51 | + await redis.delete(f"{self.prefix}:{schedule_id}") |
| 52 | + |
| 53 | + async def add_schedule(self, schedule: ScheduledTask) -> None: |
| 54 | + """ |
| 55 | + Add schedule to redis. |
| 56 | +
|
| 57 | + :param schedule: schedule to add. |
| 58 | + :param schedule_id: schedule id. |
| 59 | + """ |
| 60 | + async with Redis(connection_pool=self.connection_pool) as redis: |
| 61 | + await redis.set( |
| 62 | + f"{self.prefix}:{schedule.schedule_id}", |
| 63 | + self.serializer.dumpb(dataclasses.asdict(schedule)), |
| 64 | + ) |
| 65 | + |
| 66 | + async def get_schedules(self) -> List[ScheduledTask]: |
| 67 | + """ |
| 68 | + Get all schedules from redis. |
| 69 | +
|
| 70 | + This method is used by scheduler to get all schedules. |
| 71 | +
|
| 72 | + :return: list of schedules. |
| 73 | + """ |
| 74 | + schedules = [] |
| 75 | + async with Redis(connection_pool=self.connection_pool) as redis: |
| 76 | + buffer = [] |
| 77 | + async for key in redis.scan_iter(f"{self.prefix}:*"): |
| 78 | + buffer.append(key) |
| 79 | + if len(buffer) >= self.buffer_size: |
| 80 | + schedules.extend(await redis.mget(buffer)) |
| 81 | + buffer = [] |
| 82 | + if buffer: |
| 83 | + schedules.extend(await redis.mget(buffer)) |
| 84 | + return [ |
| 85 | + ScheduledTask(**self.serializer.loadb(schedule)) |
| 86 | + for schedule in schedules |
| 87 | + if schedule |
| 88 | + ] |
| 89 | + |
| 90 | + async def post_send(self, task: ScheduledTask) -> None: |
| 91 | + """Delete a task after it's completed.""" |
| 92 | + if task.time is not None: |
| 93 | + await self.delete_schedule(task.schedule_id) |
| 94 | + |
| 95 | + async def shutdown(self) -> None: |
| 96 | + """Shut down the schedule source.""" |
| 97 | + await self.connection_pool.disconnect() |
0 commit comments