添加定时任务

This commit is contained in:
李运家 2024-06-27 15:28:25 +08:00
parent 6226de6dbb
commit 80caca2dc8
7 changed files with 99 additions and 3 deletions

46
Cargo.lock generated
View File

@ -302,6 +302,7 @@ name = "chuanyue-service"
version = "0.1.0"
dependencies = [
"api",
"job",
"library",
"mimalloc",
"tokio",
@ -362,6 +363,17 @@ version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19d374276b40fb8bbdee95aef7c7fa6b5316ec764510eb64b8dd0e2ed0d7e7f5"
[[package]]
name = "cron"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f8c3e73077b4b4a6ab1ea5047c37c57aee77657bc8ecd6f29b0af082d0b0c07"
dependencies = [
"chrono",
"nom",
"once_cell",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.12"
@ -1019,6 +1031,14 @@ version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
[[package]]
name = "job"
version = "0.1.0"
dependencies = [
"tokio-cron-scheduler",
"tracing",
]
[[package]]
name = "js-sys"
version = "0.3.69"
@ -1298,6 +1318,17 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"
[[package]]
name = "num-derive"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "876a53fff98e03a936a674b29568b0e605f06b29372c2489ff4de23f1949743d"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "num-integer"
version = "0.1.46"
@ -2482,6 +2513,21 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "tokio-cron-scheduler"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4c2e3a88f827f597799cf70a6f673074e62f3fc5ba5993b2873345c618a29af"
dependencies = [
"chrono",
"cron",
"num-derive",
"num-traits",
"tokio",
"tracing",
"uuid",
]
[[package]]
name = "tokio-macros"
version = "2.2.0"

View File

@ -4,15 +4,17 @@ version = "0.1.0"
edition = "2021"
[workspace]
members = [".", "api", "domain", "i18n","library", "service"]
members = [".", "api", "domain", "i18n", "job","library", "service"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tokio = { workspace = true, features = ["full"]}
mimalloc = { workspace = true }
api = { path = "api" }
library = { path = "library" }
mimalloc = { workspace = true }
job = { path = "job" }
[workspace.dependencies]
tokio = "1.36"
@ -43,3 +45,4 @@ error-stack = "0.4"
jsonwebtoken = "9.3.0"
lazy_static = "1.4.0"
mimalloc = "0.1.42"
tokio-cron-scheduler = "0.10.2"

8
job/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "job"
version = "0.1.0"
edition = "2021"
[dependencies]
tokio-cron-scheduler = { workspace = true }
tracing = { workspace = true }

30
job/src/lib.rs Normal file
View File

@ -0,0 +1,30 @@
use task::google_iap;
use tokio_cron_scheduler::{JobScheduler, JobSchedulerError};
pub(crate) mod task;
pub async fn start() {
match schedule().await {
Ok(_) => tracing::info!("定时任务启动成功"),
Err(err) => tracing::error!(error = ?err, "定时任务启动失败"),
}
}
async fn schedule() -> Result<(), JobSchedulerError> {
let mut scheduler = JobScheduler::new().await?;
scheduler.init().await?;
// 添加任务
scheduler.add(google_iap::get_task()?).await?;
// 添加关闭监听
scheduler.set_shutdown_handler(Box::new(|| {
Box::pin(async move {
tracing::info!("定时任务已关闭");
})
}));
// 启动任务
scheduler.start().await?;
Ok(())
}

View File

@ -0,0 +1,7 @@
use tokio_cron_scheduler::{Job, JobSchedulerError};
pub fn get_task() -> Result<Job, JobSchedulerError> {
Job::new("0/10 * * * * ?", |_uuid, _l| {
tracing::info!("定时任务执行中...");
})
}

1
job/src/task/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod google_iap;

View File

@ -9,5 +9,6 @@ async fn main() {
let (_std_guard, _file_guard) = library::core::logger::init_log(config!());
library::core::db::init_database(config!()).await;
job::start().await;
api::serve().await;
}