-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stepped tasks: adds ability to write stepped tasks
- Loading branch information
1 parent
d563db3
commit 05ff8b0
Showing
18 changed files
with
423 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "stepped-tasks" | ||
version = "0.1.0" | ||
edition.workspace = true | ||
repository.workspace = true | ||
|
||
[dependencies] | ||
tower = { version = "0.5", features = ["util"] } | ||
tokio = { version = "1", features = ["full"] } | ||
apalis = { path = "../../", features = ["limit", "catch-panic"] } | ||
apalis-redis = { path = "../../packages/apalis-redis" } | ||
# apalis-sql = { path = "../../packages/apalis-sql", features = [ | ||
# "sqlite", | ||
# "tokio-comp", | ||
# ] } | ||
serde = "1" | ||
serde_json = "1" | ||
tracing-subscriber = "0.3.11" | ||
futures = "0.3" | ||
[dependencies.tracing] | ||
default-features = false | ||
version = "0.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use std::time::Duration; | ||
|
||
use apalis::prelude::*; | ||
use apalis_redis::RedisStorage; | ||
use serde::{Deserialize, Serialize}; | ||
use tracing::info; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone)] | ||
struct WelcomeEmail { | ||
user_id: usize, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone)] | ||
|
||
struct FirstWeekEmail { | ||
user_id: usize, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone)] | ||
|
||
struct FirstMonthEmail { | ||
user_id: usize, | ||
} | ||
|
||
async fn welcome(req: WelcomeEmail, ctx: Data<()>) -> Result<GoTo<FirstWeekEmail>, Error> { | ||
Ok::<_, _>(GoTo::Next(FirstWeekEmail { | ||
user_id: req.user_id + 1, | ||
})) | ||
} | ||
|
||
async fn first_week_email( | ||
req: FirstWeekEmail, | ||
ctx: Data<()>, | ||
) -> Result<GoTo<FirstMonthEmail>, Error> { | ||
Ok::<_, _>(GoTo::Delay { | ||
next: FirstMonthEmail { | ||
user_id: req.user_id + 1, | ||
}, | ||
delay: Duration::from_secs(10), | ||
}) | ||
} | ||
|
||
async fn first_month_email(req: FirstMonthEmail, ctx: Data<()>) -> Result<GoTo<()>, Error> { | ||
Ok::<_, _>(GoTo::Done) | ||
} | ||
|
||
async fn produce_jobs(storage: &mut RedisStorage<StepRequest<Vec<u8>>>) { | ||
storage | ||
.push(StepRequest { | ||
current: 0, | ||
inner: serde_json::to_vec(&WelcomeEmail { user_id: 1 }).unwrap(), | ||
}) | ||
.await | ||
.unwrap(); | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), std::io::Error> { | ||
std::env::set_var("RUST_LOG", "debug,sqlx::query=error"); | ||
tracing_subscriber::fmt::init(); | ||
let conn = apalis_redis::connect("redis://127.0.0.1/").await.unwrap(); | ||
let config = apalis_redis::Config::default() | ||
.set_namespace("apalis_redis-with-msg-pack") | ||
.set_max_retries(5); | ||
|
||
let mut storage = RedisStorage::new_with_config(conn, config); | ||
produce_jobs(&mut storage).await; | ||
|
||
// Build steps | ||
let steps = StepBuilder::new() | ||
.step_fn(welcome) | ||
.step_fn(first_week_email) | ||
.step_fn(first_month_email); | ||
|
||
WorkerBuilder::new("tasty-banana") | ||
.data(()) | ||
.enable_tracing() | ||
.concurrency(2) | ||
.backend(storage) | ||
.build_steps(steps) | ||
.on_event(|e| info!("{e}")) | ||
.run() | ||
.await; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.