Limits & Heartbeat
Relevant source files
The following files were used as context for generating this wiki page:
The Limits and Heartbeat systems in abx-dl provide resource management and health monitoring for archival operations. The Limits system enforces constraints on the number of URLs and total data size permitted during a crawl, ensuring that automated tasks do not exceed system capacity or user-defined quotas. The Heartbeat system provides a liveness mechanism for background processes, particularly headless browser sessions, to facilitate safe cleanup of stale or abandoned tasks.
These components are critical for maintaining system stability, especially when abx-dl is deployed in long-running environments or integrated into larger orchestration frameworks like ArchiveBox. Sources: README.md, abx_dl/limits.py, abx_dl/heartbeat.py
Resource Limits Management
The resource limits system is primarily implemented through the CrawlLimitState class, which manages persistence and enforcement of crawl-wide constraints.
CrawlLimitState Logic
CrawlLimitState uses a file-based locking mechanism to synchronize state across multiple processes participating in a crawl. It tracks admitted snapshots and the cumulative size of output files in a limits.json file stored within the crawl directory. Sources: abx_dl/limits.py:65-71
Key functionalities include:
- Snapshot Admission: Determines if a new URL can be processed based on the
MAX_URLSlimit. Sources: abx_dl/limits.py:90-117 - Size Enforcement: Tracks the total byte size of outputs produced by plugins. If the
MAX_SIZElimit is reached, it signals a "max_size" stop reason. Sources: abx_dl/limits.py:129-152 - Atomic Persistence: Uses
fcntl(on non-Windows platforms) to perform advisory locking onlimits.lockbefore reading or updating the state file. Sources: abx_dl/limits.py:154-169
Filesize Parsing
abx-dl supports human-readable strings for defining size limits. The parse_filesize_to_bytes utility converts strings like "45mb" or "2GB" into integer bytes. Sources: abx_dl/limits.py:24-60
| Unit Suffix | Multiplier |
|---|---|
| b, byte, bytes | 1 |
| k, kb, kib | 1,024 |
| m, mb, mib | 1,048,576 |
| g, gb, gib | 1,073,741,824 |
| t, tb, tib | 1,099,511,627,776 |
Sources: abx_dl/limits.py:24-41
Limits Flow Diagram
This diagram illustrates how CrawlLimitState decides whether to allow a new snapshot.
The diagram shows the logic flow for enforcing maximum URL counts during a crawl session. Sources: abx_dl/limits.py:90-120
Process Heartbeat
The Heartbeat system, implemented in CrawlHeartbeat, ensures that long-running background tasks (such as Chrome daemon hooks) can be monitored for health.
Heartbeat Mechanism
A CrawlHeartbeat instance runs as an asynchronous background task. It periodically updates a .heartbeat.json file in the crawl directory. This file contains metadata about the process, including its PID and the last time it was confirmed alive. Sources: abx_dl/heartbeat.py:15-32
The heartbeat payload includes:
- runtime: The execution environment identifier.
- crawl_id: Unique identifier for the current crawl session.
- owner_pid: The process ID of the orchestrator.
- last_alive_at: A Unix timestamp of the last update.
- kill_after_seconds: A threshold after which the process is considered stale.
Sources: abx_dl/heartbeat.py:34-41
Heartbeat Sequence
The following sequence diagram shows the lifecycle of a heartbeat update loop.
This diagram demonstrates the periodic, atomic update process used to signal process liveness. Sources: abx_dl/heartbeat.py:48-68
Configuration and CLI Integration
Limits are exposed through both environment variables and CLI flags.
Configuration Parameters
| CLI Flag | Env Var | Description | Default |
|---|---|---|---|
--max-urls | MAX_URLS | Max number of URLs to snapshot per crawl. | 0 (unlimited) |
--max-size | MAX_SIZE | Max total crawl size (e.g., 500mb, 1gb). | 0 (unlimited) |
--timeout | TIMEOUT | Global process timeout in seconds. | 60 |
Sources: abx_dl/cli.py:596-601, abx_dl/config.py, README.md
CLI Limit Enforcement
During execution, the CLI uses CrawlLimitState to monitor output files produced by each plugin hook. If a hook produces output that exceeds the size limit, the orchestrator records the stop reason and prevents further snapshots. Sources: tests/test_cli.py:440-455, abx_dl/cli.py:1010-1025
Server-Side Session TTL
In the abx-dl Web Download Server, resource management is extended through session Time-To-Live (TTL). Sessions are automatically cleaned up after 24 hours to free disk space.
- Cleanup Loop: A background thread runs hourly to identify and delete directories for sessions older than
SESSION_TTL_HOURS. - Session State: Session metadata, including status (starting, running, completed, failed, timeout), is persisted in
session.jsonwithin each session directory.
Sources: server/server.py:53-56, server/server.py:214-230
Technical Summary
The combination of CrawlLimitState for hard resource boundaries and CrawlHeartbeat for process health monitoring provides a robust foundation for automated web archiving. By using atomic file operations and persistent state, abx-dl ensures that resource constraints are respected even across distributed plugin executions and system restarts. Sources: abx_dl/limits.py, abx_dl/heartbeat.py, server/server.py