diff --git a/crates/crud/src/lib.rs b/crates/crud/src/lib.rs index 771c3f2..c6a9298 100644 --- a/crates/crud/src/lib.rs +++ b/crates/crud/src/lib.rs @@ -9,6 +9,7 @@ use sea_query::{self, Query}; use tracing_unwrap::{self, ResultExt}; use std::sync::Arc; +use std::time::Duration; use tracing::{info, warn}; @@ -30,6 +31,7 @@ pub async fn insert_item( dictionary_item::Entity::insert_many(items.to_vec()) .exec(db) .await?; + tokio::time::sleep(Duration::from_secs_f32(0.1)).await; } info!("{} new dictionary items inserted", count); diff --git a/crates/update_data/src/lib.rs b/crates/update_data/src/lib.rs index 6e7a5f0..0a7464c 100644 --- a/crates/update_data/src/lib.rs +++ b/crates/update_data/src/lib.rs @@ -2,7 +2,7 @@ use std::{collections::HashMap, sync::Arc}; use lazy_static::lazy_static; -use tokio::{self, task::JoinSet}; + use model::Language; @@ -36,39 +36,29 @@ lazy_static! { } pub async fn update_all_data(db: Arc) -> anyhow::Result<()> { - let mut set: JoinSet> = JoinSet::new(); - delete_all_dictionary(&db).await?; - LANGUAGE_URL_MAPPING.iter().for_each(|(lang, url)| { - let db = db.clone(); - set.spawn(async move { - info!("Getting data for {}", lang.str_id()); - let dictionary_map = reqwest::get(url) - .await? - .json::>() - .await?; - info!("Updating data for {}", lang.str_id()); - let item_inserted_count = insert_item( - dictionary_map - .into_iter() - .map(|(word_id, word_translation)| dictionary_item::ActiveModel { - vocabulary_id: Set(word_id), - language: Set(lang.clone()), - vocabulary_translation: Set(word_translation), - ..Default::default() - }) - .collect(), - &db, - ) + for (lang, url) in LANGUAGE_URL_MAPPING.iter() { + info!("Getting data for {}", lang.str_id()); + let dictionary_map = reqwest::get(url) + .await? + .json::>() .await?; - info!("Data for {} updated", lang.str_id()); - Ok(item_inserted_count) - }); - }); - - while let Some(handle) = set.join_next().await { - handle??; + info!("Updating data for {}", lang.str_id()); + let item_inserted_count = insert_item( + dictionary_map + .into_iter() + .map(|(word_id, word_translation)| dictionary_item::ActiveModel { + vocabulary_id: Set(word_id), + language: Set(lang.clone()), + vocabulary_translation: Set(word_translation), + ..Default::default() + }) + .collect(), + &db, + ) + .await?; + info!("Data for {} updated ({})", lang.str_id(), item_inserted_count); } delete_duplicate_items(&db).await?; diff --git a/justfile b/justfile new file mode 100644 index 0000000..fdc0b0b --- /dev/null +++ b/justfile @@ -0,0 +1,5 @@ +dockerbuild: + docker build -t daicanglong/starrail-dictionary-backend:latest . --platform linux/x86_64 + +dockerpush: + docker push daicanglong/starrail-dictionary-backend:latest \ No newline at end of file