Skip to main content
Version: Next

IRequestLoader

An abstract interface defining a read-only stream of requests to crawl.

Request loaders are used to manage and provide access to a storage of crawling requests.

Key responsibilities:

  • Fetching the next request to be processed.
  • Marking requests as handled once they are no longer in progress.
  • Managing state information such as the total and handled request counts.

Request lifecycle contract

Every request returned by IRequestLoader.fetchNextRequest is considered in progress until it is passed to IRequestLoader.markRequestAsHandled. Once you fetch a request, you are obligated to eventually mark it as handled — there is no way to hand a request back to a loader (only an IRequestManager can reclaim requests for a retry). "Handled" therefore means "finished with this request", whether processing succeeded or was abandoned after exhausting retries.

Honoring this contract matters for three reasons:

Concrete implementations such as RequestList or SitemapRequestLoader build on this interface. The IRequestManager interface extends it with the capability to enqueue and reclaim requests.

Hierarchy

Implemented by

Index

Methods

[asyncIterator]

  • Can be used to iterate over the loader instance in a for await .. of loop. Provides an alternative for the repeated use of fetchNextRequest.


    Returns AsyncGenerator<CrawleeRequest<Dictionary>, any, any>

checkReadiness

  • Reports whether the loader has a request to hand over, is waiting on one, or is done — see RequestSourceStatus.

    A consumer's task loop is gated on this, so implementations MUST answer ready before evaluating anything else. finished may arrive late behind distributed storage, but it is never wrong.


    Returns Promise<RequestSourceStatus>

fetchNextRequest

  • Gets the next Request to process, or null if there are no more pending requests.

    The returned request is marked as in progress and remains so until it is passed to IRequestLoader.markRequestAsHandled. The caller is responsible for eventually marking every fetched request as handled; otherwise the loader never considers itself finished and the request may be re-served after a restart. See the request lifecycle contract on IRequestLoader.


    Returns Promise<null | CrawleeRequest<T>>

getHandledCount

  • getHandledCount(): Promise<number>
  • Returns the number of requests in the loader that have been handled.


    Returns Promise<number>

getPendingCount

  • getPendingCount(): Promise<number>
  • Returns an approximation of the number of pending requests in the loader.


    Returns Promise<number>

getTotalCount

  • getTotalCount(): Promise<number>
  • Returns an approximation of the total number of requests in the loader (i.e. pending + handled).


    Returns Promise<number>

markRequestAsHandled

  • Marks a request previously returned by IRequestLoader.fetchNextRequest as handled, removing it from the set of in-progress requests.

    Call this once you are done with the request — whether processing succeeded or was abandoned after exhausting retries. Because a loader cannot take a request back, marking it handled is the only way to signal completion; failing to do so prevents IRequestLoader.checkReadiness from ever reporting finished and skews the handled and pending counts. See the request lifecycle contract on IRequestLoader.


    Parameters

    Returns Promise<null | void | RequestQueueOperationInfo>

optionalpersistState

  • persistState(): Promise<void>
  • Persists the current state of the loader into the default KeyValueStore.

    Not all loaders support persistence; implementations that do not should leave this undefined.


    Returns Promise<void>

optionaltoTandem

  • Combines the loader with a request manager to support adding and reclaiming requests.


    Parameters

    Returns Promise<IRequestManager>