From cbc831bb70c71d5ed6dbc8f1d2875ac98d9ceb09 Mon Sep 17 00:00:00 2001 From: Lumither Tao Date: Wed, 27 Nov 2024 04:00:38 -0800 Subject: [PATCH] fix(cron::update) fix several errors --- .../src/blog/cron/check_update/index.rs | 26 ++++++++++++++++--- .../src/blog/cron/check_update/mod.rs | 6 +++-- backend/api_server/src/blog/cron/mod.rs | 2 +- backend/markdown/Cargo.toml | 1 - 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/backend/api_server/src/blog/cron/check_update/index.rs b/backend/api_server/src/blog/cron/check_update/index.rs index 8bfe006..f391639 100644 --- a/backend/api_server/src/blog/cron/check_update/index.rs +++ b/backend/api_server/src/blog/cron/check_update/index.rs @@ -295,10 +295,28 @@ $$ LANGUAGE plpgsql; pub async fn index_file(path: &Path, db_conn: &Pool) -> Result<(), Box> { let md_file = MdFile::from_file(path)?; - base(db_conn, &md_file).await?; - tag(db_conn, &md_file).await?; - meta(db_conn, &md_file).await?; - hash(db_conn, &md_file).await?; + async fn trail(db_conn: &Pool, md_file: &MdFile) -> Result<(), PostIdxError> { + base(db_conn, md_file).await?; + tag(db_conn, md_file).await?; + meta(db_conn, md_file).await?; + hash(db_conn, md_file).await.map(|_| ()) + } + let mut cnt = 5; + + while let Err(e) = trail(db_conn, &md_file).await { + match e { + PostIdxError::DBWriteFailure { .. } => { + cnt -= 1; + } + PostIdxError::DBReadFailure { .. } => { + cnt -= 1; + } + _ => return Err(e.into()), + } + if cnt <= 0 { + return Err(e.into()); + } + } info!("indexed markdown file: {}", path.display()); Ok(()) diff --git a/backend/api_server/src/blog/cron/check_update/mod.rs b/backend/api_server/src/blog/cron/check_update/mod.rs index 63ca30e..9568ae2 100644 --- a/backend/api_server/src/blog/cron/check_update/mod.rs +++ b/backend/api_server/src/blog/cron/check_update/mod.rs @@ -111,7 +111,9 @@ pub fn task(db_connection: &Pool) { } } } else if let Some(path) = &update.new_path { - if path.extension().is_some_and(|ext| ext == "md") { + if path.components().any(|comp| comp.as_os_str() == "posts") + && path.extension().is_some_and(|ext| ext == "md") + { update_md.push(path.to_path_buf()); } } @@ -132,7 +134,7 @@ pub fn task(db_connection: &Pool) { match Repository::clone(&git_url, &git_work_dir) { Ok(_repo) => { // full repo index - let files = search_md(git_work_dir); + let files = search_md(git_work_dir.join("posts")); index_files(&files, db_connection); } Err(e) => { diff --git a/backend/api_server/src/blog/cron/mod.rs b/backend/api_server/src/blog/cron/mod.rs index 71581de..e8fd785 100644 --- a/backend/api_server/src/blog/cron/mod.rs +++ b/backend/api_server/src/blog/cron/mod.rs @@ -7,7 +7,7 @@ pub mod check_update; pub fn get_tasks(db_connection: Pool) -> Vec> { let db_conn_clone = db_connection.clone(); vec![ - BasicTask::new(move || check_update::task(&db_conn_clone), "* * * * * *") + BasicTask::new(move || check_update::task(&db_conn_clone), "0 * * * * *") .unwrap() .to_task(), ] diff --git a/backend/markdown/Cargo.toml b/backend/markdown/Cargo.toml index a7d8aef..e0ca45e 100644 --- a/backend/markdown/Cargo.toml +++ b/backend/markdown/Cargo.toml @@ -5,7 +5,6 @@ edition = "2021" [dependencies] uuid = { version = "1.10.0", features = ["v4", "fast-rng", "serde"] } -chrono = { version = "0.4.38", features = ["serde", "alloc"] } serde = { version = "1.0.209", features = ["derive"] } serde_json = "1.0.128" regex = "1.10.4"