Crate apalis

source ·
Expand description

apalis is a simple, extensible multithreaded background job processing library for rust.

Core Features

  • Simple and predictable functional job handling model with a macro free API.
  • Takes full advantage of the tower ecosystem of middleware, services, and utilities.
  • Anything that implements Stream can be used as a job source.
  • Runtime agnostic with inbuilt support for tokio and async-std.
  • Provides high concurrency, and allows for configuration of workers, jobs and thread pool.

An apalis job is powered by a tower Service which means you have access to the tower middleware.

Example

use apalis::prelude::*;
use serde::{Deserialize, Serialize};
use apalis_redis::RedisStorage;

#[derive(Debug, Deserialize, Serialize)]
struct Email {
    to: String,
}

impl Job for Email {
    const NAME: &'static str = "apalis::Email";
}

async fn send_email(job: Email, ctx: JobContext) -> Result<(), JobError> {
    Ok(())
}

#[tokio::main]
async fn main() {
    let redis = std::env::var("REDIS_URL").expect("Missing REDIS_URL env variable");
    let storage = RedisStorage::connect(redis).await.expect("Storage failed");
    Monitor::new()
        .register_with_count(2, move |index| {
            WorkerBuilder::new(&format!("quick-sand-{index}"))
                .with_storage(storage.clone())
                .build_fn(send_email)
        })
        .run()
        .await
        .unwrap();
}

Web UI Available

UI See this example

Feature flags

  • tracing (enabled by default) — Support Tracing 👀

  • redis — Include redis storage

  • postgres — Include Postgres storage

  • sqlite — Include SQlite storage

  • mysql — Include MySql storage

  • cron — Include Cron functionality

  • sentry — Support for Sentry exception and performance monitoring

  • prometheus — Support Prometheus metrics

  • retry — Support direct retrying jobs

  • timeout — Support timeouts on jobs

  • limit — 💪 Limit the amount of jobs

  • filter — Support filtering jobs based on a predicate

  • extensions — Add a global extensions to jobs

  • async-std-comp — Compatibility with async-std and smol runtimes

  • tokio-comp (enabled by default) — Compatibility with tokio and actix runtimes

  • expose — Expose job sources for web and cli purposes

Modules

  • croncron
    Include Cron utilities
  • apalis jobs fully support tower middleware via Layer
  • postgrespostgres
    Include the default Postgres storage
  • Common imports
  • redisredis
    Include the default Redis storage
  • sqlitesqlite
    Include the default Sqlite storage