Skip to content

Commit

Permalink
fix: wisdoms checker update
Browse files Browse the repository at this point in the history
  • Loading branch information
nikola-bozin-org committed May 4, 2024
1 parent 7f6ce12 commit 262d0b2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async fn main() {

sqlx::migrate!("./migrations").run(&db).await.unwrap();

let state = AppState { db };
let state = AppState { db: db.clone() };

let shared_state = Arc::new(state);

Expand All @@ -47,7 +47,7 @@ async fn main() {
.layer(middleware::from_fn(middlewares::rate_limit))
.layer(middleware::map_response(response_mapper));

tokio::spawn(worker_thread());
tokio::spawn(worker_thread(db));

axum::serve(
listener,
Expand Down
43 changes: 37 additions & 6 deletions src/wisdoms_checker.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use base64::{self, Engine};
use serde::Deserialize;
use sqlx::PgPool;
use std::fs;
use std::time::Duration;

Expand All @@ -8,24 +9,54 @@ struct Wisdom {
description: String,
}

pub async fn worker_thread() {
#[derive(Debug, sqlx::FromRow)]
struct CountResult {
count: i64,
}

pub async fn worker_thread(pool: PgPool) {
let mut prev_base64_string = String::new();

loop {
tokio::time::sleep(Duration::from_secs(5)).await;

let base64_string = fs::read_to_string("encoded-wisdoms.b64")
.expect("Failed to read encoded-wisdoms.b64 file")
.replace(['\n','\r'], "");
.replace(['\n', '\r'], "");

dbg!(&base64_string);
if base64_string == prev_base64_string {
continue;
}

let decoded_bytes = base64::prelude::BASE64_STANDARD.decode(base64_string)
let decoded_bytes = base64::prelude::BASE64_STANDARD
.decode(&base64_string)
.expect("Failed to decode base64 string");

let wisdoms: Vec<Wisdom> =
serde_json::from_slice(&decoded_bytes).expect("Failed to parse JSON");

for wisdom in wisdoms {
println!("Decoded wisdom: {:?}", wisdom);
let existing_wisdom = sqlx::query_as::<_, CountResult>(
"SELECT COUNT(*) as count FROM wisdoms WHERE description = $1",
)
.bind(&wisdom.description)
.fetch_one(&pool)
.await
.expect("Failed to check if wisdom exists");

if existing_wisdom.count == 0 {
sqlx::query!(
"INSERT INTO wisdoms (description) VALUES ($1)",
wisdom.description
)
.execute(&pool)
.await
.expect("Failed to insert wisdom into database");
} else {
println!("Wisdom already exists, skipping: {:?}", wisdom);
}
}

tokio::time::sleep(Duration::from_secs(1)).await;
prev_base64_string = base64_string;
}
}

0 comments on commit 262d0b2

Please sign in to comment.