|
1 |
| -import pickle |
2 |
| -from abc import abstractmethod |
3 | 1 | from logging import getLogger
|
4 | 2 | from typing import Any, AsyncGenerator, Callable, Optional, TypeVar
|
5 | 3 |
|
@@ -52,66 +50,63 @@ async def shutdown(self) -> None:
|
52 | 50 | await super().shutdown()
|
53 | 51 | await self.connection_pool.disconnect()
|
54 | 52 |
|
55 |
| - async def listen(self) -> AsyncGenerator[BrokerMessage, None]: |
56 |
| - """ |
57 |
| - Listen redis queue for new messages. |
58 | 53 |
|
59 |
| - This function listens to the queue |
60 |
| - and yields new messages if they have BrokerMessage type. |
| 54 | +class PubSubBroker(BaseRedisBroker): |
| 55 | + """Broker that works with Redis and broadcasts tasks to all workers.""" |
61 | 56 |
|
62 |
| - :yields: broker messages. |
63 |
| - """ |
64 |
| - async for message in self._listen_to_raw_messages(): |
65 |
| - try: |
66 |
| - redis_message = pickle.loads(message) |
67 |
| - if isinstance(redis_message, BrokerMessage): |
68 |
| - yield redis_message |
69 |
| - except ( |
70 |
| - TypeError, |
71 |
| - AttributeError, |
72 |
| - pickle.UnpicklingError, |
73 |
| - ) as exc: |
74 |
| - logger.debug( |
75 |
| - "Cannot read broker message %s", |
76 |
| - exc, |
77 |
| - exc_info=True, |
78 |
| - ) |
79 |
| - |
80 |
| - @abstractmethod |
81 |
| - async def _listen_to_raw_messages(self) -> AsyncGenerator[bytes, None]: |
| 57 | + async def kick(self, message: BrokerMessage) -> None: |
82 | 58 | """
|
83 |
| - Generator for reading raw data from Redis. |
| 59 | + Publish message over PUBSUB channel. |
84 | 60 |
|
85 |
| - :yields: raw data. |
| 61 | + :param message: message to send. |
86 | 62 | """
|
87 |
| - yield # type: ignore |
88 |
| - |
| 63 | + async with Redis(connection_pool=self.connection_pool) as redis_conn: |
| 64 | + await redis_conn.publish(self.queue_name, message.message) |
89 | 65 |
|
90 |
| -class PubSubBroker(BaseRedisBroker): |
91 |
| - """Broker that works with Redis and broadcasts tasks to all workers.""" |
| 66 | + async def listen(self) -> AsyncGenerator[bytes, None]: |
| 67 | + """ |
| 68 | + Listen redis queue for new messages. |
92 | 69 |
|
93 |
| - async def kick(self, message: BrokerMessage) -> None: # noqa: D102 |
94 |
| - async with Redis(connection_pool=self.connection_pool) as redis_conn: |
95 |
| - await redis_conn.publish(self.queue_name, pickle.dumps(message)) |
| 70 | + This function listens to the pubsub channel |
| 71 | + and yields all messages with proper types. |
96 | 72 |
|
97 |
| - async def _listen_to_raw_messages(self) -> AsyncGenerator[bytes, None]: |
| 73 | + :yields: broker messages. |
| 74 | + """ |
98 | 75 | async with Redis(connection_pool=self.connection_pool) as redis_conn:
|
99 | 76 | redis_pubsub_channel = redis_conn.pubsub()
|
100 | 77 | await redis_pubsub_channel.subscribe(self.queue_name)
|
101 | 78 | async for message in redis_pubsub_channel.listen():
|
102 | 79 | if not message:
|
103 | 80 | continue
|
| 81 | + if message["type"] != "message": |
| 82 | + logger.debug("Received non-message from redis: %s", message) |
| 83 | + continue |
104 | 84 | yield message["data"]
|
105 | 85 |
|
106 | 86 |
|
107 | 87 | class ListQueueBroker(BaseRedisBroker):
|
108 | 88 | """Broker that works with Redis and distributes tasks between workers."""
|
109 | 89 |
|
110 |
| - async def kick(self, message: BrokerMessage) -> None: # noqa: D102 |
| 90 | + async def kick(self, message: BrokerMessage) -> None: |
| 91 | + """ |
| 92 | + Put a message in a list. |
| 93 | +
|
| 94 | + This method appends a message to the list of all messages. |
| 95 | +
|
| 96 | + :param message: message to append. |
| 97 | + """ |
111 | 98 | async with Redis(connection_pool=self.connection_pool) as redis_conn:
|
112 |
| - await redis_conn.lpush(self.queue_name, pickle.dumps(message)) |
| 99 | + await redis_conn.lpush(self.queue_name, message.message) |
113 | 100 |
|
114 |
| - async def _listen_to_raw_messages(self) -> AsyncGenerator[bytes, None]: |
| 101 | + async def listen(self) -> AsyncGenerator[bytes, None]: |
| 102 | + """ |
| 103 | + Listen redis queue for new messages. |
| 104 | +
|
| 105 | + This function listens to the queue |
| 106 | + and yields new messages if they have BrokerMessage type. |
| 107 | +
|
| 108 | + :yields: broker messages. |
| 109 | + """ |
115 | 110 | redis_brpop_data_position = 1
|
116 | 111 | async with Redis(connection_pool=self.connection_pool) as redis_conn:
|
117 | 112 | while True: # noqa: WPS457
|
|
0 commit comments