|
7 | 7 | from asyncio_signal_bus.injector import Injector |
8 | 8 | from asyncio_signal_bus.publisher import SignalPublisher |
9 | 9 | from asyncio_signal_bus.queue_getter import QueueGetter |
10 | | -from asyncio_signal_bus.subscriber import SignalSubscriber |
| 10 | +from asyncio_signal_bus.subscriber import BatchSignalSubscriber, SignalSubscriber |
11 | 11 | from asyncio_signal_bus.types import R, S |
12 | 12 |
|
13 | 13 | LOGGER = getLogger(__name__) |
@@ -89,7 +89,52 @@ def subscriber( |
89 | 89 | self._queues.get(topic_name).append(queue) |
90 | 90 |
|
91 | 91 | def _wrapper(f: Callable[[S], Awaitable[R]]) -> SignalSubscriber[S, R]: |
92 | | - s = SignalSubscriber(error_handler(f), queue) |
| 92 | + s = SignalSubscriber( |
| 93 | + error_handler(f), queue, shutdown_timeout=shutdown_timeout |
| 94 | + ) |
| 95 | + LOGGER.debug(f"Registering subscriber to topic {topic_name}") |
| 96 | + self._subscribers.append(s) |
| 97 | + return s |
| 98 | + |
| 99 | + return _wrapper |
| 100 | + |
| 101 | + def batch_subscriber( |
| 102 | + self, |
| 103 | + topic_name="default", |
| 104 | + error_handler: Type[SubscriberErrorHandler] = SubscriberErrorHandler[S, R], |
| 105 | + shutdown_timeout: Optional[SupportsFloat] = 120, |
| 106 | + max_items: int = 10, |
| 107 | + period_seconds: int = 10, |
| 108 | + ): |
| 109 | + """ |
| 110 | + A subscriber that consumes batches of events. The subscriber will wait no longer |
| 111 | + than the period_seconds between aggregations. Batches will not exceed batch |
| 112 | + seconds in size. |
| 113 | + :param topic_name: The name of the topic used to link one or more subscribers |
| 114 | + with one or more publishers. |
| 115 | + :param error_handler: An error handler used to handle errors within the callable. |
| 116 | + Error handling should usually terminate at the subscriber, with the |
| 117 | + subscriber catching all exceptions. Any unhandled errors will block the |
| 118 | + shutdown of the bus when the bus exits context or the stop method is used. |
| 119 | + :param shutdown_timeout: If the subscriber takes longer than this time during |
| 120 | + shutdown, then the task is killed and an error is raised. If you do not |
| 121 | + want the task timeout to be limited, then set this value to None. |
| 122 | + :param max_items: The maximum amount of time for the batch. |
| 123 | + :param period_seconds: The maximum amount of timem to wait between batches. |
| 124 | + :return: Wrapped callable |
| 125 | + """ |
| 126 | + self._queues.setdefault(topic_name, []) |
| 127 | + queue = Queue() |
| 128 | + self._queues.get(topic_name).append(queue) |
| 129 | + |
| 130 | + def _wrapper(f: Callable[[S], Awaitable[R]]) -> SignalSubscriber[S, R]: |
| 131 | + s = BatchSignalSubscriber( |
| 132 | + error_handler(f), |
| 133 | + queue, |
| 134 | + shutdown_timeout=shutdown_timeout, |
| 135 | + max_items=max_items, |
| 136 | + period_seconds=period_seconds, |
| 137 | + ) |
93 | 138 | LOGGER.debug(f"Registering subscriber to topic {topic_name}") |
94 | 139 | self._subscribers.append(s) |
95 | 140 | return s |
|
0 commit comments