Skip to main content

Crate ezjobs

Crate ezjobs 

Source
Expand description

ezjobs — a persistent, retryable background job queue for Rust.

MariaDB/MySQL first (matches the SQLx + MariaDB stack used in typical ezconfig / ezroutes Axum backends). Jobs are stored in a table, so they survive process restarts and redeploys, and can be safely worked by multiple processes at once.

§Quick start

ⓘ
use ezjobs::{Job, JobQueue, Registry, Worker, WorkerConfig};

#[derive(serde::Serialize, serde::Deserialize)]
struct SendWelcomeEmail { user_id: i64 }

#[async_trait::async_trait]
impl Job for SendWelcomeEmail {
    const JOB_TYPE: &'static str = "send_welcome_email";

    async fn run(&self) -> ezjobs::Result<()> {
        // send the email
        Ok(())
    }
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let pool = sqlx::MySqlPool::connect("mysql://...").await?;

    // Run migrations/mysql/*.sql against your database first (see README).

    let queue = JobQueue::new(pool.clone());
    queue.enqueue(&SendWelcomeEmail { user_id: 42 }).await?;

    let mut registry = Registry::new();
    registry.register::<SendWelcomeEmail>();

    let worker = Worker::new(pool, registry, WorkerConfig::default());
    tokio::spawn(worker.run());

    Ok(())
}

Re-exports§

pub use db::DbPool;
pub use db::create_pool;
pub use db::run_migrations;

Modules§

db
Database backend abstraction — switches between MySQL and Postgres at compile time via the mysql / postgres feature flags.

Structs§

Admin
Admin API for inspecting and managing the job queue.
CancellationToken
A token which can be used to signal a cancellation request to one or more tasks.
JobQueue
The handle your app uses to push work onto the queue. Cheap to clone (wraps a connection pool) — store one in your Axum app state.
Registry
Holds the mapping from job-type string to the code that runs it. Register every job type your worker needs to process before calling Worker::run.
RunResult
Result of a single serverless run.
Scheduler
Registers and runs recurring jobs on a cron schedule (5-field cron syntax, e.g. "0 2 * * *" for 2am daily). On each tick, due schedules get a fresh row inserted into ezjobs_jobs for the worker to pick up.
Worker
Polls ezjobs_jobs for due work and runs it through the Registry.
WorkerConfig

Enums§

EzJobsError

Traits§

Job
Implement this for any struct you want to enqueue and run in the background.

Functions§

dashboard_router
Re-exported for convenience when setting up the dashboard routes. Mount the dashboard routes onto an existing router.
run_once
Poll the queue once, process all due jobs, and wait for completion.

Type Aliases§

Result