Dependencies
= { = "0.0.6" }
API interface
App implements the JobConfigurator feature, which can be used to configure the scheduling task:
#[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 process 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
}
Extract the Component registered by the plugin
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 cron;
use ;
use Component;
async