Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve seed structure #1165

Merged
merged 4 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions services/headless-lms/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions services/headless-lms/entrypoint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ headless-lms-server = { path = "../server" }
actix-web = "4.3.1"
# Flexible concrete Error type built on std::error::Error
anyhow = "1.0.71"
# Combinators and utilities for working with Futures, Streams, Sinks, and the AsyncRead and AsyncWrite traits.
futures-util = "0.3.28"
# A runtime for writing reliable network applications without compromising speed.
tokio = "1.29.1"
61 changes: 46 additions & 15 deletions services/headless-lms/entrypoint/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,60 @@
use std::future::Future;

use futures_util::FutureExt;
use headless_lms_server::programs;

/// The entrypoint to all the binaries provided by the project.
/// Expects program name as the first argument.
#[actix_web::main]
async fn main() -> anyhow::Result<()> {
fn main() -> anyhow::Result<()> {
let program_name = std::env::args()
.nth(1)
.expect("No program name provided as the first argument.");
match program_name.as_str() {
"doc-file-generator" => programs::doc_file_generator::main().await?,
"email-deliver" => programs::email_deliver::main().await?,
"ended-exams-processor" => programs::ended_exams_processor::main().await?,
let future = match program_name.as_str() {
"doc-file-generator" => programs::doc_file_generator::main().boxed_local(),
"email-deliver" => programs::email_deliver::main().boxed_local(),
"ended-exams-processor" => programs::ended_exams_processor::main().boxed_local(),
"open-university-registration-link-fetcher" => {
programs::open_university_registration_link_fetcher::main().await?
programs::open_university_registration_link_fetcher::main().boxed_local()
}
"regrader" => programs::regrader::main().boxed_local(),
"seed" => programs::seed::main().boxed_local(),
"service-info-fetcher" => programs::service_info_fetcher::main().boxed_local(),
"peer-review-updater" => programs::peer_review_updater::main().boxed_local(),
"start-server" => {
// we'll run the server on the actix runtime without boxing it
actix_run(programs::start_server::main())?;
return Ok(());
}
"sorter" => {
// not async so no need to involve a runtime
programs::sorter::sort()?;
return Ok(());
}
"regrader" => programs::regrader::main().await?,
"seed" => programs::seed::main().await?,
"service-info-fetcher" => programs::service_info_fetcher::main().await?,
"peer-review-updater" => programs::peer_review_updater::main().await?,
"start-server" => programs::start_server::main().await?,
"sorter" => programs::sorter::sort()?,
"sync-tmc-users" => programs::sync_tmc_users::main().await?,
"calculate-page-visit-stats" => programs::calculate_page_visit_stats::main().await?,
"sync-tmc-users" => programs::sync_tmc_users::main().boxed_local(),
"calculate-page-visit-stats" => programs::calculate_page_visit_stats::main().boxed_local(),
_ => panic!("Unknown program name: {}", program_name),
};
tokio_run(future)?;

Ok(())
}

fn actix_run<F>(f: F) -> anyhow::Result<()>
where
F: Future<Output = anyhow::Result<()>>,
{
let rt = actix_web::rt::Runtime::new()?;
rt.block_on(f)?;
Ok(())
}

// tokio's default runtime can use multiple threads, so it's less prone to stack overflows when spawning lots of tasks
// making it better suited for non-actix-web tasks
fn tokio_run<F>(f: F) -> anyhow::Result<()>
where
F: Future<Output = anyhow::Result<()>>,
{
let rt = tokio::runtime::Runtime::new()?;
rt.block_on(f)?;
Ok(())
}
10 changes: 5 additions & 5 deletions services/headless-lms/server/src/programs/seed/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(clippy::too_many_arguments)]

pub mod seed_certificate_fonts;
pub mod seed_courses;
pub mod seed_exercise_services;
Expand All @@ -10,7 +8,7 @@ pub mod seed_playground_examples;
pub mod seed_roles;
pub mod seed_users;

use std::{env, process::Command, sync::Arc};
use std::{env, process::Command, sync::Arc, time::Duration};

use crate::{domain::models_requests::JwtKey, setup_tracing};

Expand All @@ -28,11 +26,11 @@ pub async fn main() -> anyhow::Result<()> {
let (_, seed_users_result, _) = try_join!(
run_parallelly(seed_exercise_services::seed_exercise_services(
db_pool.clone()
),),
)),
run_parallelly(seed_users::seed_users(db_pool.clone())),
run_parallelly(seed_playground_examples::seed_playground_examples(
db_pool.clone()
),),
)),
)?;

let (uh_cs_organization_result, _uh_mathstat_organization_id) = try_join!(
Expand Down Expand Up @@ -71,6 +69,8 @@ async fn setup_seed_environment() -> anyhow::Result<Pool<Postgres>> {
let db_pool = PgPoolOptions::new()
.max_connections(10)
.min_connections(5)
// the seed process can take a while, default is 30
.acquire_timeout(Duration::from_secs(90))
.connect(&db_url)
.await?;

Expand Down
Loading
Loading