MCPcopy Index your code
hub / github.com/apalis-dev/apalis

github.com/apalis-dev/apalis @v0.7.4

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.7.4 ↗ · + Follow
802 symbols 2,254 edges 78 files 258 documented · 32%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

apalis

Simple, extensible multithreaded background job and messages processing library for Rust

Crates.io version

Download

docs.rs docs CI

Features

  • Simple and predictable task handling model.
  • Task handlers are just an async function with a macro free API.
  • Familiar dependency injection for task handlers, similar to actix and axum.
  • Take full advantage of the [tower] ecosystem of middleware, services, and utilities.
  • Easy to scale, backends are distributed by default.
  • Runtime agnostic - Use tokio, smol etc.
  • Inbuilt concurrency and parallelism.
  • Worker monitoring and graceful shutdown.
  • Ability to painlessly expose tasks and workers via APIs
  • Persisted cron jobs. Pipe your cronjobs to other backends and distribute them.
  • Optional Web interface to help you manage your jobs.

apalis job processing is powered by [tower::Service] which means you have access to the [tower] middleware.

apalis has support for:

Source Crate Example
Cron Jobs
Redis
Sqlite
Postgres
MySQL
Amqp
From Scratch

Getting Started

To get started, just add to Cargo.toml

[dependencies]
apalis = { version = "0.7", features = "limit" } # Limit for concurrency
apalis-redis = { version = "0.7" } # Use redis for persistence

Usage

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

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

/// A function called for every job
async fn send_email(job: Email, data: Data<usize>) -> Result<(), Error> {
  /// execute job
  Ok(())
}

#[tokio::main]
async fn main() -> {
    std::env::set_var("RUST_LOG", "debug");
    env_logger::init();
    let redis_url = std::env::var("REDIS_URL").expect("Missing env variable REDIS_URL");
    let conn = apalis_redis::connect(redis_url).await.expect("Could not connect");
    let storage = RedisStorage::new(conn);
    WorkerBuilder::new("email-worker")
      .concurrency(2)
      .data(0usize)
      .backend(storage)
      .build_fn(send_email)
      .run()
      .await;
}

Then

//This can be in another part of the program or another application eg a http server
async fn produce_route_jobs(storage: &mut RedisStorage<Email>) -> Result<()> {
    storage
        .push(Email {
            to: "test@example.com".to_string(),
        })
        .await?;
}

Stepped Tasks

Apalis has beta support for stepped tasks. See complete example

#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
    std::env::set_var("RUST_LOG", "debug");
    tracing_subscriber::fmt::init();
    let redis_url = std::env::var("REDIS_URL").expect("Missing env variable REDIS_URL");
    let conn = apalis_redis::connect(redis_url).await.unwrap();
    let config = apalis_redis::Config::default().set_namespace("stepped-email-task");


    let mut storage = RedisStorage::new_with_config(conn, config);
    storage
        .start_stepped(WelcomeEmail { user_id: 1 })
        .await
        .unwrap();

    // Build steps
    let steps = StepBuilder::new()
        .step_fn(welcome) // Steps are tower services
        .step_fn(campaign)
        .step_fn(complete_campaign);

    WorkerBuilder::new("tasty-banana")
        .data(()) // Shared data for all steps
        .enable_tracing()
        .concurrency(2)
        .backend(storage)
        .build_stepped(steps)
        .on_event(|e| info!("{e}"))
        .run()
        .await;
    Ok(())
}

Feature flags

  • tracing (enabled by default) — Support Tracing 👀
  • sentry — Support for Sentry exception and performance monitoring
  • prometheus — Support Prometheus metrics
  • retry — Support direct retrying jobs
  • timeout — Support timeouts on jobs
  • limit — 💪 Support for concurrency and rate-limiting
  • filter — Support filtering jobs based on a predicate
  • catch-panic - Catch panics that occur during execution

Storage Comparison

Since we provide a few storage solutions, here is a table comparing them:

Feature Redis Sqlite Postgres Sled Mysql Mongo Cron
Scheduled jobs x x
Retry jobs x x
Persistence x x BYO
Rerun Dead jobs x x x

