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.
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.
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.
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.
Logs write to .spacedrive/jobs/{job_id}.log with detailed execution traces.Monitor job metrics through the context:
Copy
let metrics = ctx.get_metrics();println!("Execution time: {}s", metrics.elapsed_seconds);println!("Memory used: {}MB", metrics.memory_mb);
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.