Skip to main content
The job system powers long-running operations in Spacedrive. It provides automatic persistence, progress tracking, and graceful interruption handling for tasks like indexing, file processing, and sync operations. Jobs execute asynchronously with minimal boilerplate. They persist their state to survive crashes and resume where they left off. The system integrates with Spacedrive’s task executor for efficient resource usage.

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.
Failed or cancelled jobs cannot resume. The system distinguishes between recoverable interruptions and permanent failures.

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 at core/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:
Progress updates throttle automatically. The system batches updates to prevent database overhead.

Error Handling

Jobs distinguish between recoverable and permanent errors:
Always check ctx.check_interrupted() in loops. This enables graceful shutdown and pause operations.

Dispatching Jobs

The job manager provides typed and dynamic dispatch methods:
Job handles provide status monitoring and progress streaming:

Database Schema

Jobs persist to a dedicated SQLite database (jobs.db) with three tables:
jobs
table
Active job records containing:
job_history
table
Completed jobs moved here for audit trails
job_checkpoints
table
Resumption checkpoints for long-running jobs

Advanced Features

Job Versioning

Jobs specify versions for schema evolution:
The registry validates versions during resumption. Incompatible versions fail to load.

Extension Jobs

The system supports WASM-based extension jobs:
Extensions run in isolated contexts with limited capabilities.

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
For high-frequency operations, batch work into larger chunks. This reduces checkpoint overhead and improves performance.

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 to JOB_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:
Logs write to .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.
The job system provides the foundation for reliable background processing in Spacedrive. Its resumable design ensures operations complete despite interruptions, while the progress system keeps users informed of ongoing work.