-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream_consumer_offset.py
60 lines (48 loc) · 1.68 KB
/
stream_consumer_offset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import asyncio
import signal
from rstream import (
AMQPMessage,
Consumer,
ConsumerOffsetSpecification,
MessageContext,
OffsetType,
amqp_decoder,
)
STREAM = "my-test-stream"
START_OFFSET = 10 # Offset to start consuming messages
MESSAGE_LIMIT = 50 # Limit the number of messages to consume
async def consume():
consumer = Consumer(
host="localhost",
port=5552,
vhost="/",
username="vlad",
password="vlad",
)
consumed_count = 0 # Track the number of messages consumed
# Handle message consumption
async def on_message(msg: AMQPMessage, message_context: MessageContext):
nonlocal consumed_count
# Increment consumed count and process the message
consumed_count += 1
stream = message_context.consumer.get_stream(message_context.subscriber_name)
offset = message_context.offset
print(f"Got message: {msg.body} from stream {stream}, offset {offset}")
# Stop after reaching the message limit
if consumed_count >= MESSAGE_LIMIT:
print(f"Consumed {MESSAGE_LIMIT} messages, stopping consumer.")
await consumer.close()
await consumer.start()
# Use OffsetType.OFFSET to start from a specific offset
await consumer.subscribe(
stream=STREAM,
callback=on_message,
decoder=amqp_decoder,
offset_specification=ConsumerOffsetSpecification(OffsetType.OFFSET, START_OFFSET),
)
try:
await consumer.run()
except KeyboardInterrupt:
print("Shutting down consumer...")
await consumer.close()
asyncio.run(consume())