-
-
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.
feat: standardize cron as backend (#359)
- Loading branch information
1 parent
fc72bc7
commit 7159edb
Showing
9 changed files
with
95 additions
and
12 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,23 @@ | ||
[package] | ||
name = "cron" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
anyhow = "1" | ||
apalis = { path = "../../", default-features = false, features = [ | ||
"tokio-comp", | ||
"tracing", | ||
"limit", | ||
] } | ||
apalis-cron = { path = "../../packages/apalis-cron" } | ||
tokio = { version = "1", features = ["full"] } | ||
serde = "1" | ||
tracing-subscriber = "0.3.11" | ||
chrono = { version = "0.4", default-features = false, features = ["clock"] } | ||
pin-project-lite = "0.2.9" | ||
tower = { version = "0.4", features = ["load-shed"] } | ||
|
||
[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,44 @@ | ||
use apalis::prelude::*; | ||
use apalis::utils::TokioExecutor; | ||
use apalis_cron::CronStream; | ||
use apalis_cron::Schedule; | ||
use chrono::{DateTime, Utc}; | ||
use std::str::FromStr; | ||
use std::time::Duration; | ||
use tower::limit::RateLimitLayer; | ||
use tower::load_shed::LoadShedLayer; | ||
|
||
#[derive(Clone)] | ||
struct FakeService; | ||
impl FakeService { | ||
fn execute(&self, item: Reminder) { | ||
dbg!(&item.0); | ||
} | ||
} | ||
|
||
#[derive(Default, Debug, Clone)] | ||
struct Reminder(DateTime<Utc>); | ||
impl From<DateTime<Utc>> for Reminder { | ||
fn from(t: DateTime<Utc>) -> Self { | ||
Reminder(t) | ||
} | ||
} | ||
async fn send_reminder(job: Reminder, svc: Data<FakeService>) { | ||
svc.execute(job); | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let schedule = Schedule::from_str("1/1 * * * * *").unwrap(); | ||
let worker = WorkerBuilder::new("morning-cereal") | ||
.layer(LoadShedLayer::new()) // Important when you have layers that block the service | ||
.layer(RateLimitLayer::new(1, Duration::from_secs(2))) | ||
.data(FakeService) | ||
.backend(CronStream::new(schedule)) | ||
.build_fn(send_reminder); | ||
Monitor::<TokioExecutor>::new() | ||
.register(worker) | ||
.run() | ||
.await | ||
.unwrap(); | ||
} |
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