Loom Crawler and Job Design
Part of Loom. Constraints: Loom Responsible Crawling. Persistence: Loom Data and Storage.
Roles and execution model
- Go API: authenticates users, validates settings, creates jobs, serves status and graph queries.
- Dispatcher/reconciler: publishes durable pending work to Redis and repairs missing deliveries.
- Go workers: consume fetch tasks using a bounded goroutine pool; each task performs bounded network and parsing work.
- Shared scheduler: enforces per-target concurrency, next-request times, cooldowns, and global limits across workers and jobs.
Queue page-fetch tasks rather than holding one queue task open for an entire crawl. Use bounded channels and parser pools; never create one unbounded goroutine per discovered URL. A goroutine is an execution tool, not durable job storage.
Durable workflow
- The API creates a crawl job, its seed frontier entry, and an outbox event in one database transaction.
- A dispatcher publishes a small task containing IDs, then marks the event dispatched. A crash can duplicate delivery, so consumers must be idempotent.
- A worker atomically claims a frontier entry with an expiring lease and attempt token. Skip stale, cancelled, or already-settled tasks.
- Validate scope, robots policy, network destination, and budgets. Acquire shared host concurrency and rate permission. If not yet eligible, reschedule with delay rather than sleeping in a worker slot.
- Fetch with a cancellable context, parse bounded HTML, normalize URLs, and collect edges.
- Atomically persist the observation, edges, new deduplicated frontier entries/outbox events, and settlement of this frontier entry. Only the current attempt token may commit.
- Acknowledge the queue task after the durable transaction commits. Retries after a crash see the settled entry and do not duplicate graph effects.
A crash after a remote request but before commit can cause a repeated request. Do not claim exactly-once HTTP fetching. Charge every actual request attempt against limits.
Queue and scheduling
Proposed queue: Redis with Asynq. Delivery can repeat; database constraints and claim tokens are the correctness boundary. Redis task uniqueness is an optimization, not the only protection.
Use fair scheduling so one large crawl cannot monopolize all workers. Jobs targeting the same host share one budget regardless of user or queue. A website-group budget can additionally combine related hosts; keep that grouping conservative and administratively configurable.
Separate controls for worker count, per-host in-flight requests, minimum request-start interval, global request rate, and per-job resource limits. Connection pooling alone is not a request limiter, especially with multiplexed protocols.
Lifecycle
queued → running → completed | cancelled | failed
Keep termination_reason separate: frontier_exhausted, page_cap, time_cap, data_cap, user_cancelled, or system_failure. Store timestamps and a monotonic state revision so stale browser updates cannot replace newer state.
Frontier entries use pending → leased → settled, with explicit retry scheduling and a final outcome. Recover expired leases; bound retry counts and reject late commits from old attempts. Cancel prevents new claims, propagates to active contexts, and fences late writes after cancellation finalization.
Completion and budget accounting
Complete a job only when no eligible pending entries, active leases, delayed retries, or undispatched crawl outbox events remain. Determine this from durable state under job-level coordination, not Redis queue length alone.
Reserve page-attempt capacity atomically before scheduling/fetching so parallel workers cannot overshoot the cap. Separate unique-URL cap, total HTTP-attempt cap, redirect-hop cap, byte budget, and wall-clock budget. Retries, robots requests, and redirects consume request budgets even when they do not add a page.
Commit page settlement and child discovery together so the last active fetch cannot finish a job before its children are durable. Denied, out-of-scope, or limit-skipped targets can remain graph stubs with explicit outcomes.
Recovery
Reconcile pending frontier records, undispatched events, and expired leases on a schedule. A Redis loss should be recoverable from the durable ledger without erasing results. Enforce host pacing during recovery as well as normal operation. If the shared limiter is unavailable, defer fetching rather than bypass it.