Skip to main content

JobQueue

Trait JobQueue 

Source
pub trait JobQueue:
    Send
    + Sync
    + 'static {
    // Required methods
    fn enqueue(
        &self,
        job: QueuedJob,
    ) -> impl Future<Output = Result<(), QueueError>> + Send;
    fn claim(
        &self,
        worker_id: &str,
        worker_labels: &[String],
        lease_ttl: Duration,
    ) -> impl Future<Output = Result<Option<(QueuedJob, Lease)>, QueueError>> + Send;
    fn renew_lease(
        &self,
        lease_id: &str,
        extend_by: Duration,
    ) -> impl Future<Output = Result<(), QueueError>> + Send;
    fn complete(
        &self,
        lease_id: &str,
        outputs: HashMap<String, String>,
    ) -> impl Future<Output = Result<(), QueueError>> + Send;
    fn fail(
        &self,
        lease_id: &str,
        error: String,
        retryable: bool,
    ) -> impl Future<Output = Result<(), QueueError>> + Send;
    fn cancel(
        &self,
        workflow_id: &str,
        job_id: &str,
    ) -> impl Future<Output = Result<(), QueueError>> + Send;
    fn cancel_workflow(
        &self,
        workflow_id: &str,
    ) -> impl Future<Output = Result<(), QueueError>> + Send;
    fn is_cancelled(
        &self,
        workflow_id: &str,
        job_id: &str,
    ) -> impl Future<Output = Result<bool, QueueError>> + Send;
    fn reap_expired_leases(
        &self,
    ) -> impl Future<Output = Result<Vec<JobEvent>, QueueError>> + Send;
    fn subscribe(&self) -> Receiver<JobEvent>;
}
Expand description

Pluggable job queue backend.

Implementations: InMemoryJobQueue, PgBossJobQueue (Postgres), RedisJobQueue.

pg-boss mapping:

  • enqueue()boss.send(queue, data, options)
  • claim()boss.fetch(queue) (SELECT … FOR UPDATE SKIP LOCKED)
  • complete()boss.complete(jobId)
  • fail()boss.fail(jobId)
  • cancel()boss.cancel(jobId)

Required Methods§

Source

fn enqueue( &self, job: QueuedJob, ) -> impl Future<Output = Result<(), QueueError>> + Send

Enqueue a job for execution. Called by the scheduler when dependencies are met.

Source

fn claim( &self, worker_id: &str, worker_labels: &[String], lease_ttl: Duration, ) -> impl Future<Output = Result<Option<(QueuedJob, Lease)>, QueueError>> + Send

Atomically claim the next available job matching the worker’s labels. Returns None if no matching job is available.

Source

fn renew_lease( &self, lease_id: &str, extend_by: Duration, ) -> impl Future<Output = Result<(), QueueError>> + Send

Renew a lease (heartbeat). Returns error if the lease has already expired.

Source

fn complete( &self, lease_id: &str, outputs: HashMap<String, String>, ) -> impl Future<Output = Result<(), QueueError>> + Send

Complete a job successfully. Releases the lease and stores outputs.

Source

fn fail( &self, lease_id: &str, error: String, retryable: bool, ) -> impl Future<Output = Result<(), QueueError>> + Send

Fail a job. The queue decides whether to re-enqueue based on RetryPolicy.

Source

fn cancel( &self, workflow_id: &str, job_id: &str, ) -> impl Future<Output = Result<(), QueueError>> + Send

Cancel a specific job. If currently claimed, marks it for cancellation.

Source

fn cancel_workflow( &self, workflow_id: &str, ) -> impl Future<Output = Result<(), QueueError>> + Send

Cancel all jobs for a workflow.

Source

fn is_cancelled( &self, workflow_id: &str, job_id: &str, ) -> impl Future<Output = Result<bool, QueueError>> + Send

Check if a job has been marked for cancellation (workers poll this).

Source

fn reap_expired_leases( &self, ) -> impl Future<Output = Result<Vec<JobEvent>, QueueError>> + Send

Collect expired leases and emit LeaseExpired events. Called periodically by the server’s monitor task.

Source

fn subscribe(&self) -> Receiver<JobEvent>

Subscribe to job events for event-driven processing.

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.

Implementors§