Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: insert too fast
Browse files Browse the repository at this point in the history
  • Loading branch information
CanglongCl committed Feb 7, 2024
1 parent 6110089 commit c2ec7c0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
2 changes: 2 additions & 0 deletions crates/crud/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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);
Expand Down
52 changes: 21 additions & 31 deletions crates/update_data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::HashMap, sync::Arc};

use lazy_static::lazy_static;

use tokio::{self, task::JoinSet};


use model::Language;

Expand Down Expand Up @@ -36,39 +36,29 @@ lazy_static! {
}

pub async fn update_all_data(db: Arc<DbConn>) -> anyhow::Result<()> {
let mut set: JoinSet<anyhow::Result<usize>> = 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::<HashMap<i32, String>>()
.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::<HashMap<i32, String>>()
.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?;
Expand Down
5 changes: 5 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c2ec7c0

Please sign in to comment.