Process & Crawl Services
Relevant source files
The following files were used as context for generating this wiki page:
The Process and Crawl services form the core execution engine of abx-dl. They manage the lifecycle of web downloads by orchestrating plugin hooks through a structured event-driven architecture. While the Crawl service (and associated lifecycle events) manages the high-level phases of a download—including setup, starting, and cleanup—the ProcessService is responsible for the low-level execution of subprocesses, log rotation, and real-time output streaming.
These services operate on a shared EventBus, allowing for decoupled communication between the CLI, the orchestrator, and individual plugin hooks. This architecture ensures that background daemons can run concurrently with foreground extraction tasks while maintaining clean lifecycle management and resource cleanup.
Sources: abx_dl/services/process_service.py:79-91, abx_dl/events.py:1-40, README.md:126-136
Lifecycle and Orchestration
The crawl lifecycle is divided into distinct phases triggered by specific events. This ensures that dependencies are resolved, daemons are launched, and snapshots are taken in a predictable sequence.
Phase Execution Order
- Install Phase: Resolves required binaries via
BinaryRequestEvent. - CrawlSetup Phase: Launches shared daemons and configures runtime state via
on_CrawlSetup__*hooks. - Snapshot Phase: Executes extraction hooks per URL.
- Cleanup Phase: Terminates background processes.
Sources: abx_dl/events.py:5-25, README.md:46-51
The following diagram illustrates the flow of events during a standard crawl operation:
The flow starts with dependency installation and proceeds through setup, snapshotting, and finally cleanup.
Sources: abx_dl/events.py:5-25
ProcessService Architecture
The ProcessService is the primary handler for ProcessEvent records. It manages the actual spawning of asyncio.subprocess instances and handles the complexities of log management and process signals.
Key Responsibilities
- Subprocess Management: Spawns processes using
asyncio.create_subprocess_execwith new sessions to allow targeted interrupts. - Log Rotation: Implements
_rotate_existing_logto ensure failed attempts remain debuggable without overwriting previous logs. - Real-time Streaming: Uses
_stream_stdoutto emitProcessStdoutEventfor every line produced by a hook, enabling the CLI to show live progress. - Resource Tracking: Writes
.pid,.sh(command), and.logfiles to the output directory for monitoring and cleanup.
Sources: abx_dl/services/process_service.py:79-110, abx_dl/services/process_service.py:44-76
Process Execution Sequence
When a ProcessEvent is received, the service follows a strict sequence:
The sequence ensures that the system remains responsive to live output while maintaining persistent logs on disk.
Sources: abx_dl/services/process_service.py:126-285
Data Models and Event Schemas
The system relies on Pydantic models to define the state of processes and the structure of events.
Core Models
| Model | Description | Key Fields |
|---|---|---|
Process | Represents a single hook invocation. | id, cmd, exit_code, status, stdout, stderr |
ArchiveResult | The output record for a plugin's work. | snapshot_id, plugin, status, output_files |
Hook | Metadata about a plugin executable. | name, event, plugin_name, path, is_background |
Sources: abx_dl/models.py:90-112, abx_dl/models.py:202-224, abx_dl/models.py:270-288
Critical Events
| Event | Purpose | Handler |
|---|---|---|
ProcessEvent | Command to run a hook. | ProcessService.on_ProcessEvent |
ProcessKillEvent | Command to terminate a background daemon. | ProcessService.on_ProcessKillEvent |
ProcessStdoutEvent | Transmits a single line of output. | SnapshotService / CLI |
BinaryRequestEvent | Requests a dependency. | BinaryService |
Sources: abx_dl/events.py:157-190, abx_dl/events.py:246-271
Implementation Details
Subprocess Spawning and Logging
The ProcessService ensures that every hook run is isolated and documented. Hook retries use timestamped log filenames to avoid data loss.
# abx_dl/services/process_service.py:145-155
artifact_stem = event.hook_name
if event.hook_name.startswith("on_BinaryRequest__"):
artifact_stem = f"{event.hook_name}.{proc.id}"
cmd = [event.hook_path, *event.hook_args]
stdout_file = plugin_output_dir / f"{artifact_stem}.stdout.log"
stderr_file = plugin_output_dir / f"{artifact_stem}.stderr.log"
pid_file = plugin_output_dir / f"{artifact_stem}.pid"
cmd_file = plugin_output_dir / f"{artifact_stem}.sh"
_rotate_existing_log(stdout_file)
_rotate_existing_log(stderr_file)
Sources: abx_dl/services/process_service.py:145-157
Status Normalization
Subprocess exit codes are mapped to human-readable statuses that the ArchiveResult system can consume.
| Exit Code | Status |
|---|---|
0 | succeeded |
10 (PROCESS_EXIT_SKIPPED) | skipped |
Other | failed |
Sources: abx_dl/services/process_service.py:34-41
Summary
The Process and Crawl services provide a robust, event-driven framework for executing arbitrary plugin hooks. By separating high-level crawl lifecycle management from low-level subprocess handling, the system achieves a high degree of reliability and observability. The ProcessService specifically handles the "heavy lifting" of process isolation, real-time logging, and graceful termination, ensuring that the project can run a wide variety of external tools as part of a single download operation.