合并job和library,job具体任务放到service中

This commit is contained in:
李运家 2024-09-23 15:41:51 +08:00
parent f6d7ef8761
commit acda323a42
13 changed files with 46 additions and 39 deletions

11
Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

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

View File

@ -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!("定时任务执行中...");
})
}

View File

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

View File

@ -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" }

View File

@ -7,3 +7,4 @@ pub mod token;
pub mod social;
pub mod cache;
pub mod context;
pub mod task;

View File

@ -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<dyn Fn(String, String) -> Result<Job, JobSchedulerError> + Send + Sync>,
pub interval: String
}
/// 启动定时任务
pub async fn start() {
match schedule().await {
pub async fn start(tasks: Vec<Task>) {
match schedule(tasks).await {
Ok(_) => tracing::info!("定时任务启动成功"),
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?;
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(|| {

View File

@ -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;
}

View File

@ -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" }

View File

@ -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();
}

View 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
View 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(),
})
}