Skip to content

Commit

Permalink
feat: init leaderboard for all users before server starts
Browse files Browse the repository at this point in the history
  • Loading branch information
Extheoisah committed Nov 21, 2024
1 parent de3e82f commit b9a720a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/app/init/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use log::{error, info};

use crate::service::database::{
conn::DbPool,
models::{Leaderboard, User},
};

pub async fn initialize_leaderboards(pool: &DbPool) -> Result<(), Box<dyn std::error::Error>> {
let mut conn = pool.get()?;

// Get all users
let users = User::get_all_users(&mut conn)?;

if users.is_empty() {
info!("No users found in database - skipping leaderboard initialization");
return Ok(());
}

let mut initialized_count = 0;
for user in users {
// Check if user has a leaderboard entry
match Leaderboard::get_leaderboard(&mut conn, Some(&user.id)) {
Ok(leaderboard) => {
if leaderboard.is_empty() {
// Create new leaderboard for user
let new_leaderboard = Leaderboard::new(&user.id, None, 0, 0);
Leaderboard::create(&mut conn, new_leaderboard)?;
initialized_count += 1;
}
}
Err(e) => {
error!("Error checking leaderboard for user {}: {}", user.id, e);
// Create new leaderboard for user
let new_leaderboard = Leaderboard::new(&user.id, None, 0, 0);
Leaderboard::create(&mut conn, new_leaderboard)?;
initialized_count += 1;
}
}
}

if initialized_count > 0 {
info!(
"Initialized {} missing leaderboard records",
initialized_count
);
} else {
info!("All users have leaderboard records - no initialization needed");
}

Ok(())
}
1 change: 1 addition & 0 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod auth;
pub mod init;
pub mod routes;
pub mod progress;
pub mod repo;
Expand Down
16 changes: 16 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ use actix_cors::Cors;
use actix_web::{middleware::Logger, web, App, HttpServer};
use app::{
auth::middleware::AuthMiddleware,
init::initialize_leaderboards,
routes,
websockets::{handler::websocket_handler, manager::WebSocketManagerHandle},
};
use dotenvy::dotenv;
use env_logger::Env;
use log::error;
use service::{database::conn::get_connection_pool, queue::consume_queue};

mod app;
Expand Down Expand Up @@ -37,6 +39,20 @@ async fn main() -> std::io::Result<()> {
let connection_url =
std::env::var("CONNECTION_URL").unwrap_or_else(|_| "127.0.0.1:4925".to_string());
let pool = get_connection_pool();

// Initialize leaderboards before starting server
// This is done to ensure that leaderboards are created for all users
// since we have some users created before the leaderboard service was
// added to signup.
// TODO: Remove this once all users have leaderboard records
if let Err(e) = initialize_leaderboards(&pool).await {
error!("Failed to initialize leaderboards: {}", e);
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to initialize leaderboards: {}", e),
));
}

let manager_handle = WebSocketManagerHandle::new();
let manager_handle_clone = manager_handle.clone();

Expand Down
4 changes: 4 additions & 0 deletions src/service/repository/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,8 @@ impl User {
_ => Err(anyhow::anyhow!("No input provided")),
}
}

pub fn get_all_users(conn: &mut PgConnection) -> QueryResult<Vec<User>> {
users::table.select(User::as_select()).load(conn)
}
}

0 comments on commit b9a720a

Please sign in to comment.