Skip to main content

Job

Trait Job 

Source
pub trait Job:
    Serialize
    + DeserializeOwned
    + Send
    + Sync
    + 'static {
    const KIND: &'static str;
    const MAX_ATTEMPTS: u32 = 8;
    const TIMEOUT: Duration = _;

    // Required method
    fn run(self, cx: &Cx) -> impl Future<Output = Result<()>> + Send;

    // Provided method
    fn lane(&self) -> Option<String> { ... }
}
Expand description

A kind of background work, whose fields are its payload.

use serde::{Deserialize, Serialize};
use tablo_jobs::Job;
use topcoat::context::Cx;

#[derive(Serialize, Deserialize)]
struct SyncSeats {
    team: u64,
}

impl Job for SyncSeats {
    const KIND: &'static str = "sync-seats";

    fn lane(&self) -> Option<String> {
        Some(format!("team:{}", self.team))
    }

    async fn run(self, cx: &Cx) -> topcoat::Result<()> {
        // Read the team as it is now, and push its seat count.
        Ok(())
    }
}
  • run may run twice for one enqueue, after a crash or a lapsed lock, so it is idempotent.
  • It reads the world as it is when it runs, not as it was at enqueue: the payload names rows, it does not copy them.
  • The payload lives in the database and its backups in clear: it carries no secret.
  • A run past TIMEOUT is dropped at its next .await. A run that blocks its thread instead outlives its lock, and another worker starts it again beside it: blocking work goes through tokio::task::spawn_blocking.

Required Associated Constants§

Source

const KIND: &'static str

What the queue calls this kind. A queued job of a kind no worker registers waits, so a rename keeps the old name registered until the queue holds none.

Provided Associated Constants§

Source

const MAX_ATTEMPTS: u32 = 8

How many runs it gets before it turns Dead.

Source

const TIMEOUT: Duration = _

How long a run may take before it counts as failed. The claim locks the job a minute longer, so no other worker takes over a live run.

Required Methods§

Source

fn run(self, cx: &Cx) -> impl Future<Output = Result<()>> + Send

Does the work, reading what it needs from cx: the context PanelHandle::context builds, with what Worker::context adds. An error retries it with backoff until it has spent its attempts.

Provided Methods§

Source

fn lane(&self) -> Option<String>

The lane it runs in: of the jobs sharing a lane, one runs at a time across every worker. None, the default, runs beside anything.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§