合并job和library,job具体任务放到service中
This commit is contained in:
parent
f6d7ef8761
commit
acda323a42
11
Cargo.lock
generated
11
Cargo.lock
generated
@ -292,7 +292,6 @@ dependencies = [
|
|||||||
name = "chuanyue-service"
|
name = "chuanyue-service"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"job",
|
|
||||||
"library",
|
"library",
|
||||||
"mimalloc",
|
"mimalloc",
|
||||||
"system",
|
"system",
|
||||||
@ -1019,14 +1018,6 @@ version = "1.0.11"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
|
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
|
||||||
|
|
||||||
[[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"
|
||||||
@ -1109,6 +1100,7 @@ dependencies = [
|
|||||||
"sqlx-postgres",
|
"sqlx-postgres",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
"tokio-cron-scheduler",
|
||||||
"toml",
|
"toml",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-appender",
|
"tracing-appender",
|
||||||
@ -2397,6 +2389,7 @@ dependencies = [
|
|||||||
"reqwest",
|
"reqwest",
|
||||||
"sqlx",
|
"sqlx",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
"tokio-cron-scheduler",
|
||||||
"tower-http",
|
"tower-http",
|
||||||
"tracing",
|
"tracing",
|
||||||
"validator",
|
"validator",
|
||||||
|
@ -4,7 +4,7 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[workspace]
|
[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
|
# 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" }
|
system = { path = "system" }
|
||||||
library = { path = "library" }
|
library = { path = "library" }
|
||||||
job = { path = "job" }
|
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
tokio = "1.36"
|
tokio = "1.36"
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "job"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
tokio-cron-scheduler = { workspace = true }
|
|
||||||
tracing = { workspace = true }
|
|
@ -1,7 +0,0 @@
|
|||||||
use tokio_cron_scheduler::{Job, JobSchedulerError};
|
|
||||||
|
|
||||||
pub fn get_task() -> Result<Job, JobSchedulerError> {
|
|
||||||
Job::new("0 0 0/1 * * ?", |_uuid, _l| {
|
|
||||||
tracing::info!("定时任务执行中...");
|
|
||||||
})
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
pub mod google_iap;
|
|
@ -31,6 +31,7 @@ lazy_static = { workspace = true }
|
|||||||
hmac = { workspace = true }
|
hmac = { workspace = true }
|
||||||
sha2 = { workspace = true }
|
sha2 = { workspace = true }
|
||||||
hex-literal = { workspace = true }
|
hex-literal = { workspace = true }
|
||||||
|
tokio-cron-scheduler = { workspace = true }
|
||||||
|
|
||||||
domain = { path = "../domain" }
|
domain = { path = "../domain" }
|
||||||
i18n = { path = "../i18n" }
|
i18n = { path = "../i18n" }
|
@ -6,4 +6,5 @@ pub mod middleware;
|
|||||||
pub mod token;
|
pub mod token;
|
||||||
pub mod social;
|
pub mod social;
|
||||||
pub mod cache;
|
pub mod cache;
|
||||||
pub mod context;
|
pub mod context;
|
||||||
|
pub mod task;
|
@ -1,22 +1,27 @@
|
|||||||
use task::google_iap;
|
use tokio_cron_scheduler::{Job, JobScheduler, JobSchedulerError};
|
||||||
use tokio_cron_scheduler::{JobScheduler, JobSchedulerError};
|
|
||||||
|
|
||||||
pub(crate) mod task;
|
pub struct Task {
|
||||||
|
pub name: String,
|
||||||
|
pub job: Box<dyn Fn(String, String) -> Result<Job, JobSchedulerError> + Send + Sync>,
|
||||||
|
pub interval: String
|
||||||
|
}
|
||||||
|
|
||||||
/// 启动定时任务
|
/// 启动定时任务
|
||||||
pub async fn start() {
|
pub async fn start(tasks: Vec<Task>) {
|
||||||
match schedule().await {
|
match schedule(tasks).await {
|
||||||
Ok(_) => tracing::info!("定时任务启动成功"),
|
Ok(_) => tracing::info!("定时任务启动成功"),
|
||||||
Err(err) => tracing::error!(error = ?err, "定时任务启动失败"),
|
Err(err) => tracing::error!(error = ?err, "定时任务启动失败"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn schedule() -> Result<(), JobSchedulerError> {
|
async fn schedule(tasks: Vec<Task>) -> Result<(), JobSchedulerError> {
|
||||||
let mut scheduler = JobScheduler::new().await?;
|
let mut scheduler = JobScheduler::new().await?;
|
||||||
scheduler.init().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(|| {
|
scheduler.set_shutdown_handler(Box::new(|| {
|
||||||
@ -24,8 +29,8 @@ async fn schedule() -> Result<(), JobSchedulerError> {
|
|||||||
tracing::info!("定时任务已关闭");
|
tracing::info!("定时任务已关闭");
|
||||||
})
|
})
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// 启动任务
|
// 启动任务
|
||||||
scheduler.start().await?;
|
scheduler.start().await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
@ -9,6 +9,5 @@ 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;
|
|
||||||
system::serve().await;
|
system::serve().await;
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ error-stack = { workspace = true }
|
|||||||
sqlx = { workspace = true, features = ["uuid"] }
|
sqlx = { workspace = true, features = ["uuid"] }
|
||||||
moka = { workspace = true, features = ["future", "logging"] }
|
moka = { workspace = true, features = ["future", "logging"] }
|
||||||
lazy_static = { workspace = true }
|
lazy_static = { workspace = true }
|
||||||
|
tokio-cron-scheduler = { workspace = true }
|
||||||
|
|
||||||
library = { path = "../library" }
|
library = { path = "../library" }
|
||||||
domain = { path = "../domain" }
|
domain = { path = "../domain" }
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
use library::config;
|
use library::{config, task};
|
||||||
|
use tasks::get_tasks;
|
||||||
|
|
||||||
mod controller;
|
mod controller;
|
||||||
mod router;
|
mod router;
|
||||||
mod service;
|
mod service;
|
||||||
|
mod tasks;
|
||||||
|
|
||||||
/// 启动服务
|
/// 启动服务
|
||||||
pub async fn serve() {
|
pub async fn serve() {
|
||||||
@ -12,5 +14,6 @@ pub async fn serve() {
|
|||||||
|
|
||||||
tracing::info!("服务监听地址: {}", addr);
|
tracing::info!("服务监听地址: {}", addr);
|
||||||
|
|
||||||
|
task::start(get_tasks()).await;
|
||||||
axum::serve(listener, router::init()).await.unwrap();
|
axum::serve(listener, router::init()).await.unwrap();
|
||||||
}
|
}
|
||||||
|
10
system/src/tasks/google_iap.rs
Normal file
10
system/src/tasks/google_iap.rs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
use tokio_cron_scheduler::{Job, JobSchedulerError};
|
||||||
|
|
||||||
|
pub fn get_task(name: String, cron: String) -> Result<Job, JobSchedulerError> {
|
||||||
|
tracing::info!("添加定时任务: {}", name);
|
||||||
|
Job::new_async(cron.as_str(), |_uuid, _l| {
|
||||||
|
Box::pin(async move {
|
||||||
|
tracing::info!("定时任务执行中");
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
11
system/src/tasks/mod.rs
Normal file
11
system/src/tasks/mod.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
use library::task::Task;
|
||||||
|
|
||||||
|
pub mod google_iap;
|
||||||
|
|
||||||
|
pub fn get_tasks() -> Vec<Task> {
|
||||||
|
vec!(Task {
|
||||||
|
name: "google_iap".to_string(),
|
||||||
|
job: Box::new(google_iap::get_task),
|
||||||
|
interval: "0 0 0/1 * * ?".to_string(),
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user