1
0
mirror of https://github.com/actix/examples synced 2024-11-27 16:02:57 +01:00

Chore: Update apalis to the latest version

This commit is contained in:
geofmureithi 2024-07-12 22:41:00 +03:00
parent 42acb27186
commit 642fbc84ad
No known key found for this signature in database
GPG Key ID: 0DB5FB3B267761E6
4 changed files with 72 additions and 147 deletions

176
Cargo.lock generated
View File

@ -767,60 +767,43 @@ checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da"
[[package]] [[package]]
name = "apalis" name = "apalis"
version = "0.4.9" version = "0.6.0-rc.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78bbaeebf00817d5aa561515b313ef0d280bf4b92592e4709b21925c1233f613" checksum = "3e78684d69b4361d01bddf318a5117c18179c52396c7792fc0ebb04da46abbb2"
dependencies = [ dependencies = [
"apalis-core", "apalis-core",
"apalis-cron",
"apalis-redis",
"apalis-sql",
]
[[package]]
name = "apalis-core"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1deb48475efcdece1f23a0553209ee842f264c2a5e9bcc4928bfa6a15a044cde"
dependencies = [
"async-stream",
"async-trait",
"chrono",
"futures", "futures",
"graceful-shutdown",
"http 1.0.0",
"log",
"pin-project-lite", "pin-project-lite",
"serde", "serde",
"strum",
"thiserror", "thiserror",
"tokio", "tokio",
"tower", "tower",
"tracing", "tracing",
"tracing-futures", "tracing-futures",
]
[[package]]
name = "apalis-core"
version = "0.6.0-rc.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94c70a31bd587ce6ffe53af31d9693257af40b47f515999b3cf70b8d7353d259"
dependencies = [
"async-oneshot",
"futures",
"futures-timer",
"pin-project-lite",
"serde",
"serde_json",
"thiserror",
"tower",
"ulid", "ulid",
] ]
[[package]]
name = "apalis-cron"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43310b7e0132f9520b09224fb6faafb32eec82a672aa79c09e46b5b488ed505b"
dependencies = [
"apalis-core",
"async-stream",
"chrono",
"cron",
"futures",
"tokio",
"tower",
]
[[package]] [[package]]
name = "apalis-redis" name = "apalis-redis"
version = "0.4.9" version = "0.6.0-rc.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2abee8225fd875e57b530abbcf2d9c3122c1a2cce905367b67c6410b6f9654d7" checksum = "d2ae52c1baf49b8faa59b2b03d5cb47ebf9bed90411d7b5878bc97600752761c"
dependencies = [ dependencies = [
"apalis-core", "apalis-core",
"async-stream", "async-stream",
@ -828,28 +811,8 @@ dependencies = [
"chrono", "chrono",
"futures", "futures",
"log", "log",
"redis 0.24.0", "redis 0.25.4",
"serde", "serde",
"serde_json",
"tokio",
]
[[package]]
name = "apalis-sql"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5899bfd124e460f1449ffab643f6bac0dc417e7ca234f34c732c923c6a3addbf"
dependencies = [
"apalis-core",
"async-stream",
"async-trait",
"chrono",
"debounced",
"futures",
"futures-lite",
"serde",
"serde_json",
"sqlx",
"tokio", "tokio",
] ]
@ -1048,6 +1011,15 @@ dependencies = [
"serde_json", "serde_json",
] ]
[[package]]
name = "async-oneshot"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae47de2a02d543205f3f5457a90b6ecbc9494db70557bd29590ec8f1ddff5463"
dependencies = [
"futures-micro",
]
[[package]] [[package]]
name = "async-stream" name = "async-stream"
version = "0.3.5" version = "0.3.5"
@ -1614,6 +1586,7 @@ version = "1.0.0"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"apalis", "apalis",
"apalis-redis",
"chrono", "chrono",
"color-eyre", "color-eyre",
"dotenvy", "dotenvy",
@ -2348,17 +2321,6 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "cron"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f8c3e73077b4b4a6ab1ea5047c37c57aee77657bc8ecd6f29b0af082d0b0c07"
dependencies = [
"chrono",
"nom",
"once_cell",
]
[[package]] [[package]]
name = "crossbeam" name = "crossbeam"
version = "0.8.4" version = "0.8.4"
@ -2658,16 +2620,6 @@ dependencies = [
"tokio", "tokio",
] ]
[[package]]
name = "debounced"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3d8b0346b9fa0aa01a3fa4bcce48d62f8738e9c2956e92f275bbf6cf9d6fab5"
dependencies = [
"futures-timer",
"futures-util",
]
[[package]] [[package]]
name = "der" name = "der"
version = "0.6.1" version = "0.6.1"
@ -3327,9 +3279,9 @@ dependencies = [
[[package]] [[package]]
name = "futures" name = "futures"
version = "0.3.29" version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da0290714b38af9b4a7b094b8a37086d1b4e61f2df9122c3cad2577669145335" checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0"
dependencies = [ dependencies = [
"futures-channel", "futures-channel",
"futures-core", "futures-core",
@ -3358,9 +3310,9 @@ checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
[[package]] [[package]]
name = "futures-executor" name = "futures-executor"
version = "0.3.29" version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f4fb8693db0cf099eadcca0efe2a5a22e4550f98ed16aba6c48700da29597bc" checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"futures-task", "futures-task",
@ -3384,19 +3336,6 @@ version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1"
[[package]]
name = "futures-lite"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52527eb5074e35e9339c6b4e8d12600c7128b68fb25dcb9fa9dec18f7c25f3a5"
dependencies = [
"fastrand",
"futures-core",
"futures-io",
"parking",
"pin-project-lite",
]
[[package]] [[package]]
name = "futures-macro" name = "futures-macro"
version = "0.3.30" version = "0.3.30"
@ -3408,6 +3347,15 @@ dependencies = [
"syn 2.0.69", "syn 2.0.69",
] ]
[[package]]
name = "futures-micro"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b460264b3593d68b16a7bc35f7bc226ddfebdf9a1c8db1ed95d5cc6b7168c826"
dependencies = [
"pin-project-lite",
]
[[package]] [[package]]
name = "futures-sink" name = "futures-sink"
version = "0.3.30" version = "0.3.30"
@ -3472,8 +3420,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c" checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"js-sys",
"libc", "libc",
"wasi", "wasi",
"wasm-bindgen",
] ]
[[package]] [[package]]
@ -3542,17 +3492,6 @@ dependencies = [
"spinning_top", "spinning_top",
] ]
[[package]]
name = "graceful-shutdown"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3effbaf774a1da3462925bb182ccf975c284cf46edca5569ea93420a657af484"
dependencies = [
"futures-core",
"pin-project-lite",
"tokio",
]
[[package]] [[package]]
name = "group" name = "group"
version = "0.12.1" version = "0.12.1"
@ -5407,12 +5346,6 @@ dependencies = [
"sha2", "sha2",
] ]
[[package]]
name = "parking"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb813b8af86854136c6922af0598d719255ecb2179515e6e7730d468f05c9cae"
[[package]] [[package]]
name = "parking_lot" name = "parking_lot"
version = "0.11.2" version = "0.11.2"
@ -5659,9 +5592,9 @@ dependencies = [
[[package]] [[package]]
name = "pin-project-lite" name = "pin-project-lite"
version = "0.2.13" version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02"
[[package]] [[package]]
name = "pin-utils" name = "pin-utils"
@ -6033,7 +5966,6 @@ dependencies = [
"percent-encoding", "percent-encoding",
"pin-project-lite", "pin-project-lite",
"ryu", "ryu",
"sha1_smol",
"tokio", "tokio",
"tokio-retry", "tokio-retry",
"tokio-util", "tokio-util",
@ -7205,7 +7137,6 @@ dependencies = [
"atoi", "atoi",
"byteorder", "byteorder",
"bytes", "bytes",
"chrono",
"crc", "crc",
"crossbeam-queue", "crossbeam-queue",
"either", "either",
@ -7269,7 +7200,6 @@ dependencies = [
"sha2", "sha2",
"sqlx-core", "sqlx-core",
"sqlx-mysql", "sqlx-mysql",
"sqlx-postgres",
"sqlx-sqlite", "sqlx-sqlite",
"syn 1.0.109", "syn 1.0.109",
"tempfile", "tempfile",
@ -7288,7 +7218,6 @@ dependencies = [
"bitflags 2.4.1", "bitflags 2.4.1",
"byteorder", "byteorder",
"bytes", "bytes",
"chrono",
"crc", "crc",
"digest", "digest",
"dotenvy", "dotenvy",
@ -7330,7 +7259,6 @@ dependencies = [
"base64 0.21.7", "base64 0.21.7",
"bitflags 2.4.1", "bitflags 2.4.1",
"byteorder", "byteorder",
"chrono",
"crc", "crc",
"dotenvy", "dotenvy",
"etcetera", "etcetera",
@ -7366,7 +7294,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b244ef0a8414da0bed4bb1910426e890b19e5e9bccc27ada6b797d05c55ae0aa" checksum = "b244ef0a8414da0bed4bb1910426e890b19e5e9bccc27ada6b797d05c55ae0aa"
dependencies = [ dependencies = [
"atoi", "atoi",
"chrono",
"flume", "flume",
"futures-channel", "futures-channel",
"futures-core", "futures-core",
@ -8266,7 +8193,6 @@ version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97d095ae15e245a057c8e8451bab9b3ee1e1f68e9ba2b4fbc18d0ac5237835f2" checksum = "97d095ae15e245a057c8e8451bab9b3ee1e1f68e9ba2b4fbc18d0ac5237835f2"
dependencies = [ dependencies = [
"pin-project",
"tracing", "tracing",
] ]
@ -8457,11 +8383,13 @@ checksum = "ed646292ffc8188ef8ea4d1e0e0150fb15a5c2e12ad9b8fc191ae7a8a7f3c4b9"
[[package]] [[package]]
name = "ulid" name = "ulid"
version = "1.1.0" version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e37c4b6cbcc59a8dcd09a6429fbc7890286bcbb79215cea7b38a3c4c0921d93" checksum = "34778c17965aa2a08913b57e1f34db9b4a63f5de31768b55bf20d2795f921259"
dependencies = [ dependencies = [
"getrandom",
"rand", "rand",
"web-time",
] ]
[[package]] [[package]]

View File

@ -6,7 +6,8 @@ edition = "2021"
[dependencies] [dependencies]
actix-web.workspace = true actix-web.workspace = true
apalis = { version = "0.4", features = ["redis"] } apalis = { version = "0.6.0-rc.2" }
apalis-redis = { version = "0.6.0-rc.2" }
chrono.workspace = true chrono.workspace = true
color-eyre.workspace = true color-eyre.workspace = true
dotenvy.workspace = true dotenvy.workspace = true

View File

@ -2,7 +2,8 @@
use std::time::Duration; use std::time::Duration;
use apalis::{prelude::*, redis::RedisStorage}; use apalis::prelude::*;
use apalis_redis::{Config, RedisStorage};
use rand::distributions::{Alphanumeric, DistString as _}; use rand::distributions::{Alphanumeric, DistString as _};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -19,37 +20,31 @@ impl Email {
} }
} }
impl Job for Email { async fn process_email_job(job: Email) {
const NAME: &'static str = "send_email";
}
async fn process_email_job(job: Email, _ctx: JobContext) -> Result<(), JobError> {
log::info!("sending email to {}", &job.to); log::info!("sending email to {}", &job.to);
// simulate time taken to send email // simulate time taken to send email
tokio::time::sleep(rand_delay_with_jitter()).await; tokio::time::sleep(rand_delay_with_jitter()).await;
Ok(()) // Ok(())
} }
pub(crate) async fn start_processing_email_queue() -> eyre::Result<RedisStorage<Email>> { pub(crate) async fn start_processing_email_queue() -> eyre::Result<RedisStorage<Email>> {
let redis_url = std::env::var("REDIS_URL").expect("Missing env variable REDIS_URL"); let redis_url = std::env::var("REDIS_URL").expect("Missing env variable REDIS_URL");
let storage = RedisStorage::connect(redis_url).await?; let conn = apalis_redis::connect(redis_url).await?;
let config = Config::default().set_namespace("send_email");
let storage = RedisStorage::new_with_config(conn, config);
// create job monitor(s) and attach email job handler // create unmonitored workers for handling emails
let monitor = Monitor::new().register_with_count(2, { let workers = WorkerBuilder::new("job-handler")
let storage = storage.clone(); .backend(storage.clone())
move |n| {
WorkerBuilder::new(format!("job-handler-{n}"))
.with_storage(storage.clone())
.build_fn(process_email_job) .build_fn(process_email_job)
} .with_executor_instances(2, TokioExecutor);
});
// spawn job monitor into background for worker in workers {
// the monitor manages itself otherwise so we don't need to return a join handle
#[allow(clippy::let_underscore_future)] #[allow(clippy::let_underscore_future)]
let _ = tokio::spawn(monitor.run()); let _ = tokio::spawn(worker.run());
}
Ok(storage) Ok(storage)
} }

View File

@ -3,7 +3,8 @@ use actix_web::{
web::{self, Data}, web::{self, Data},
HttpResponse, Responder, HttpResponse, Responder,
}; };
use apalis::{prelude::*, redis::RedisStorage}; use apalis::prelude::*;
use apalis_redis::RedisStorage;
use chrono::{TimeDelta, Utc}; use chrono::{TimeDelta, Utc};
use serde::Deserialize; use serde::Deserialize;