diff --git a/Cargo.lock b/Cargo.lock index 06b1c59..31dba14 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -292,7 +292,6 @@ dependencies = [ name = "chuanyue-service" version = "0.1.0" dependencies = [ - "job", "library", "mimalloc", "system", @@ -1019,14 +1018,6 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" -[[package]] -name = "job" -version = "0.1.0" -dependencies = [ - "tokio-cron-scheduler", - "tracing", -] - [[package]] name = "js-sys" version = "0.3.69" @@ -1109,6 +1100,7 @@ dependencies = [ "sqlx-postgres", "thiserror", "tokio", + "tokio-cron-scheduler", "toml", "tracing", "tracing-appender", @@ -2397,6 +2389,7 @@ dependencies = [ "reqwest", "sqlx", "tokio", + "tokio-cron-scheduler", "tower-http", "tracing", "validator", diff --git a/Cargo.toml b/Cargo.toml index db6227f..3315450 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [workspace] -members = [".", "system", "domain", "i18n", "job","library"] +members = [".", "system", "domain", "i18n","library"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -14,7 +14,6 @@ mimalloc = { workspace = true } system = { path = "system" } library = { path = "library" } -job = { path = "job" } [workspace.dependencies] tokio = "1.36" diff --git a/job/Cargo.toml b/job/Cargo.toml deleted file mode 100644 index 6bd68ce..0000000 --- a/job/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[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/task/google_iap.rs b/job/src/task/google_iap.rs deleted file mode 100644 index 223e1b4..0000000 --- a/job/src/task/google_iap.rs +++ /dev/null @@ -1,7 +0,0 @@ -use tokio_cron_scheduler::{Job, JobSchedulerError}; - -pub fn get_task() -> Result { - Job::new("0 0 0/1 * * ?", |_uuid, _l| { - tracing::info!("定时任务执行中..."); - }) -} diff --git a/job/src/task/mod.rs b/job/src/task/mod.rs deleted file mode 100644 index 27deab2..0000000 --- a/job/src/task/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod google_iap; \ No newline at end of file diff --git a/library/Cargo.toml b/library/Cargo.toml index d28edce..c550e19 100644 --- a/library/Cargo.toml +++ b/library/Cargo.toml @@ -31,6 +31,7 @@ lazy_static = { workspace = true } hmac = { workspace = true } sha2 = { workspace = true } hex-literal = { workspace = true } +tokio-cron-scheduler = { workspace = true } domain = { path = "../domain" } i18n = { path = "../i18n" } \ No newline at end of file diff --git a/library/src/lib.rs b/library/src/lib.rs index 3ecd36f..0c64fae 100644 --- a/library/src/lib.rs +++ b/library/src/lib.rs @@ -6,4 +6,5 @@ pub mod middleware; pub mod token; pub mod social; pub mod cache; -pub mod context; \ No newline at end of file +pub mod context; +pub mod task; \ No newline at end of file diff --git a/job/src/lib.rs b/library/src/task.rs similarity index 51% rename from job/src/lib.rs rename to library/src/task.rs index eecc6cb..a973b4f 100644 --- a/job/src/lib.rs +++ b/library/src/task.rs @@ -1,22 +1,27 @@ -use task::google_iap; -use tokio_cron_scheduler::{JobScheduler, JobSchedulerError}; +use tokio_cron_scheduler::{Job, JobScheduler, JobSchedulerError}; -pub(crate) mod task; +pub struct Task { + pub name: String, + pub job: Box Result + Send + Sync>, + pub interval: String +} /// 启动定时任务 -pub async fn start() { - match schedule().await { +pub async fn start(tasks: Vec) { + match schedule(tasks).await { Ok(_) => tracing::info!("定时任务启动成功"), Err(err) => tracing::error!(error = ?err, "定时任务启动失败"), } } -async fn schedule() -> Result<(), JobSchedulerError> { +async fn schedule(tasks: Vec) -> Result<(), JobSchedulerError> { let mut scheduler = JobScheduler::new().await?; scheduler.init().await?; // 添加任务 - scheduler.add(google_iap::get_task()?).await?; + for task in tasks { + let _task_uuid = scheduler.add((&task.job)(task.name, task.interval).unwrap()).await?; + } // 添加关闭监听 scheduler.set_shutdown_handler(Box::new(|| { @@ -24,8 +29,8 @@ async fn schedule() -> Result<(), JobSchedulerError> { tracing::info!("定时任务已关闭"); }) })); - + // 启动任务 scheduler.start().await?; Ok(()) -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index df13deb..efb6782 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,5 @@ async fn main() { let (_std_guard, _file_guard) = library::core::logger::init_log(config!()); library::core::db::init_database(config!()).await; - job::start().await; system::serve().await; } diff --git a/system/Cargo.toml b/system/Cargo.toml index 8764bf3..f5bd7e6 100644 --- a/system/Cargo.toml +++ b/system/Cargo.toml @@ -19,6 +19,7 @@ error-stack = { workspace = true } sqlx = { workspace = true, features = ["uuid"] } moka = { workspace = true, features = ["future", "logging"] } lazy_static = { workspace = true } +tokio-cron-scheduler = { workspace = true } library = { path = "../library" } domain = { path = "../domain" } diff --git a/system/src/lib.rs b/system/src/lib.rs index bb2792f..400cbaa 100644 --- a/system/src/lib.rs +++ b/system/src/lib.rs @@ -1,8 +1,10 @@ -use library::config; +use library::{config, task}; +use tasks::get_tasks; mod controller; mod router; mod service; +mod tasks; /// 启动服务 pub async fn serve() { @@ -12,5 +14,6 @@ pub async fn serve() { tracing::info!("服务监听地址: {}", addr); + task::start(get_tasks()).await; axum::serve(listener, router::init()).await.unwrap(); } diff --git a/system/src/tasks/google_iap.rs b/system/src/tasks/google_iap.rs new file mode 100644 index 0000000..d1faf7e --- /dev/null +++ b/system/src/tasks/google_iap.rs @@ -0,0 +1,10 @@ +use tokio_cron_scheduler::{Job, JobSchedulerError}; + +pub fn get_task(name: String, cron: String) -> Result { + tracing::info!("添加定时任务: {}", name); + Job::new_async(cron.as_str(), |_uuid, _l| { + Box::pin(async move { + tracing::info!("定时任务执行中"); + }) + }) +} diff --git a/system/src/tasks/mod.rs b/system/src/tasks/mod.rs new file mode 100644 index 0000000..f3f8445 --- /dev/null +++ b/system/src/tasks/mod.rs @@ -0,0 +1,11 @@ +use library::task::Task; + +pub mod google_iap; + +pub fn get_tasks() -> Vec { + vec!(Task { + name: "google_iap".to_string(), + job: Box::new(google_iap::get_task), + interval: "0 0 0/1 * * ?".to_string(), + }) +} \ No newline at end of file