Crates.io | spring-job |
lib.rs | spring-job |
version | |
source | src |
created_at | 2024-08-07 04:44:09.663062 |
updated_at | 2024-12-11 02:19:27.549183 |
description | Integrate tokio-cron-scheduler with spring-rs framework |
homepage | |
repository | https://github.com/spring-rs/spring-rs |
max_upload_size | |
id | 1328065 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
spring-job = { version = "<version>" }
App implements the JobConfigurator feature, which can be used to configure the scheduling task:
use spring::App;
use spring_job::{cron, JobPlugin, JobConfigurator, Jobs};
use spring_sqlx::SqlxPlugin;
#[tokio::main]
async fn main() {
App::new()
.add_plugin(JobPlugin)
.add_plugin(SqlxPlugin)
.add_jobs(jobs())
.run()
.await
}
fn jobs() -> Jobs {
Jobs::new().typed_job(cron_job)
}
#[cron("1/10 * * * * *")]
async fn cron_job() {
println!("cron scheduled: {:?}", SystemTime::now())
}
You can also use the auto_config
macro to implement automatic configuration. This process macro will automatically register the scheduled tasks marked by the Procedural Macro into the app:
+#[auto_config(JobConfigurator)]
#[tokio::main]
async fn main() {
App::new()
.add_plugin(JobPlugin)
.add_plugin(SqlxPlugin)
- .add_jobs(jobs())
.run()
.await
}
The SqlxPlugin
plugin above automatically registers a Sqlx connection pool component for us. We can use Component
to extract this connection pool from App. It should be noted that although the implementation principles of spring-job
's Component
and spring-web
's Component
are similar, these two extractors belong to different crates.
use spring_sqlx::{
sqlx::{self, Row}, ConnectPool
};
use spring_job::cron;
use spring_job::extractor::Component;
#[cron("1/10 * * * * *")]
async fn cron_job(Component(db): Component<ConnectPool>) {
let time: String = sqlx::query("select DATE_FORMAT(now(),'%Y-%m-%d %H:%i:%s') as time")
.fetch_one(&db)
.await
.context("query failed")
.unwrap()
.get("time");
println!("cron scheduled: {:?}", time)
}
You can use Config
to extract the configuration in toml. The usage is exactly the same as spring-web
.
#[derive(Debug, Configurable, Deserialize)]
#[config_prefix = "custom"]
struct CustomConfig {
a: u32,
b: bool,
}
#[cron("1/10 * * * * *")]
async fn use_toml_config(Config(conf): Config<CustomConfig>) -> impl IntoResponse {
format!("a={}, b={}", conf.a, conf.b)
}
Add the corresponding configuration to your configuration file:
[custom]
a = 1
b = true