@@ -303,23 +303,24 @@ async def add_batch_of_requests(
303
303
processed_requests = list [ProcessedRequest ]()
304
304
unprocessed_requests = list [UnprocessedRequest ]()
305
305
306
+ # Prepare a dictionary to track existing requests by their unique keys.
307
+ existing_unique_keys : dict [str , Path ] = {}
308
+ existing_request_files = await self ._get_request_files (self .path_to_rq )
309
+
310
+ for request_file in existing_request_files :
311
+ existing_request = await self ._parse_request_file (request_file )
312
+ if existing_request is not None :
313
+ existing_unique_keys [existing_request .unique_key ] = request_file
314
+
315
+ # Process each request in the batch.
306
316
for request in requests :
307
- existing_request_files = await self . _get_request_files ( self . path_to_rq )
317
+ existing_request_file = existing_unique_keys . get ( request . unique_key )
308
318
existing_request = None
309
319
310
- # Go through existing requests to find if the request already exists in the queue.
311
- for existing_request_file in existing_request_files :
320
+ # Only load the full request from disk if we found a duplicate
321
+ if existing_request_file is not None :
312
322
existing_request = await self ._parse_request_file (existing_request_file )
313
323
314
- if existing_request is None :
315
- continue
316
-
317
- # If the unique key matches, we found an existing request
318
- if existing_request .unique_key == request .unique_key :
319
- break
320
-
321
- existing_request = None
322
-
323
324
# If there is no existing request with the same unique key, add the new request.
324
325
if existing_request is None :
325
326
request_path = self ._get_request_path (request .id )
@@ -343,6 +344,9 @@ async def add_batch_of_requests(
343
344
new_total_request_count += 1
344
345
new_pending_request_count += 1
345
346
347
+ # Add to our index for subsequent requests in this batch
348
+ existing_unique_keys [request .unique_key ] = self ._get_request_path (request .id )
349
+
346
350
processed_requests .append (
347
351
ProcessedRequest (
348
352
id = request .id ,
@@ -352,7 +356,7 @@ async def add_batch_of_requests(
352
356
)
353
357
)
354
358
355
- # If the request already exists, we need to update it.
359
+ # If the request already exists in the RQ, just update it if needed .
356
360
else :
357
361
# Set the processed request flags.
358
362
was_already_present = existing_request is not None
@@ -371,10 +375,10 @@ async def add_batch_of_requests(
371
375
372
376
# If the request is already in the RQ but not handled yet, update it.
373
377
elif was_already_present and not was_already_handled :
374
- request_path = self ._get_request_path (request .id )
378
+ request_path = self ._get_request_path (existing_request .id )
375
379
request_dict = existing_request .model_dump ()
376
380
request_dict ['__forefront' ] = forefront
377
- request_data = await json_dumps (existing_request . model_dump () )
381
+ request_data = await json_dumps (request_dict )
378
382
await atomic_write (request_path , request_data )
379
383
380
384
processed_requests .append (
0 commit comments