Skip to content

Library events and spawn

Abdulkader Safi edited this page Nov 16, 2026 · 1 revision

Library events and spawn

Stream progress with audit_stream

audit_stream starts the audit and gives you a futures::Stream of AuditEvents. Use it to drive a progress bar or a live log.

use futures::StreamExt;
use sitevet::{AuditEvent, Options, audit_stream};

let mut stream = audit_stream("https://example.com", Options::default());
while let Some(event) = stream.next().await {
    match event {
        AuditEvent::PageFetched { index, url, status, .. } => println!("{index:>3} {status} {url}"),
        AuditEvent::CrawlComplete { pages, checks_total, .. } => println!("{pages} pages, {checks_total} checks"),
        AuditEvent::PageComplete { index, pages_total, score, .. } => println!("{index}/{pages_total} scored {score}"),
        AuditEvent::AuditComplete { report, .. } => println!("done: {}", report.score),
        _ => {}
    }
}

Like audit, it needs a tokio context. Dropping the stream cancels the audit. stream.cancel() does the same while you keep the stream, and stream.final_report().await waits for the report and drops events you have not read.

Event order

  1. AuditStart { url, max_pages }
  2. PageFetched { url, status, depth, index, ms }, one per page as the crawl finds it
  3. PageRendered { url, ok, index, pages }, one per page the browser or PageSpeed Insights finished, then RenderComplete { provider, rendered, pages, ms }. Only when rendering ran.
  4. CrawlComplete { pages, checks_total, ms }. From here on, counts run against a known total.
  5. CheckComplete { url, rule_id, status, checks_done, checks_total } and PageComplete { url, index, pages_total, score, findings, metrics, ms } as pages finish
  6. AuditComplete { score, pages, checks, ms, report }, with the same report audit would return

AuditEvent is #[non_exhaustive]: keep a _ => {} arm, since later versions add variants. Events serialise to JSON with a "type" field ("page_fetched" and so on), so you can forward them to a web UI as they are.

The event channel is bounded. A slow reader slows the audit down instead of growing memory.

Run without tokio: spawn

spawn runs the audit on a runtime sitevet owns, so it works from any executor: GPUI, smol, futures::executor or a plain thread.

use futures::executor::block_on;
use sitevet::{AuditEvent, Options, spawn};

let handle = spawn("https://example.com", Options::default());
let events = handle.events(); // an async_channel::Receiver<AuditEvent>; clone it freely

std::thread::spawn(move || {
    block_on(async {
        while let Ok(event) = events.recv().await {
            if let AuditEvent::PageFetched { url, .. } = event {
                eprintln!("fetched {url}");
            }
        }
    })
});

let report = block_on(handle.report())?;

handle.cancel() stops network work at once, and report() then returns Error::Cancelled. Dropping the handle cancels too. The runtime starts on first use with options.runtime_threads workers (default 2) and shuts down when the last handle is gone.

A full example is in examples/spawn-futures.rs:

cargo run -p sitevet --example spawn-futures -- https://example.com

Clone this wiki locally