添加定时任务
This commit is contained in:
parent
6226de6dbb
commit
80caca2dc8
46
Cargo.lock
generated
46
Cargo.lock
generated
@ -302,6 +302,7 @@ name = "chuanyue-service"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"api",
|
"api",
|
||||||
|
"job",
|
||||||
"library",
|
"library",
|
||||||
"mimalloc",
|
"mimalloc",
|
||||||
"tokio",
|
"tokio",
|
||||||
@ -362,6 +363,17 @@ version = "2.4.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "19d374276b40fb8bbdee95aef7c7fa6b5316ec764510eb64b8dd0e2ed0d7e7f5"
|
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]]
|
[[package]]
|
||||||
name = "crossbeam-channel"
|
name = "crossbeam-channel"
|
||||||
version = "0.5.12"
|
version = "0.5.12"
|
||||||
@ -1019,6 +1031,14 @@ version = "1.0.10"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
|
checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "job"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"tokio-cron-scheduler",
|
||||||
|
"tracing",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "js-sys"
|
name = "js-sys"
|
||||||
version = "0.3.69"
|
version = "0.3.69"
|
||||||
@ -1298,6 +1318,17 @@ version = "0.1.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"
|
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]]
|
[[package]]
|
||||||
name = "num-integer"
|
name = "num-integer"
|
||||||
version = "0.1.46"
|
version = "0.1.46"
|
||||||
@ -2482,6 +2513,21 @@ dependencies = [
|
|||||||
"windows-sys 0.48.0",
|
"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]]
|
[[package]]
|
||||||
name = "tokio-macros"
|
name = "tokio-macros"
|
||||||
version = "2.2.0"
|
version = "2.2.0"
|
||||||
|
@ -4,15 +4,17 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[workspace]
|
[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
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tokio = { workspace = true, features = ["full"]}
|
tokio = { workspace = true, features = ["full"]}
|
||||||
|
mimalloc = { workspace = true }
|
||||||
|
|
||||||
api = { path = "api" }
|
api = { path = "api" }
|
||||||
library = { path = "library" }
|
library = { path = "library" }
|
||||||
mimalloc = { workspace = true }
|
job = { path = "job" }
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
tokio = "1.36"
|
tokio = "1.36"
|
||||||
@ -42,4 +44,5 @@ futures-executor = "0.3"
|
|||||||
error-stack = "0.4"
|
error-stack = "0.4"
|
||||||
jsonwebtoken = "9.3.0"
|
jsonwebtoken = "9.3.0"
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
mimalloc = "0.1.42"
|
mimalloc = "0.1.42"
|
||||||
|
tokio-cron-scheduler = "0.10.2"
|
8
job/Cargo.toml
Normal file
8
job/Cargo.toml
Normal 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
30
job/src/lib.rs
Normal 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(())
|
||||||
|
}
|
7
job/src/task/google_iap.rs
Normal file
7
job/src/task/google_iap.rs
Normal 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
1
job/src/task/mod.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod google_iap;
|
@ -9,5 +9,6 @@ async fn main() {
|
|||||||
let (_std_guard, _file_guard) = library::core::logger::init_log(config!());
|
let (_std_guard, _file_guard) = library::core::logger::init_log(config!());
|
||||||
library::core::db::init_database(config!()).await;
|
library::core::db::init_database(config!()).await;
|
||||||
|
|
||||||
|
job::start().await;
|
||||||
api::serve().await;
|
api::serve().await;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user