11import asyncio
22import pickle # noqa: S403
33from logging import getLogger
4- from typing import AsyncGenerator , Callable , List , Optional , Set , TypeVar , Union
4+ from typing import Any , AsyncGenerator , Callable , List , Optional , Set , TypeVar , Union
55
66from aiokafka import AIOKafkaConsumer , AIOKafkaProducer
77from kafka .admin import KafkaAdminClient , NewTopic
88from taskiq import AsyncResultBackend , BrokerMessage
99from taskiq .abc .broker import AsyncBroker
1010
1111from taskiq_aio_kafka .exceptions import WrongAioKafkaBrokerParametersError
12+ from taskiq_aio_kafka .models import KafkaConsumerParameters , KafkaProducerParameters
1213
1314_T = TypeVar ("_T" ) # noqa: WPS111
1415
@@ -86,9 +87,13 @@ def __init__( # noqa: WPS211
8687 replication_factor = 1 ,
8788 )
8889
89- self ._aiokafka_producer : Optional [AIOKafkaProducer ] = aiokafka_producer
90+ self ._aiokafka_producer_params : KafkaProducerParameters = (
91+ KafkaProducerParameters ()
92+ )
9093
91- self ._aiokafka_consumer : Optional [AIOKafkaConsumer ] = aiokafka_consumer
94+ self ._aiokafka_consumer_params : KafkaConsumerParameters = (
95+ KafkaConsumerParameters ()
96+ )
9297
9398 self ._kafka_admin_client : KafkaAdminClient = (
9499 kafka_admin_client
@@ -105,6 +110,30 @@ def __init__( # noqa: WPS211
105110 self ._is_producer_started = False
106111 self ._is_consumer_started = False
107112
113+ def configure_producer (self , ** producer_parameters : Any ) -> None :
114+ """Configure kafka producer.
115+
116+ You can pass here any configuration parameters
117+ accepted by the kafka producer.
118+
119+ :param producer_parameters: producer parameters kwargs.
120+ """
121+ self ._aiokafka_producer_params = KafkaProducerParameters (
122+ ** producer_parameters ,
123+ )
124+
125+ def configure_consumer (self , ** consumer_parameters : Any ) -> None :
126+ """Configure kafka consumer.
127+
128+ You can pass here any configuration parameters
129+ accepted by the kafka consumer.
130+
131+ :param consumer_parameters: consumer parameters kwargs.
132+ """
133+ self ._aiokafka_consumer_params = KafkaConsumerParameters (
134+ ** consumer_parameters ,
135+ )
136+
108137 async def startup (self ) -> None :
109138 """Setup AIOKafkaProducer, AIOKafkaConsumer and kafka topics.
110139
@@ -114,30 +143,28 @@ async def startup(self) -> None:
114143 if there are no producer and consumer passed.
115144 """
116145 await super ().startup ()
117-
118- is_topic_available : bool = bool (
119- self ._kafka_admin_client .describe_topics ([self ._kafka_topic .name ]),
146+ available_condition : bool = (
147+ self ._kafka_topic .name not in self ._kafka_admin_client .list_topics ()
120148 )
121- if not is_topic_available :
149+ if available_condition :
122150 self ._kafka_admin_client .create_topics (
123151 new_topics = [self ._kafka_topic ],
124152 validate_only = False ,
125153 )
154+ self ._aiokafka_producer = AIOKafkaProducer (
155+ bootstrap_servers = self ._bootstrap_servers ,
156+ loop = self ._loop ,
157+ ** self ._aiokafka_producer_params .dict (),
158+ )
159+ await self ._aiokafka_producer .start ()
126160
127- if not self ._aiokafka_producer :
128- self ._aiokafka_producer = AIOKafkaProducer (
161+ if self .is_worker_process :
162+ self ._aiokafka_consumer = AIOKafkaConsumer (
163+ self ._kafka_topic .name ,
129164 bootstrap_servers = self ._bootstrap_servers ,
130165 loop = self ._loop ,
166+ ** self ._aiokafka_consumer_params .dict (),
131167 )
132- await self ._aiokafka_producer .start ()
133-
134- if self .is_worker_process :
135- if not self ._aiokafka_consumer :
136- self ._aiokafka_consumer = AIOKafkaConsumer (
137- self ._kafka_topic .name ,
138- bootstrap_servers = self ._bootstrap_servers ,
139- loop = self ._loop ,
140- )
141168
142169 await self ._aiokafka_consumer .start ()
143170 self ._is_consumer_started = True
@@ -148,10 +175,10 @@ async def shutdown(self) -> None:
148175 """Close all connections on shutdown."""
149176 await super ().shutdown ()
150177
151- if self ._aiokafka_producer :
178+ if self ._is_producer_started :
152179 await self ._aiokafka_producer .stop ()
153180
154- if self ._aiokafka_consumer :
181+ if self ._is_consumer_started :
155182 await self ._aiokafka_consumer .stop ()
156183
157184 topic_delete_condition : bool = all (
0 commit comments