Skip to content

Commit a19aeb5

Browse files
committed
fix: add min_id parameter to get_new_updates
1 parent 0c58a6c commit a19aeb5

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

openfoodfacts/redis.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def get_processed_since(
106106
def get_new_updates(
107107
redis_client: Redis,
108108
stream_name: str = "product_updates_off",
109+
min_id: Union[str, datetime.datetime] = "$",
109110
batch_size: int = 100,
110111
) -> Iterator[RedisUpdate]:
111112
"""Reads new updates from a Redis Stream, starting from the moment this
@@ -115,34 +116,41 @@ def get_new_updates(
115116
116117
:param redis_client: the Redis client
117118
:param stream_name: the name of the Redis stream to read from
119+
:param min_id: the minimum ID to start from, defaults to "$".
118120
:param batch_size: the size of the batch to fetch, defaults to 100
119121
:yield: a RedisUpdate instance for each update
120122
"""
121123
yield from get_new_updates_multistream(
122124
redis_client,
123125
[stream_name],
126+
min_id=min_id,
124127
batch_size=batch_size,
125128
)
126129

127130

128131
def get_new_updates_multistream(
129132
redis_client: Redis,
130133
stream_names: list[str],
134+
min_id: Union[str, datetime.datetime] = "$",
131135
batch_size: int = 100,
132136
) -> Iterator[RedisUpdate]:
133137
"""Reads new updates from Redis Stream, starting from the moment this
134138
function is called.
135139
136140
The function will block until new updates are available.
137141
138-
:param redis_client: the Redis client
139-
:param stream_names: the names of the Redis streams to read from
142+
:param redis_client: the Redis client.
143+
:param stream_names: the names of the Redis streams to read from.
144+
:param min_id: the minimum ID to start from, defaults to "$".
140145
:param batch_size: the size of the batch to fetch, defaults to 100.
141-
:yield: a RedisUpdate instance for each update
146+
:yield: a RedisUpdate instance for each update.
142147
"""
148+
if isinstance(min_id, datetime.datetime):
149+
min_id = f"{int(min_id.timestamp() * 1000)}-0"
150+
143151
# We start from the last ID
144152
min_ids: dict[Union[bytes, str, memoryview], Union[int, bytes, str, memoryview]] = {
145-
stream_name: "$" for stream_name in stream_names
153+
stream_name: min_id for stream_name in stream_names
146154
}
147155
while True:
148156
logger.debug(
@@ -155,8 +163,8 @@ def get_new_updates_multistream(
155163

156164
for stream_name, batch in response:
157165
# We update the min_id to the last ID of the batch
158-
min_id = batch[-1][0]
159-
min_ids[stream_name] = min_id
166+
new_min_id = batch[-1][0]
167+
min_ids[stream_name] = new_min_id
160168
for timestamp_id, item in batch:
161169
# Get the timestamp from the ID
162170
timestamp = int(timestamp_id.split("-")[0])

0 commit comments

Comments
 (0)