How apalis works

Here is a basic example of how the core parts integrate

sequenceDiagram
    participant App
    participant Worker
    participant Backend

    App->>+Backend: Add job to queue
    Backend-->>+Worker: Job data
    Worker->>+Backend: Update job status to 'Running'
    Worker->>+App: Started job
    loop job execution
        Worker-->>-App: Report job progress
    end
    Worker->>+Backend: Update job status to 'completed'

External examples

Projects using apalis

  • Ryot: A self hosted platform for tracking various facets of your life - media, fitness etc.
  • Summarizer: Podcast summarizer
  • Universal Inbox: Universal Inbox is a solution that centralizes all your notifications and tasks in one place to create a unique inbox.
  • Hatsu: Self-hosted and fully-automated ActivityPub bridge for static sites.

Resources

Web UI

If you are running apalis Board, you can easily manage your jobs. See a working rest API example here

Thanks to

  • [tower] - Tower is a library of modular and reusable components for building robust networking clients and servers.
  • redis-rs - Redis library for rust
  • sqlx - The Rust SQL Toolkit
  • cron - A cron expression parser and schedule explorer

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Extension points exported contracts — how you extend this code

FromRequest (Interface)
Handles extraction [9 implementers]
packages/apalis-core/src/service_fn.rs
MakeSpan (Interface)
Trait used to generate [`Span`]s from requests. [`Trace`] wraps all request handling in this span. [`Span`]: tracing::S [4 …
src/layers/tracing/make_span.rs
Ack (Interface)
A trait for acknowledging successful processing This trait is called even when a task fails. This is a way of a [`Backen [6 …
packages/apalis-core/src/layers.rs
OnRequest (Interface)
Trait used to tell [`Trace`] what to do when a request is received. See the [module docs](../trace/index.html#on_reques [3 …
src/layers/tracing/on_request.rs
Backend (Interface)
A backend represents a task source Both [`Storage`] and [`MessageQueue`] need to implement it for workers to be able to [9 …
packages/apalis-core/src/backend.rs
OnFailure (Interface)
Trait used to tell [`Trace`] what to do when a request fails. See the [module docs](../trace/index.html#on_failure) for [3 …
src/layers/tracing/on_failure.rs
Codec (Interface)
A codec allows backends to encode and decode data [5 implementers]
packages/apalis-core/src/codec/mod.rs
OnResponse (Interface)
Trait used to tell [`Trace`] what to do when a response has been produced. See the [module docs](../trace/index.html#on [3 …
src/layers/tracing/on_response.rs

Core symbols most depended-on inside this repo

map_err
called by 74
src/layers/mod.rs
clone
called by 57
packages/apalis-sql/src/mysql.rs
map
called by 52
packages/apalis-core/src/response.rs
clone
called by 38
packages/apalis-core/src/worker/mod.rs
push
called by 34
packages/apalis-core/src/worker/call_all.rs
sleep
called by 32
packages/apalis-core/src/lib.rs
clone
called by 32
examples/redis-mq-example/src/main.rs
clone
called by 30
packages/apalis-redis/src/storage.rs

Shape

Method 500
Function 153
Class 110
Interface 22
Enum 17

Languages

Rust100%

Modules by API surface

packages/apalis-redis/src/storage.rs73 symbols
packages/apalis-sql/src/postgres.rs50 symbols
packages/apalis-core/src/worker/mod.rs49 symbols
packages/apalis-sql/src/sqlite.rs43 symbols
packages/apalis-sql/src/mysql.rs41 symbols
packages/apalis-core/src/step/mod.rs24 symbols
packages/apalis-core/src/data.rs24 symbols
packages/apalis-sql/src/context.rs20 symbols
packages/apalis-sql/src/lib.rs19 symbols
src/layers/mod.rs18 symbols
src/layers/catch_panic/mod.rs17 symbols
src/layers/tracing/mod.rs16 symbols

Datastores touched

(mysql)Database · 1 repos

For agents

$ claude mcp add apalis \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact