-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add crontab runner to the worker main run function
- Loading branch information
Showing
8 changed files
with
153 additions
and
14 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
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,57 @@ | ||
use std::str::FromStr; | ||
|
||
use archimedes::{WorkerContext, WorkerOptions}; | ||
use serde::Deserialize; | ||
use sqlx::postgres::PgConnectOptions; | ||
use tracing_subscriber::{ | ||
filter::EnvFilter, prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, | ||
}; | ||
|
||
#[derive(Deserialize)] | ||
struct HelloPayload { | ||
message: String, | ||
} | ||
|
||
async fn say_hello(_ctx: WorkerContext, payload: HelloPayload) -> Result<(), ()> { | ||
println!("Hello {} !", payload.message); | ||
Ok(()) | ||
} | ||
|
||
fn enable_logs() { | ||
let fmt_layer = tracing_subscriber::fmt::layer(); | ||
// Log level set to debug except for sqlx set at warn (to not show all sql requests) | ||
let filter_layer = EnvFilter::try_new("debug,sqlx=warn").unwrap(); | ||
|
||
tracing_subscriber::registry() | ||
.with(filter_layer) | ||
.with(fmt_layer) | ||
.init(); | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
enable_logs(); | ||
|
||
let pg_options = PgConnectOptions::from_str("postgres://postgres:root@localhost:5432").unwrap(); | ||
|
||
let pg_pool = sqlx::postgres::PgPoolOptions::new() | ||
.max_connections(5) | ||
.connect_with(pg_options) | ||
.await | ||
.unwrap(); | ||
|
||
WorkerOptions::default() | ||
.concurrency(2) | ||
.schema("example_simple_worker") | ||
.define_job("say_hello", say_hello) | ||
.pg_pool(pg_pool) | ||
// Run say_hello every two minutes | ||
.with_crontab(r#"*/2 * * * * say_hello ?fill=10m {message:"Crontab"}"#) | ||
.unwrap() | ||
.init() | ||
.await | ||
.unwrap() | ||
.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