Repository navigation
Expand file tree
/
Copy path_request_queue_client.py
More file actions
846 lines (686 loc) · 34.2 KB
/
Copy path_request_queue_client.py
File metadata and controls
846 lines (686 loc) · 34.2 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
from __future__ import annotations
import asyncio
import json
import shutil
from collections import deque
from datetime import datetime, timezone
from hashlib import sha256
from logging import getLogger
from pathlib import Path
from typing import TYPE_CHECKING
from pydantic import BaseModel, ValidationError
from typing_extensions import Self, override
from crawlee import Request
from crawlee._consts import METADATA_FILENAME
from crawlee._utils.crypto import crypto_random_object_id
from crawlee._utils.file import atomic_write, json_dumps, validate_subdirectory
from crawlee._utils.raise_if_too_many_kwargs import raise_if_too_many_kwargs
from crawlee._utils.recoverable_state import RecoverableState
from crawlee.storage_clients._base import RequestQueueClient
from crawlee.storage_clients.models import (
AddRequestsResponse,
ProcessedRequest,
RequestQueueMetadata,
UnprocessedRequest,
)
if TYPE_CHECKING:
from collections.abc import Sequence
from crawlee.configuration import Configuration
from crawlee.storages import KeyValueStore
logger = getLogger(__name__)
class RequestQueueState(BaseModel):
"""State model for the `FileSystemRequestQueueClient`."""
sequence_counter: int = 0
"""Counter for regular request ordering."""
forefront_sequence_counter: int = 0
"""Counter for forefront request ordering."""
forefront_requests: dict[str, int] = {}
"""Mapping of forefront request unique keys to their sequence numbers."""
regular_requests: dict[str, int] = {}
"""Mapping of regular request unique keys to their sequence numbers."""
in_progress_requests: set[str] = set()
"""Set of request unique keys currently being processed."""
handled_requests: set[str] = set()
"""Set of request unique keys that have been handled."""
class FileSystemRequestQueueClient(RequestQueueClient):
"""A file system implementation of the request queue client.
This client persists requests to the file system as individual JSON files, making it suitable for scenarios
where data needs to survive process restarts. Each request is stored as a separate file in a directory
structure following the pattern:
```
{STORAGE_DIR}/request_queues/{QUEUE_ID}/{REQUEST_ID}.json
```
The implementation uses `RecoverableState` to maintain ordering information, in-progress status, and
request handling status. This allows for proper state recovery across process restarts without
embedding metadata in individual request files. File system storage provides durability at the cost of
slower I/O operations compared to memory only-based storage.
This implementation is ideal for long-running crawlers where persistence is important and for situations
where you need to resume crawling after process termination.
"""
_STORAGE_SUBDIR = 'request_queues'
"""The name of the subdirectory where request queues are stored."""
_STORAGE_SUBSUBDIR_DEFAULT = 'default'
"""The name of the subdirectory for the default request queue."""
_MAX_REQUESTS_IN_CACHE = 100_000
"""Maximum number of requests to keep in cache for faster access."""
def __init__(
self,
*,
metadata: RequestQueueMetadata,
path_to_rq: Path,
lock: asyncio.Lock,
recoverable_state: RecoverableState[RequestQueueState],
) -> None:
"""Initialize a new instance.
Preferably use the `FileSystemRequestQueueClient.open` class method to create a new instance.
"""
self._metadata = metadata
self._path_to_rq = path_to_rq
"""The full path to the request queue directory."""
self._lock = lock
"""A lock to ensure that only one operation is performed at a time."""
self._request_cache = deque[Request]()
"""Cache for requests: forefront requests at the beginning, regular requests at the end."""
self._request_cache_needs_refresh = True
"""Flag indicating whether the cache needs to be refreshed from filesystem."""
self._is_empty_cache: bool | None = None
"""Cache for is_empty result: None means unknown, True/False is cached state."""
self._state = recoverable_state
"""Recoverable state to maintain request ordering, in-progress status, and handled status."""
@override
async def get_metadata(self) -> RequestQueueMetadata:
return self._metadata
@property
def path_to_rq(self) -> Path:
"""The full path to the request queue directory."""
return self._path_to_rq
@property
def path_to_metadata(self) -> Path:
"""The full path to the request queue metadata file."""
return self.path_to_rq / METADATA_FILENAME
@classmethod
async def _create_recoverable_state(cls, id: str, configuration: Configuration) -> RecoverableState:
async def kvs_factory() -> KeyValueStore:
from crawlee.storage_clients import FileSystemStorageClient # noqa: PLC0415 avoid circular import
from crawlee.storages import KeyValueStore # noqa: PLC0415 avoid circular import
return await KeyValueStore.open(storage_client=FileSystemStorageClient(), configuration=configuration)
return RecoverableState[RequestQueueState](
default_state=RequestQueueState(),
persist_state_key=f'__RQ_STATE_{id}',
persist_state_kvs_factory=kvs_factory,
persistence_enabled=True,
logger=logger,
)
@classmethod
async def open(
cls,
*,
id: str | None,
name: str | None,
alias: str | None,
configuration: Configuration,
) -> Self:
"""Open or create a file system request queue client.
This method attempts to open an existing request queue from the file system. If a queue with the specified
ID or name exists, it loads the metadata and state from the stored files. If no existing queue is found,
a new one is created.
Args:
id: The ID of the request queue to open. If provided, searches for existing queue by ID.
name: The name of the request queue for named (global scope) storages.
alias: The alias of the request queue for unnamed (run scope) storages.
configuration: The configuration object containing storage directory settings.
Returns:
An instance for the opened or created storage client.
Raises:
ValueError: If a queue with the specified ID is not found, if metadata is invalid,
or if both name and alias are provided.
"""
# Validate input parameters.
raise_if_too_many_kwargs(id=id, name=name, alias=alias)
rq_base_path = Path(configuration.storage_dir) / cls._STORAGE_SUBDIR
if not rq_base_path.exists():
await asyncio.to_thread(rq_base_path.mkdir, parents=True, exist_ok=True)
# Open an existing RQ by its ID, raise an error if not found.
if id:
found = False
for rq_dir in rq_base_path.iterdir():
if not rq_dir.is_dir():
continue
path_to_metadata = rq_dir / METADATA_FILENAME
if not path_to_metadata.exists():
continue
try:
file = await asyncio.to_thread(path_to_metadata.open, mode='r', encoding='utf-8')
try:
file_content = json.load(file)
metadata = RequestQueueMetadata(**file_content)
if metadata.id == id:
client = cls(
metadata=metadata,
path_to_rq=rq_base_path / rq_dir,
lock=asyncio.Lock(),
recoverable_state=await cls._create_recoverable_state(
id=id, configuration=configuration
),
)
await client._state.initialize()
await client._discover_existing_requests()
await client._update_metadata(update_accessed_at=True)
found = True
break
finally:
await asyncio.to_thread(file.close)
except (json.JSONDecodeError, ValidationError):
continue
if not found:
raise ValueError(f'Request queue with ID "{id}" not found')
# Open an existing RQ by its name or alias, or create a new one if not found.
else:
path_to_rq = validate_subdirectory(rq_base_path, name or alias or 'default')
path_to_metadata = path_to_rq / METADATA_FILENAME
# If the RQ directory exists, reconstruct the client from the metadata file.
if path_to_rq.exists() and path_to_metadata.exists():
file = await asyncio.to_thread(path_to_metadata.open, encoding='utf-8')
try:
file_content = json.load(file)
finally:
await asyncio.to_thread(file.close)
try:
metadata = RequestQueueMetadata(**file_content)
except ValidationError as exc:
raise ValueError(f'Invalid metadata file for request queue "{name or alias}"') from exc
client = cls(
metadata=metadata,
path_to_rq=path_to_rq,
lock=asyncio.Lock(),
recoverable_state=await cls._create_recoverable_state(id=metadata.id, configuration=configuration),
)
await client._state.initialize()
await client._discover_existing_requests()
await client._update_metadata(update_accessed_at=True)
# Otherwise, create a new dataset client.
else:
now = datetime.now(timezone.utc)
metadata = RequestQueueMetadata(
id=crypto_random_object_id(),
name=name,
created_at=now,
accessed_at=now,
modified_at=now,
had_multiple_clients=False,
handled_request_count=0,
pending_request_count=0,
total_request_count=0,
)
client = cls(
metadata=metadata,
path_to_rq=path_to_rq,
lock=asyncio.Lock(),
recoverable_state=await cls._create_recoverable_state(id=metadata.id, configuration=configuration),
)
await client._state.initialize()
await client._update_metadata()
return client
@override
async def drop(self) -> None:
async with self._lock:
# Remove the RQ dir recursively if it exists.
if self.path_to_rq.exists():
await asyncio.to_thread(shutil.rmtree, self.path_to_rq)
# Clear recoverable state
await self._state.reset()
await self._state.teardown()
self._request_cache.clear()
self._request_cache_needs_refresh = True
# Invalidate is_empty cache.
self._is_empty_cache = None
@override
async def purge(self) -> None:
async with self._lock:
request_files = await self._get_request_files(self.path_to_rq)
for file_path in request_files:
await asyncio.to_thread(file_path.unlink, missing_ok=True)
# Clear recoverable state
await self._state.reset()
self._request_cache.clear()
self._request_cache_needs_refresh = True
await self._update_metadata(
update_modified_at=True,
update_accessed_at=True,
new_pending_request_count=0,
new_handled_request_count=0,
new_total_request_count=0,
)
# Invalidate is_empty cache.
self._is_empty_cache = None
@override
async def add_batch_of_requests(
self,
requests: Sequence[Request],
*,
forefront: bool = False,
) -> AddRequestsResponse:
async with self._lock:
self._is_empty_cache = None
new_total_request_count = self._metadata.total_request_count
new_pending_request_count = self._metadata.pending_request_count
processed_requests = list[ProcessedRequest]()
unprocessed_requests = list[UnprocessedRequest]()
state = self._state.current_value
def is_in_queue(unique_key: str) -> bool:
return unique_key in state.forefront_requests or unique_key in state.regular_requests
requests_to_enqueue = {}
# Determine which requests can be added or are modified.
for request in requests:
# Check if the request has already been handled.
if request.unique_key in state.handled_requests:
processed_requests.append(
ProcessedRequest(
unique_key=request.unique_key,
was_already_present=True,
was_already_handled=True,
)
)
# Check if the request is already in progress.
# Or if the request is already in the queue and the `forefront` flag is not used, we do not change the
# position of the request.
elif (request.unique_key in state.in_progress_requests) or (
not forefront and is_in_queue(request.unique_key)
):
processed_requests.append(
ProcessedRequest(
unique_key=request.unique_key,
was_already_present=True,
was_already_handled=False,
)
)
# These requests must either be added or update their position.
else:
requests_to_enqueue[request.unique_key] = request
# Process each request in the batch.
for request in requests_to_enqueue.values():
# If the request is not already in the RQ, this is a new request.
if not is_in_queue(request.unique_key):
request_path = self._get_request_path(request.unique_key)
# Add sequence number to ensure FIFO ordering using state.
if forefront:
sequence_number = state.forefront_sequence_counter
state.forefront_sequence_counter += 1
state.forefront_requests[request.unique_key] = sequence_number
else:
sequence_number = state.sequence_counter
state.sequence_counter += 1
state.regular_requests[request.unique_key] = sequence_number
# Save the clean request without extra fields
request_data = await json_dumps(request.model_dump())
await atomic_write(request_path, request_data)
# A new forefront request belongs to the very front of the queue, so it can go straight
# to the cache without a full refresh, unless the cache is at capacity or a refresh is
# already pending. New regular requests must not be appended to the cache: it may be
# truncated, so its tail is not necessarily the end of the queue; they are picked up by a
# refresh once the cache drains.
if forefront and not self._request_cache_needs_refresh:
if len(self._request_cache) < self._MAX_REQUESTS_IN_CACHE:
self._request_cache.appendleft(request)
else:
self._request_cache_needs_refresh = True
# Update the metadata counts.
new_total_request_count += 1
new_pending_request_count += 1
processed_requests.append(
ProcessedRequest(
unique_key=request.unique_key,
was_already_present=False,
was_already_handled=False,
)
)
# If the request already exists in the RQ and use the forefront flag to update its position
elif forefront:
# If the request is among `regular`, remove it from its current position.
if request.unique_key in state.regular_requests:
state.regular_requests.pop(request.unique_key)
# If the request is already in `forefront`, we just need to update its position.
state.forefront_requests[request.unique_key] = state.forefront_sequence_counter
state.forefront_sequence_counter += 1
# The request may already sit elsewhere in the cache, so its position must be recomputed.
self._request_cache_needs_refresh = True
processed_requests.append(
ProcessedRequest(
unique_key=request.unique_key,
was_already_present=True,
was_already_handled=False,
)
)
else:
logger.warning(f'Request with unique key "{request.unique_key}" could not be processed.')
unprocessed_requests.append(
UnprocessedRequest(
unique_key=request.unique_key,
url=request.url,
method=request.method,
)
)
await self._update_metadata(
update_modified_at=True,
update_accessed_at=True,
new_total_request_count=new_total_request_count,
new_pending_request_count=new_pending_request_count,
)
# Invalidate is_empty cache.
self._is_empty_cache = None
return AddRequestsResponse(
processed_requests=processed_requests,
unprocessed_requests=unprocessed_requests,
)
@override
async def get_request(self, unique_key: str) -> Request | None:
async with self._lock:
request_path = self._get_request_path(unique_key)
request = await self._parse_request_file(request_path)
if request is None:
logger.warning(f'Request with unique key "{unique_key}" not found in the queue.')
return None
await self._update_metadata(update_accessed_at=True)
return request
@override
async def fetch_next_request(self) -> Request | None:
async with self._lock:
# Refresh cache if needed or if it's empty.
if self._request_cache_needs_refresh or not self._request_cache:
await self._refresh_cache()
next_request: Request | None = None
state = self._state.current_value
# Fetch from the front of the deque (forefront requests are at the beginning).
while self._request_cache and next_request is None:
candidate = self._request_cache.popleft()
# Skip requests that are already in progress, however this should not happen.
if candidate.unique_key not in state.in_progress_requests:
next_request = candidate
if next_request is not None:
state.in_progress_requests.add(next_request.unique_key)
# `in_progress_requests` is updated, so we need to invalidate the `is_empty` cache.
self._is_empty_cache = None
return next_request
@override
async def mark_request_as_handled(self, request: Request) -> ProcessedRequest | None:
async with self._lock:
self._is_empty_cache = None
state = self._state.current_value
# Check if the request is in progress.
if request.unique_key not in state.in_progress_requests:
logger.warning(f'Marking request {request.unique_key} as handled that is not in progress.')
return None
# Update the request's handled_at timestamp.
if request.handled_at is None:
request.handled_at = datetime.now(timezone.utc)
# Dump the updated request to the file.
request_path = self._get_request_path(request.unique_key)
if not await asyncio.to_thread(request_path.exists):
logger.warning(f'Request file for {request.unique_key} does not exist, cannot mark as handled.')
return None
request_data = await json_dumps(request.model_dump())
await atomic_write(request_path, request_data)
# Update state: remove from in-progress and pending, and add to handled. Dropping the key from
# the pending mappings keeps them (and the persisted state) sized by the backlog rather than by
# every request ever processed.
state.in_progress_requests.discard(request.unique_key)
state.forefront_requests.pop(request.unique_key, None)
state.regular_requests.pop(request.unique_key, None)
state.handled_requests.add(request.unique_key)
# Update RQ metadata.
await self._update_metadata(
update_modified_at=True,
update_accessed_at=True,
new_handled_request_count=self._metadata.handled_request_count + 1,
new_pending_request_count=self._metadata.pending_request_count - 1,
)
return ProcessedRequest(
unique_key=request.unique_key,
was_already_present=True,
was_already_handled=True,
)
@override
async def reclaim_request(
self,
request: Request,
*,
forefront: bool = False,
) -> ProcessedRequest | None:
async with self._lock:
self._is_empty_cache = None
state = self._state.current_value
# Check if the request is in progress.
if request.unique_key not in state.in_progress_requests:
logger.info(f'Reclaiming request {request.unique_key} that is not in progress.')
return None
request_path = self._get_request_path(request.unique_key)
if not await asyncio.to_thread(request_path.exists):
logger.warning(f'Request file for {request.unique_key} does not exist, cannot reclaim.')
return None
# Update sequence number and state to ensure proper ordering.
if forefront:
# Remove from regular requests if it was there
state.regular_requests.pop(request.unique_key, None)
sequence_number = state.forefront_sequence_counter
state.forefront_sequence_counter += 1
state.forefront_requests[request.unique_key] = sequence_number
else:
# Remove from forefront requests if it was there
state.forefront_requests.pop(request.unique_key, None)
sequence_number = state.sequence_counter
state.sequence_counter += 1
state.regular_requests[request.unique_key] = sequence_number
# Save the clean request without extra fields
request_data = await json_dumps(request.model_dump())
await atomic_write(request_path, request_data)
# Remove from in-progress.
state.in_progress_requests.discard(request.unique_key)
# Update RQ metadata.
await self._update_metadata(
update_modified_at=True,
update_accessed_at=True,
)
# Add the request back to the cache.
if forefront:
self._request_cache.appendleft(request)
else:
self._request_cache.append(request)
return ProcessedRequest(
unique_key=request.unique_key,
was_already_present=True,
was_already_handled=False,
)
@override
async def is_empty(self) -> bool:
async with self._lock:
# If we have a cached value, return it immediately.
if self._is_empty_cache is not None:
return self._is_empty_cache
state = self._state.current_value
# If we have a cached requests, check them first (fast path).
if self._request_cache:
for req in self._request_cache:
if req.unique_key not in state.handled_requests:
self._is_empty_cache = False
return False
self._is_empty_cache = True
return True
# Fallback: check state for unhandled requests.
await self._update_metadata(update_accessed_at=True)
# Check pending requests in state.
queue_requests = (
set(state.forefront_requests.keys()) | set(state.regular_requests.keys())
) - state.in_progress_requests
pending_requests = queue_requests - state.handled_requests
if pending_requests:
self._is_empty_cache = False
return False
self._is_empty_cache = True
return True
@override
async def is_finished(self) -> bool:
# If there are requests available to fetch, the queue is not finished.
if not await self.is_empty():
return False
async with self._lock:
# Check, if cache changed while waiting for the lock.
if self._is_empty_cache is not True:
return False
# If there are any in-progress requests, the queue is not finished.
return not self._state.current_value.in_progress_requests
def _get_request_path(self, unique_key: str) -> Path:
"""Get the path to a specific request file.
Args:
unique_key: Unique key of the request.
Returns:
The path to the request file.
"""
return self.path_to_rq / f'{self._get_file_base_name_from_unique_key(unique_key)}.json'
async def _update_metadata(
self,
*,
new_handled_request_count: int | None = None,
new_pending_request_count: int | None = None,
new_total_request_count: int | None = None,
update_had_multiple_clients: bool = False,
update_accessed_at: bool = False,
update_modified_at: bool = False,
) -> None:
"""Update the dataset metadata file with current information.
Args:
new_handled_request_count: If provided, update the handled_request_count to this value.
new_pending_request_count: If provided, update the pending_request_count to this value.
new_total_request_count: If provided, update the total_request_count to this value.
update_had_multiple_clients: If True, set had_multiple_clients to True.
update_accessed_at: If True, update the `accessed_at` timestamp to the current time.
update_modified_at: If True, update the `modified_at` timestamp to the current time.
"""
# Always create a new timestamp to ensure it's truly updated
now = datetime.now(timezone.utc)
# Update timestamps according to parameters
if update_accessed_at:
self._metadata.accessed_at = now
if update_modified_at:
self._metadata.modified_at = now
# Update request counts if provided
if new_handled_request_count is not None:
self._metadata.handled_request_count = new_handled_request_count
if new_pending_request_count is not None:
self._metadata.pending_request_count = new_pending_request_count
if new_total_request_count is not None:
self._metadata.total_request_count = new_total_request_count
if update_had_multiple_clients:
self._metadata.had_multiple_clients = True
# Ensure the parent directory for the metadata file exists.
await asyncio.to_thread(self.path_to_metadata.parent.mkdir, parents=True, exist_ok=True)
# Dump the serialized metadata to the file.
data = await json_dumps(self._metadata.model_dump())
await atomic_write(self.path_to_metadata, data)
async def _refresh_cache(self) -> None:
"""Refresh the request cache from the filesystem.
This method loads up to `_MAX_REQUESTS_IN_CACHE` requests from the filesystem, prioritizing forefront
requests and maintaining proper ordering. Only requests that are pending according to the state are
read, so files of already handled or in-progress requests are not touched at all.
"""
self._request_cache.clear()
state = self._state.current_value
def is_pending(unique_key: str) -> bool:
return unique_key not in state.handled_requests and unique_key not in state.in_progress_requests
# Forefront requests are fetched newest first (LIFO), regular requests oldest first (FIFO).
forefront_keys = sorted(
filter(is_pending, state.forefront_requests),
key=lambda unique_key: state.forefront_requests[unique_key],
reverse=True,
)
regular_keys = sorted(
filter(is_pending, state.regular_requests),
key=lambda unique_key: state.regular_requests[unique_key],
)
for unique_key in forefront_keys + regular_keys:
if len(self._request_cache) >= self._MAX_REQUESTS_IN_CACHE:
break
request = await self._parse_request_file(self._get_request_path(unique_key))
if request is None:
logger.warning(f'Request file for "{unique_key}" is missing or invalid, skipping.')
continue
self._request_cache.append(request)
self._request_cache_needs_refresh = False
@classmethod
async def _get_request_files(cls, path_to_rq: Path) -> list[Path]:
"""Get all request files from the RQ.
Args:
path_to_rq: The path to the request queue directory.
Returns:
A list of paths to all request files.
"""
# Create the requests directory if it doesn't exist.
await asyncio.to_thread(path_to_rq.mkdir, parents=True, exist_ok=True)
# List all the json files.
files = list(await asyncio.to_thread(path_to_rq.glob, '*.json'))
# Filter out metadata file and non-file entries.
filtered = filter(lambda request_file: request_file.is_file() and request_file.name != METADATA_FILENAME, files)
return list(filtered)
@classmethod
async def _parse_request_file(cls, file_path: Path) -> Request | None:
"""Parse a request file and return the `Request` object.
Args:
file_path: The path to the request file.
Returns:
The parsed `Request` object or `None` if the file could not be read or parsed.
"""
# Read the request file in a thread to avoid blocking the event loop.
try:
file_content = await asyncio.to_thread(file_path.read_text, encoding='utf-8')
except FileNotFoundError:
logger.warning(f'Request file "{file_path}" not found.')
return None
# Parse the file content as JSON.
try:
parsed = json.loads(file_content)
except json.JSONDecodeError as exc:
logger.warning(f'Failed to parse request file {file_path}: {exc!s}')
return None
# Validate the content against the Request model.
try:
return Request.model_validate(parsed)
except ValidationError as exc:
logger.warning(f'Failed to validate request file {file_path}: {exc!s}')
return None
async def _discover_existing_requests(self) -> None:
"""Discover and load existing requests into the state when opening an existing request queue.
On recovery after a crash, any requests that were previously in-progress are reclaimed as pending,
since there is no active processing after a restart.
"""
request_files = await self._get_request_files(self.path_to_rq)
state = self._state.current_value
if state.in_progress_requests:
logger.info(
f'Reclaiming {len(state.in_progress_requests)} in-progress request(s) from previous run.',
)
state.in_progress_requests.clear()
for request_file in request_files:
request = await self._parse_request_file(request_file)
if request is None:
continue
# Already handled requests are tracked only for deduplication, not as pending work.
if request.handled_at is not None:
state.handled_requests.add(request.unique_key)
# Add pending request to state as regular request (assign sequence numbers)
elif (
request.unique_key not in state.regular_requests and request.unique_key not in state.forefront_requests
):
state.regular_requests[request.unique_key] = state.sequence_counter
state.sequence_counter += 1
@staticmethod
def _get_file_base_name_from_unique_key(unique_key: str) -> str:
"""Generate a deterministic file name for a unique_key.
Args:
unique_key: Unique key to be used to generate filename.
Returns:
A file name based on the unique_key.
"""
# hexdigest produces filenames compliant strings
hashed_key = sha256(unique_key.encode('utf-8')).hexdigest()
name_length = 15
# Truncate the key to the desired length
return hashed_key[:name_length]