Snapshot & Archive Result Services
Relevant source files
The following files were used as context for generating this wiki page:
The Snapshot and Archive Result Services form the core execution and reporting engine of abx-dl. These services manage the lifecycle of web content extraction, from the initial triggering of plugin hooks to the structured persistence of the results into the filesystem.
The SnapshotService orchestrates the extraction phase by launching plugin-specific hooks, managing background daemons, and handling phase-level cleanup. Meanwhile, the ArchiveResultService monitors these processes, collecting their output and synthesizing standardized ArchiveResult records. Together, they ensure that every extraction attempt is tracked, validated, and recorded in an ArchiveBox-compatible format.
Sources: abx_dl/services/snapshot_service.py:12-40, abx_dl/services/archive_result_service.py:12-30
Snapshot Lifecycle Orchestration
The extraction process is event-driven and strictly ordered. The SnapshotService begins its work upon receiving a SnapshotEvent at depth 0, which signifies the root URL being processed. It filters available plugins for "Snapshot" hooks and executes them according to their defined sort order.
Sources: abx_dl/services/snapshot_service.py:44-63
Execution Flow
- Plugin Filtering: The service identifies all plugins with
Snapshothooks. - Serial Execution: Hooks are sorted and executed. Finite hooks (foreground) are awaited, while daemon/background hooks are emitted as non-blocking processes.
- Discovery: If a hook outputs a new URL (discovered snapshot), a new
SnapshotEventis emitted with an incremented depth. - Cleanup: Once all hooks for a snapshot are finished, a
SnapshotCleanupEventtriggers the termination of background daemons.
Sources: abx_dl/services/snapshot_service.py:65-103, abx_dl/services/snapshot_service.py:157-195
The diagram above illustrates how the SnapshotService transitions from initial execution to cleanup.
Sources: abx_dl/services/snapshot_service.py:15-38
Key Classes and Functions
| Component | Type | Description |
|---|---|---|
SnapshotService | Class | Manages the snapshot phase, including hook launching and cleanup. |
on_SnapshotEvent__for_hook | Method | Generates a specific handler for a plugin hook, handling environment and timeout logic. |
on_SnapshotCleanupEvent | Method | Sends SIGTERM/SIGKILL to background processes associated with the snapshot. |
SnapshotAdmission | Dataclass | Tracks whether a snapshot is allowed to proceed based on crawl limits. |
| Sources: abx_dl/services/snapshot_service.py:44-155, abx_dl/limits.py:53-56 |
Archive Result Construction
The ArchiveResultService is responsible for translating technical process output into meaningful archival metadata. It listens for ProcessStdoutEvent and ProcessCompletedEvent to build ArchiveResult records.
Sources: abx_dl/services/archive_result_service.py:12-40
Processing Logic
- Inline Results: If a hook prints a JSON object with
type: ArchiveResultto stdout, the service parses it immediately, attaches the owningsnapshot_id, and writes it toindex.jsonl. - Synthetic Results: If a snapshot hook completes without reporting a result:
- Failure: If the exit code is non-zero, it creates a "failed" result containing the stderr.
- Success: If the exit code is zero and files were created, it creates a "succeeded" result.
- No Result: If the exit code is zero but no files were found, it records "noresult".
Sources: abx_dl/services/archive_result_service.py:44-118
This sequence shows the dual-path for result recording: proactive reporting vs. reactive synthesis.
Sources: abx_dl/services/archive_result_service.py:15-30, abx_dl/services/archive_result_service.py:44-118
Crawl Limit Enforcement
The CrawlLimitState class provides mechanisms to stop extraction once user-defined thresholds are met. This is shared across services to prevent over-consumption of disk space or network bandwidth.
Sources: abx_dl/limits.py:60-70
Limit Types
- MAX_URLS: Limits the total number of unique URLs processed in a single crawl.
- MAX_SIZE: Limits the total size of files written to the
CRAWL_DIR.
Sources: abx_dl/limits.py:84-115
# abx_dl/limits.py:117-135
def record_process_output(self, event_id: str, output_dir: Path, output_files: list[str]) -> str:
if not self.max_size:
return ""
# ... calculates added size and updates total ...
if self.max_size and int(state["total_size"]) >= self.max_size:
state["stop_reason"] = "max_size"
return str(state["stop_reason"])
Sources: abx_dl/limits.py:117-135
Metadata Persistence (index.jsonl)
The services collaborate to maintain a structured log of all actions in the index.jsonl file. This file serves as the primary manifest for the extraction run.
Sources: abx_dl/cli.py:535-542, abx_dl/services/archive_result_service.py:117-120
| Field | Description |
|---|---|
snapshot_id | Unique ID of the URL being archived. |
plugin | The name of the plugin that performed the work. |
status | The outcome (succeeded, failed, noresult, skipped). |
output_files | List of OutputFile objects with path and size. |
error | Error message or stderr output in case of failure. |
| Sources: abx_dl/events.py:170-190, abx_dl/services/archive_result_service.py:70-85 |
Conclusion
The Snapshot & Archive Result Services provide a robust, asynchronous framework for web extraction. By decoupling the execution of hooks (SnapshotService) from the reporting of results (ArchiveResultService), abx-dl ensures that extraction is both scalable and accurately documented, even when individual plugins fail or behave unexpectedly.
Sources: abx_dl/services/snapshot_service.py:12-20, abx_dl/services/archive_result_service.py:12-25