Core Concepts
A job represents a resumable unit of work. Jobs report progress, handle interruptions, and maintain state across executions. The system manages job lifecycles automatically.Jobs are library-scoped. Each library maintains its own job database and execution queue.
Job Lifecycle
Jobs transition through defined states during execution:1
Queued
Job created and waiting for execution. Initial state after dispatch.
2
Running
Job actively executing. Progress updates flow to subscribers.
3
Paused
Job interrupted but resumable. State persisted to database.
4
Completed
Job finished successfully. Moved to history table.
Key Components
The job system consists of several interconnected parts: Job Manager coordinates all job operations. It maintains the job database, tracks running jobs, and handles lifecycle transitions. Located atcore/src/infra/job/manager.rs.
Job Registry enables automatic job discovery. Jobs register themselves at compile time using the derive macro. The registry creates jobs dynamically from saved state. See core/src/infra/job/registry.rs.
Job Context provides execution environment. Jobs access the database, report progress, and interact with services through context. Implementation in core/src/infra/job/context.rs.
Job Executor bridges jobs with the task system. It manages interruption signals and checkpoint operations. Found at core/src/infra/job/executor.rs.
Defining Jobs
Jobs implement two traits:Job for metadata and JobHandler for execution logic.
The
#[typetag::serde] attribute enables polymorphic serialization. Jobs must be serializable to support resumption.Progress Reporting
Jobs communicate progress through the context. The system supports multiple progress types:Error Handling
Jobs distinguish between recoverable and permanent errors:Dispatching Jobs
The job manager provides typed and dynamic dispatch methods:Database Schema
Jobs persist to a dedicated SQLite database (jobs.db) with three tables:
Active job records containing:
Completed jobs moved here for audit trails
Resumption checkpoints for long-running jobs
Advanced Features
Job Versioning
Jobs specify versions for schema evolution:Extension Jobs
The system supports WASM-based extension jobs:Performance Considerations
The job system optimizes for throughput and resumability:- Progress updates batch at 2-second intervals
- Checkpoints save incrementally
- Database operations use prepared statements
- Channels use bounded capacity to prevent memory growth
Integration Points
Jobs integrate with core Spacedrive systems: Task System: Jobs execute as tasks with configurable priority. The executor handles work distribution across threads. Event System: State changes emit events for UI updates. Subscribe toJOB_MANAGER_EVENTS for notifications.
Action System: User actions spawn jobs with audit context. The system tracks who initiated operations.
Library System: Each library maintains independent job state. Jobs cannot access cross-library data.
Common Patterns
Batch Processing
Process items in chunks for efficiency:Phased Execution
Split complex jobs into phases:Child Jobs
Spawn dependent jobs (feature in development):Debugging
Enable file-based logging for troubleshooting:.spacedrive/jobs/{job_id}.log with detailed execution traces.
Monitor job metrics through the context:
Never block the job executor thread. Use
tokio::task::spawn_blocking for CPU-intensive work.