Skip to main content

CrawlerEngine

Struct CrawlerEngine 

Source
pub struct CrawlerEngine<'a> {
    pub paused: bool,
    /* private fields */
}
Expand description

The crawler engine – orchestrates the entire crawl loop.

CrawlerEngine is the runtime counterpart to the Spider trait. It owns all the infrastructure (scheduler, sessions, cache, checkpoint, robots.txt) and drives the fetch-parse-enqueue cycle. Create one with new, then call crawl to start processing.

The engine supports graceful pause via request_pause: the first call initiates a graceful wind-down (waiting for in-flight requests to finish), and a second call triggers an immediate force stop.

Fields§

§paused: bool

Whether the crawl is currently in a paused state.

Implementations§

Source§

impl<'a> CrawlerEngine<'a>

Source

pub fn new( spider: &'a dyn Spider, crawldir: Option<PathBuf>, interval_secs: f64, ) -> Result<Self>

Creates a new crawler engine for the given spider with optional checkpoint support.

Pass a crawldir path to enable pause/resume checkpointing, or None to disable it. interval_secs controls how often auto-checkpoints are saved during the crawl (0.0 disables periodic saves). The spider’s configure_sessions method is called immediately to populate the session manager; an error is returned if no sessions are registered.

Source

pub fn request_pause(&mut self)

Requests a graceful pause of the crawl. On the first call, the engine waits for all in-flight requests to finish before saving a checkpoint and exiting the loop. Calling this a second time triggers a force stop that abandons in-flight requests immediately.

Source

pub async fn crawl(&mut self) -> Result<CrawlStats>

Runs the main crawl loop and returns aggregate statistics when finished.

This is the primary entry point for executing a crawl. The method blocks (asynchronously) until the scheduler is empty and all tasks are done, or until a pause/force-stop is requested. On success it returns the final CrawlStats; check self.paused to determine whether the crawl completed or was interrupted.

Source

pub fn items(&self) -> &ItemList

Returns a reference to the collected scraped items. You can call this during or after the crawl to inspect what has been scraped so far.

Source

pub fn stats(&self) -> &CrawlStats

Returns a reference to the current crawl statistics. Like items(), this is available both during and after the crawl for monitoring progress.

Source

pub fn stream(&mut self) -> UnboundedReceiver<Value>

Creates a streaming receiver that yields items as they are scraped.

Call this before crawl() to get an unbounded receiver. Each item passes through on_scraped_item() and is sent to both the receiver and the internal ItemList.

This is the Rust equivalent of Python’s async for item in spider.stream().

§Example
let mut engine = CrawlerEngine::new(&spider, None, 0.0)?;
let mut rx = engine.stream();

// Spawn the crawl in the background
let crawl_handle = tokio::spawn(async move {
    engine.crawl().await
});

// Process items as they arrive
while let Some(item) = rx.recv().await {
    println!("Got item: {}", item);
}

let stats = crawl_handle.await??;

Auto Trait Implementations§

§

impl<'a> Freeze for CrawlerEngine<'a>

§

impl<'a> !RefUnwindSafe for CrawlerEngine<'a>

§

impl<'a> !Send for CrawlerEngine<'a>

§

impl<'a> !Sync for CrawlerEngine<'a>

§

impl<'a> Unpin for CrawlerEngine<'a>

§

impl<'a> UnsafeUnpin for CrawlerEngine<'a>

§

impl<'a> !UnwindSafe for CrawlerEngine<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more