diff --git a/Cargo.lock b/Cargo.lock index 69e882b..3fafee9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 70155d1..bf1ab10 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -42,4 +44,5 @@ futures-executor = "0.3" error-stack = "0.4" jsonwebtoken = "9.3.0" lazy_static = "1.4.0" -mimalloc = "0.1.42" \ No newline at end of file +mimalloc = "0.1.42" +tokio-cron-scheduler = "0.10.2" \ No newline at end of file diff --git a/job/Cargo.toml b/job/Cargo.toml new file mode 100644 index 0000000..6bd68ce --- /dev/null +++ b/job/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "job" +version = "0.1.0" +edition = "2021" + +[dependencies] +tokio-cron-scheduler = { workspace = true } +tracing = { workspace = true } \ No newline at end of file diff --git a/job/src/lib.rs b/job/src/lib.rs new file mode 100644 index 0000000..44ac301 --- /dev/null +++ b/job/src/lib.rs @@ -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(()) +} \ No newline at end of file diff --git a/job/src/task/google_iap.rs b/job/src/task/google_iap.rs new file mode 100644 index 0000000..a6621f8 --- /dev/null +++ b/job/src/task/google_iap.rs @@ -0,0 +1,7 @@ +use tokio_cron_scheduler::{Job, JobSchedulerError}; + +pub fn get_task() -> Result { + Job::new("0/10 * * * * ?", |_uuid, _l| { + tracing::info!("定时任务执行中..."); + }) +} diff --git a/job/src/task/mod.rs b/job/src/task/mod.rs new file mode 100644 index 0000000..27deab2 --- /dev/null +++ b/job/src/task/mod.rs @@ -0,0 +1 @@ +pub mod google_iap; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 0581735..db4db42 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; }