From d2557e47c130501240033fa21132cc92d90e4849 Mon Sep 17 00:00:00 2001 From: Diptesh Choudhuri Date: Tue, 5 Nov 2024 11:26:21 +0530 Subject: [PATCH] perf(backend): remove useless clones --- apps/backend/src/common.rs | 16 ++-- apps/backend/src/main.rs | 164 +++++++++++++++++-------------------- 2 files changed, 84 insertions(+), 96 deletions(-) diff --git a/apps/backend/src/common.rs b/apps/backend/src/common.rs index f2a5b2a794..b9693896c8 100644 --- a/apps/backend/src/common.rs +++ b/apps/backend/src/common.rs @@ -59,11 +59,11 @@ pub struct AppServices { pub async fn create_app_services( is_pro: bool, db: DatabaseConnection, + timezone: chrono_tz::Tz, s3_client: aws_sdk_s3::Client, config: Arc, perform_application_job: &MemoryStorage, perform_core_application_job: &MemoryStorage, - timezone: chrono_tz::Tz, ) -> AppServices { let oidc_client = create_oidc_client(&config).await; let file_storage_service = Arc::new(FileStorageService::new( @@ -99,16 +99,16 @@ pub async fn create_app_services( EmptySubscription, ) .extension(Tracing) - .data(miscellaneous_service.clone()) + .data(db.clone()) + .data(config.clone()) + .data(user_service.clone()) .data(importer_service.clone()) .data(exporter_service.clone()) .data(exercise_service.clone()) - .data(file_storage_service.clone()) .data(statistics_service.clone()) .data(collection_service.clone()) - .data(user_service.clone()) - .data(config.clone()) - .data(db.clone()) + .data(file_storage_service.clone()) + .data(miscellaneous_service.clone()) .finish(); let cors_origins = config @@ -161,9 +161,9 @@ async fn create_oidc_client(config: &config::AppConfig) -> Option { Ok(redirect_url) => match IssuerUrl::new(config.server.oidc.issuer_url.clone()) { Ok(issuer_url) => { match CoreProviderMetadata::discover_async(issuer_url, &async_http_client).await { - Ok(provider) => Some( + Ok(provider_metadata) => Some( CoreClient::from_provider_metadata( - provider, + provider_metadata, ClientId::new(config.server.oidc.client_id.clone()), Some(ClientSecret::new(config.server.oidc.client_secret.clone())), ) diff --git a/apps/backend/src/main.rs b/apps/backend/src/main.rs index b79499c49c..6b51e67488 100644 --- a/apps/backend/src/main.rs +++ b/apps/backend/src/main.rs @@ -129,17 +129,6 @@ async fn main() -> Result<()> { .unwrap_or_else(|_| chrono_tz::Etc::GMT); ryot_log!(info, "Timezone: {}", tz); - let app_services = create_app_services( - is_pro, - db.clone(), - s3_client, - config, - &perform_application_job_storage, - &perform_core_application_job_storage, - tz, - ) - .await; - perform_application_job_storage .enqueue(ApplicationJob::SyncIntegrationsData) .await @@ -156,6 +145,17 @@ async fn main() -> Result<()> { .unwrap(); } + let app_services = create_app_services( + is_pro, + db, + tz, + s3_client, + config, + &perform_application_job_storage, + &perform_core_application_job_storage, + ) + .await; + if cfg!(debug_assertions) { use dependent_models::CompleteExport; use schematic::schema::{SchemaGenerator, TypeScriptRenderer, YamlTemplateRenderer}; @@ -198,93 +198,81 @@ async fn main() -> Result<()> { let importer_service_1 = app_services.importer_service.clone(); let exporter_service_1 = app_services.exporter_service.clone(); - let miscellaneous_service_2 = app_services.miscellaneous_service.clone(); - let miscellaneous_service_4 = app_services.miscellaneous_service.clone(); - let miscellaneous_service_5 = app_services.miscellaneous_service.clone(); let exercise_service_1 = app_services.exercise_service.clone(); let statistics_service_1 = app_services.statistics_service.clone(); let integration_service_1 = app_services.integration_service.clone(); let integration_service_2 = app_services.integration_service.clone(); let integration_service_3 = app_services.integration_service.clone(); - - let monitor = async { - Monitor::::new() - .register_with_count( - 1, - WorkerBuilder::new("background_jobs") - .stream( - // every day - CronStream::new_with_timezone( - Schedule::from_str("0 0 0 * * *").unwrap(), - tz, - ) - .into_stream(), - ) - .layer(ApalisTraceLayer::new()) - .data(miscellaneous_service_2.clone()) - .build_fn(background_jobs), - ) - .register_with_count( - 1, - WorkerBuilder::new("sync_integrations_data") - .stream( - CronStream::new_with_timezone( - Schedule::from_str(&format!("0 */{} * * * *", sync_every_minutes)) - .unwrap(), - tz, - ) + let miscellaneous_service_1 = app_services.miscellaneous_service.clone(); + let miscellaneous_service_2 = app_services.miscellaneous_service.clone(); + let miscellaneous_service_3 = app_services.miscellaneous_service.clone(); + + let monitor = Monitor::::new() + .register_with_count( + 1, + WorkerBuilder::new("background_jobs") + .stream( + // every day + CronStream::new_with_timezone(Schedule::from_str("0 0 0 * * *").unwrap(), tz) .into_stream(), + ) + .layer(ApalisTraceLayer::new()) + .data(miscellaneous_service_1.clone()) + .build_fn(background_jobs), + ) + .register_with_count( + 1, + WorkerBuilder::new("sync_integrations_data") + .stream( + CronStream::new_with_timezone( + Schedule::from_str(&format!("0 */{} * * * *", sync_every_minutes)).unwrap(), + tz, ) - .layer(ApalisTraceLayer::new()) - .data(integration_service_1.clone()) - .build_fn(sync_integrations_data), - ) - // application jobs - .register_with_count( - 1, - WorkerBuilder::new("perform_core_application_job") - .layer(ApalisTraceLayer::new()) - .data(miscellaneous_service_5.clone()) - .data(integration_service_2.clone()) - .source(perform_core_application_job_storage) - .build_fn(perform_core_application_job), - ) - .register_with_count( - 3, - WorkerBuilder::new("perform_application_job") - .data(importer_service_1.clone()) - .data(integration_service_3.clone()) - .data(exporter_service_1.clone()) - .data(miscellaneous_service_4.clone()) - .data(exercise_service_1.clone()) - .data(statistics_service_1.clone()) - .source(perform_application_job_storage) - // DEV: Had to do this fuckery because of https://github.com/geofmureithi/apalis/issues/297 - .chain(|s| { - s.layer(BufferLayer::new(1024)) - .layer(ApalisRateLimitLayer::new( - rate_limit_count, - Duration::new(5, 0), - )) - .layer(ApalisTraceLayer::new()) - }) - .build_fn(perform_application_job), - ) - .run() - .await - .unwrap(); - }; - - let http = async { - axum::serve(listener, app_services.app_router.into_make_service()) - .await - .unwrap(); - }; + .into_stream(), + ) + .layer(ApalisTraceLayer::new()) + .data(integration_service_1.clone()) + .build_fn(sync_integrations_data), + ) + // application jobs + .register_with_count( + 1, + WorkerBuilder::new("perform_core_application_job") + .layer(ApalisTraceLayer::new()) + .data(integration_service_2.clone()) + .data(miscellaneous_service_3.clone()) + .source(perform_core_application_job_storage) + .build_fn(perform_core_application_job), + ) + .register_with_count( + 3, + WorkerBuilder::new("perform_application_job") + .data(exercise_service_1.clone()) + .data(exporter_service_1.clone()) + .data(importer_service_1.clone()) + .data(statistics_service_1.clone()) + .data(integration_service_3.clone()) + .data(miscellaneous_service_2.clone()) + .source(perform_application_job_storage) + // DEV: Had to do this fuckery because of https://github.com/geofmureithi/apalis/issues/297 + .chain(|s| { + s.layer(BufferLayer::new(1024)) + .layer(ApalisRateLimitLayer::new( + rate_limit_count, + Duration::new(5, 0), + )) + .layer(ApalisTraceLayer::new()) + }) + .build_fn(perform_application_job), + ) + .run(); + + let http = axum::serve(listener, app_services.app_router.into_make_service()); if disable_background_jobs { - join!(http); + let _ = join!(http); } else { - join!(monitor, http); + let _ = join!(monitor, http); } Ok(())