From 2d00604c6fe6965eaffb8c26259eab9bc3fc96a6 Mon Sep 17 00:00:00 2001 From: "Wyatt J. Miller" Date: Wed, 27 Nov 2024 00:33:59 -0500 Subject: [PATCH] added cors to public, modified import_posts task, updated task log sql queries --- backend/public/Cargo.toml | 2 +- backend/public/src/main.rs | 20 ++++++++++++----- backend/task/src/tasks/import_posts.rs | 19 +++++++++++----- backend/task/src/utils/task_log.rs | 31 +++++++++++++++++++++----- 4 files changed, 55 insertions(+), 17 deletions(-) diff --git a/backend/public/Cargo.toml b/backend/public/Cargo.toml index 1630c65..e094372 100644 --- a/backend/public/Cargo.toml +++ b/backend/public/Cargo.toml @@ -8,7 +8,7 @@ authors = ["Wyatt J. Miller (pool: &'a sqlx::Pool) { +pub fn register(pool: &sqlx::Pool) { let p = pool.clone(); tokio::spawn(async move { import_posts("/app", &p).await; @@ -13,6 +13,7 @@ pub fn register<'a>(pool: &'a sqlx::Pool) { async fn import_posts(dir_path: &str, pool: &sqlx::Pool) { println!("hello from import_posts"); + let task = task_log::start(1, pool).await.unwrap(); let entries = fs::read_dir(dir_path).unwrap(); let options = MarkdownOptions { @@ -41,7 +42,11 @@ async fn import_posts(dir_path: &str, pool: &sqlx::Pool) { ); let file_md_contents = process_read_file(file_path, &options); let content = markdown::to_html(&file_md_contents); - let metadata = crate::utils::front_matter::YamlFrontMatter::parse::(&content).unwrap(); + let metadata = + crate::utils::front_matter::YamlFrontMatter::parse::( + &content, + ) + .unwrap(); let title = metadata.metadata.title; sqlx::query_as::<_, InsertPosts>( @@ -57,6 +62,10 @@ async fn import_posts(dir_path: &str, pool: &sqlx::Pool) { } } } + + task_log::update(task.task_id, String::from("Completed"), pool) + .await + .unwrap(); } fn process_read_file>(path: P, md_opts: &MarkdownOptions) -> String { @@ -92,7 +101,7 @@ struct MarkdownMetadata { fn deserialize_datetime<'de, D>(deserializer: D) -> Result, D::Error> where - D: serde::Deserializer<'de>, + D: Deserializer<'de>, { let s = String::deserialize(deserializer)?; chrono::DateTime::parse_from_rfc3339(&s) diff --git a/backend/task/src/utils/task_log.rs b/backend/task/src/utils/task_log.rs index a9e58a8..20eeeab 100644 --- a/backend/task/src/utils/task_log.rs +++ b/backend/task/src/utils/task_log.rs @@ -1,11 +1,30 @@ -use crate::TaskStatus; +use crate::TaskLog; -pub async fn start(task_id: i32, pool: &sqlx::Pool) -> Result<(), Box> { - let _ = sqlx::query_as!(TaskLog, "INSERT INTO logs (task_id, created_at, task_status) VALUES ($1, now(), 'pending')", task_id).fetch_one(pool).await; - Ok(()) +pub async fn start( + task_id: i32, + pool: &sqlx::Pool, +) -> Result { + sqlx::query_as!( + TaskLog, + "INSERT INTO logs (task_id, created_at, task_status) VALUES ($1, now(), 'pending') RETURNING task_id, log_id, created_at, task_status, finished_at", + task_id + ) + .fetch_one(pool) + .await } -pub async fn update(task_id: i32, task_status: String, pool: &sqlx::Pool) -> Result<(), Box> { - let _ = sqlx::query_as!(TaskLog, "UPDATE logs SET task_status = $1 WHERE task_id = $2", task_status, task_id).fetch_one(pool).await; +pub async fn update( + task_id: i32, + task_status: String, + pool: &sqlx::Pool, +) -> Result<(), Box> { + let _ = sqlx::query_as!( + TaskLog, + "UPDATE logs SET task_status = $1 WHERE task_id = $2", + task_status, + task_id + ) + .fetch_one(pool) + .await; Ok(()) }