Crates.io | apalis |
lib.rs | apalis |
version | 0.6.0-rc.8 |
source | src |
created_at | 2021-05-14 15:35:04.16062 |
updated_at | 2024-10-11 04:13:16.155867 |
description | Simple, extensible multithreaded background job processing for Rust |
homepage | |
repository | https://github.com/geofmureithi/apalis |
max_upload_size | |
id | 397435 |
size | 75,438 |
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 |
To get started, just add to Cargo.toml
[dependencies]
apalis = { version = "0.6" }
apalis-redis = { version = "0.6" }
# apalis-sql = { version = "0.6", features = ["postgres"] } # or mysql, sqlite
use apalis::prelude::*;
use apalis_redis::{RedisStorage, Config};
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)]
struct Email {
to: String,
}
/// A function that will be converted into a service.
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);
Monitor::new()
.register_with_count(2, {
WorkerBuilder::new(format!("email-worker"))
.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: &RedisStorage<Email>) -> Result<()> {
let mut storage = storage.clone();
storage
.push(Email {
to: "test@example.com".to_string(),
})
.await?;
}
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 |
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'
If you are running apalis Board, you can easily manage your jobs. See a working rest API example here
v 0.5
v 0.4
v 0.3
v 0.2
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
We use SemVer for versioning. For the versions available, see the tags on this repository.
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE.md file for details