Skip to content

Commit

Permalink
perf(backend): remove useless clones
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnisDa committed Nov 5, 2024
1 parent 81e97cf commit d2557e4
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 96 deletions.
16 changes: 8 additions & 8 deletions apps/backend/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<config::AppConfig>,
perform_application_job: &MemoryStorage<ApplicationJob>,
perform_core_application_job: &MemoryStorage<CoreApplicationJob>,
timezone: chrono_tz::Tz,
) -> AppServices {
let oidc_client = create_oidc_client(&config).await;
let file_storage_service = Arc::new(FileStorageService::new(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -161,9 +161,9 @@ async fn create_oidc_client(config: &config::AppConfig) -> Option<CoreClient> {
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())),
)
Expand Down
164 changes: 76 additions & 88 deletions apps/backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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};
Expand Down Expand Up @@ -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::<TokioExecutor>::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::<TokioExecutor>::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(())
Expand Down

0 comments on commit d2557e4

Please sign in to comment.