-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "taskiq-redis" | ||
version = "0.5.2" | ||
version = "0.5.3" | ||
description = "Redis integration for taskiq" | ||
authors = ["taskiq-team <[email protected]>"] | ||
readme = "README.md" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from typing import Any, AsyncGenerator | ||
|
||
from redis.asyncio import RedisCluster | ||
from taskiq.abc.broker import AsyncBroker | ||
from taskiq.message import BrokerMessage | ||
|
||
|
||
class BaseRedisClusterBroker(AsyncBroker): | ||
"""Base broker that works with Redis Cluster.""" | ||
|
||
def __init__( | ||
self, | ||
url: str, | ||
queue_name: str = "taskiq", | ||
max_connection_pool_size: int = 2**31, | ||
**connection_kwargs: Any, | ||
) -> None: | ||
""" | ||
Constructs a new broker. | ||
:param url: url to redis. | ||
:param queue_name: name for a list in redis. | ||
:param max_connection_pool_size: maximum number of connections in pool. | ||
:param connection_kwargs: additional arguments for aio-redis ConnectionPool. | ||
""" | ||
super().__init__() | ||
|
||
self.redis: RedisCluster[bytes] = RedisCluster.from_url( | ||
url=url, | ||
max_connections=max_connection_pool_size, | ||
**connection_kwargs, | ||
) | ||
|
||
self.queue_name = queue_name | ||
|
||
async def shutdown(self) -> None: | ||
"""Closes redis connection pool.""" | ||
await self.redis.aclose() # type: ignore[attr-defined] | ||
await super().shutdown() | ||
|
||
|
||
class ListQueueClusterBroker(BaseRedisClusterBroker): | ||
"""Broker that works with Redis Cluster and distributes tasks between workers.""" | ||
|
||
async def kick(self, message: BrokerMessage) -> None: | ||
""" | ||
Put a message in a list. | ||
This method appends a message to the list of all messages. | ||
:param message: message to append. | ||
""" | ||
await self.redis.lpush(self.queue_name, message.message) # type: ignore[attr-defined] | ||
|
||
async def listen(self) -> AsyncGenerator[bytes, None]: | ||
""" | ||
Listen redis queue for new messages. | ||
This function listens to the queue | ||
and yields new messages if they have BrokerMessage type. | ||
:yields: broker messages. | ||
""" | ||
redis_brpop_data_position = 1 | ||
while True: | ||
value = await self.redis.brpop([self.queue_name]) # type: ignore[attr-defined] | ||
yield value[redis_brpop_data_position] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters