pub trait JobQueue:
Send
+ Sync
+ Clone {
// Required methods
fn create_job(
&self,
request: CreateJobRequest,
) -> impl Future<Output = Result<HarvestJob, AppError>> + Send;
fn claim_job(
&self,
worker_id: &str,
) -> impl Future<Output = Result<Option<HarvestJob>, AppError>> + Send;
fn complete_job(
&self,
job_id: Uuid,
stats: SyncStats,
) -> impl Future<Output = Result<(), AppError>> + Send;
fn fail_job(
&self,
job_id: Uuid,
error: &str,
next_retry_at: Option<DateTime<Utc>>,
) -> impl Future<Output = Result<(), AppError>> + Send;
fn cancel_job(
&self,
job_id: Uuid,
stats: Option<SyncStats>,
) -> impl Future<Output = Result<(), AppError>> + Send;
fn get_job(
&self,
job_id: Uuid,
) -> impl Future<Output = Result<Option<HarvestJob>, AppError>> + Send;
fn list_jobs(
&self,
status: Option<JobStatus>,
limit: usize,
) -> impl Future<Output = Result<Vec<HarvestJob>, AppError>> + Send;
fn release_job(
&self,
job_id: Uuid,
) -> impl Future<Output = Result<(), AppError>> + Send;
fn release_worker_jobs(
&self,
worker_id: &str,
) -> impl Future<Output = Result<u64, AppError>> + Send;
fn count_by_status(
&self,
status: JobStatus,
) -> impl Future<Output = Result<i64, AppError>> + Send;
}Expand description
Trait for job queue persistence operations.
This abstraction enables different storage backends (PostgreSQL, in-memory for tests) and facilitates dependency injection in the worker service.
§Implementation Notes
Implementations should ensure:
- Atomic job claiming with
SELECT FOR UPDATE SKIP LOCKEDsemantics - Proper handling of retry scheduling
- Safe concurrent access from multiple workers
Required Methods§
Sourcefn create_job(
&self,
request: CreateJobRequest,
) -> impl Future<Output = Result<HarvestJob, AppError>> + Send
fn create_job( &self, request: CreateJobRequest, ) -> impl Future<Output = Result<HarvestJob, AppError>> + Send
Create a new job in the queue.
Returns the created job with generated ID and timestamps.
Sourcefn claim_job(
&self,
worker_id: &str,
) -> impl Future<Output = Result<Option<HarvestJob>, AppError>> + Send
fn claim_job( &self, worker_id: &str, ) -> impl Future<Output = Result<Option<HarvestJob>, AppError>> + Send
Claim the next available pending job for processing.
Uses SELECT FOR UPDATE SKIP LOCKED semantics for safe concurrent claiming.
Jobs are claimed in order of:
- Non-retry jobs first (next_retry_at IS NULL)
- Then retry-ready jobs (next_retry_at <= NOW)
- Oldest first within each category
Returns None if no jobs are available.
Sourcefn complete_job(
&self,
job_id: Uuid,
stats: SyncStats,
) -> impl Future<Output = Result<(), AppError>> + Send
fn complete_job( &self, job_id: Uuid, stats: SyncStats, ) -> impl Future<Output = Result<(), AppError>> + Send
Mark a job as completed with final statistics.
Sourcefn fail_job(
&self,
job_id: Uuid,
error: &str,
next_retry_at: Option<DateTime<Utc>>,
) -> impl Future<Output = Result<(), AppError>> + Send
fn fail_job( &self, job_id: Uuid, error: &str, next_retry_at: Option<DateTime<Utc>>, ) -> impl Future<Output = Result<(), AppError>> + Send
Mark a job as failed with error message.
If next_retry_at is provided, the job is reset to pending for retry.
Otherwise, the job is marked as permanently failed.
Sourcefn cancel_job(
&self,
job_id: Uuid,
stats: Option<SyncStats>,
) -> impl Future<Output = Result<(), AppError>> + Send
fn cancel_job( &self, job_id: Uuid, stats: Option<SyncStats>, ) -> impl Future<Output = Result<(), AppError>> + Send
Mark a job as cancelled.
Optionally saves partial sync statistics.
Sourcefn get_job(
&self,
job_id: Uuid,
) -> impl Future<Output = Result<Option<HarvestJob>, AppError>> + Send
fn get_job( &self, job_id: Uuid, ) -> impl Future<Output = Result<Option<HarvestJob>, AppError>> + Send
Get a job by ID.
Sourcefn list_jobs(
&self,
status: Option<JobStatus>,
limit: usize,
) -> impl Future<Output = Result<Vec<HarvestJob>, AppError>> + Send
fn list_jobs( &self, status: Option<JobStatus>, limit: usize, ) -> impl Future<Output = Result<Vec<HarvestJob>, AppError>> + Send
List jobs with optional status filter.
Results are ordered by creation time (newest first).
Sourcefn release_job(
&self,
job_id: Uuid,
) -> impl Future<Output = Result<(), AppError>> + Send
fn release_job( &self, job_id: Uuid, ) -> impl Future<Output = Result<(), AppError>> + Send
Release a job back to pending state.
Used when a worker needs to give up a job (e.g., during shutdown). Only affects jobs in ‘running’ status.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.