Skip to content

Commit 656a287

Browse files
committed
FIX: Removed binary from repo, fixed schema handling in async consumer
1 parent d4aab5c commit 656a287

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed
-5.92 MB
Binary file not shown.

pulsar/asyncio.py

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@
2323

2424
import asyncio
2525
import functools
26-
from typing import Any, Iterable, Optional, Union
26+
import logging
27+
from typing import Any, Iterable, Optional, Union, Tuple
2728

2829
import _pulsar
2930

3031
import pulsar
31-
from pulsar import _listener_wrapper
32+
from pulsar import Message, _listener_wrapper
3233

34+
from pulsar import schema
35+
_schema = schema
3336

3437
class PulsarException(BaseException):
3538
"""
@@ -163,15 +166,25 @@ def topic(self) -> str:
163166

164167

165168
class Consumer:
166-
def __init__(self, consumer: _pulsar.Consumer) -> None:
169+
# BUG: schema stuff doesn´t work at all because 90% of the methods are missing
170+
def __init__(self, consumer: _pulsar.Consumer):
167171
self._consumer: _pulsar.Consumer = consumer
168172

173+
def _prepare_logger(logger):
174+
import logging
175+
def log(level, message):
176+
old_threads = logging.logThreads
177+
logging.logThreads = False
178+
logger.log(logging.getLevelName(level), message)
179+
logging.logThreads = old_threads
180+
return log
181+
169182
async def acknowledge(self, msg: pulsar.Message) -> None:
170183
"""
171184
Acknowledge the reception of a single message.
172185
"""
173186
future = asyncio.get_running_loop().create_future()
174-
self._consumer.acknowledge_async(msg, functools.partial(_set_future, future))
187+
self._consumer.acknowledge_async(msg._message, functools.partial(_set_future, future))
175188
await future
176189

177190
async def acknowledge_cumulative(self, msg: pulsar.Message) -> None:
@@ -186,7 +199,7 @@ async def negative_acknowledge(self, msg: pulsar.Message) -> None:
186199
"""
187200
Acknowledge the failure to process a single message.
188201
"""
189-
self._consumer.negative_acknowledge(msg)
202+
self._consumer.negative_acknowledge(msg._message)
190203

191204
async def batch_receive(self) -> Iterable[pulsar.Message]:
192205
"""
@@ -203,7 +216,12 @@ async def receive(self) -> pulsar.Message:
203216
future = asyncio.get_running_loop().create_future()
204217

205218
self._consumer.receive_async(functools.partial(_set_future, future))
206-
return await future
219+
msg = await future
220+
221+
m = Message()
222+
m._message = msg
223+
m._schema = self._schema
224+
return m
207225

208226
async def close(self):
209227
"""
@@ -213,7 +231,7 @@ async def close(self):
213231
self._consumer.close_async(functools.partial(_set_future, future, value=None))
214232
await future
215233

216-
async def seek(self, position: tuple[int, int, int, int] | pulsar.MessageId):
234+
async def seek(self, position: Tuple[int, int, int, int] | pulsar.MessageId):
217235
"""
218236
Reset the subscription associated with this consumer to a specific message id or publish timestamp. The message id can either be a specific message or represent the first or last messages in the topic. ...
219237
"""
@@ -300,6 +318,7 @@ def __init__(self, service_url, **kwargs) -> None:
300318
"""
301319
assert service_url.startswith('pulsar://'), "The service url must start with 'pulsar://'"
302320
self._client = pulsar.Client(service_url, **kwargs)._client
321+
self._consumers = []
303322

304323
async def subscribe(self, topic, subscription_name,
305324
consumer_type: _pulsar.ConsumerType = _pulsar.ConsumerType.Exclusive,
@@ -325,10 +344,14 @@ async def subscribe(self, topic, subscription_name,
325344
batch_index_ack_enabled=False,
326345
regex_subscription_mode: _pulsar.RegexSubscriptionMode = _pulsar.RegexSubscriptionMode.PersistentOnly,
327346
dead_letter_policy: Union[None, pulsar.ConsumerDeadLetterPolicy] = None,) -> Consumer:
347+
print("subscribe called")
328348
conf = _pulsar.ConsumerConfiguration()
329349
conf.consumer_type(consumer_type)
330350
conf.regex_subscription_mode(regex_subscription_mode)
331351
conf.read_compacted(is_read_compacted)
352+
353+
print("core conf set")
354+
332355
if message_listener:
333356
conf.message_listener(_listener_wrapper(message_listener, schema))
334357
conf.receiver_queue_size(receiver_queue_size)
@@ -363,18 +386,36 @@ async def subscribe(self, topic, subscription_name,
363386
if dead_letter_policy:
364387
conf.dead_letter_policy(dead_letter_policy.policy())
365388

389+
print("opt conf set")
390+
366391
future = asyncio.get_running_loop().create_future()
367392

393+
print("future created")
394+
395+
c = Consumer(None)
368396
if isinstance(topic, str):
397+
print("single")
369398
self._client.subscribe_async(topic, subscription_name, conf, functools.partial(_set_future, future))
399+
c._consumer = await future
370400
elif isinstance(topic, list):
401+
print("multi")
371402
self._client.subscribe_topics_async(topic, subscription_name, conf, functools.partial(_set_future, future))
403+
c._consumer = await future
372404
elif isinstance(topic, pulsar._retype):
405+
print("regex")
373406
self._client.subscribe_pattern_async(topic, subscription_name, conf, functools.partial(_set_future, future))
407+
c._consumer = await future
374408
else:
375409
raise ValueError("Argument 'topic' is expected to be of a type between (str, list, re.pattern)")
376410

377-
return Consumer(await future)
411+
c._client = self
412+
c._schema = schema
413+
c._schema.attach_client(self._client)
414+
415+
print("consumer created")
416+
self._consumers.append(c)
417+
418+
return c
378419

379420
async def create_producer(self, topic,
380421
producer_name=None,

0 commit comments

Comments
 (0)