Skip to content
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
61 changes: 38 additions & 23 deletions crates/cli/src/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use mas_templates::Templates;
use opentelemetry::{metrics::Histogram, KeyValue};
use rand::SeedableRng;
use sqlx::PgPool;
use tracing::Instrument;

use crate::telemetry::METER;

Expand Down Expand Up @@ -85,29 +86,43 @@ impl AppState {
self.conn_acquisition_histogram = Some(histogram);
}

/// Init the metadata cache.
///
/// # Panics
///
/// Panics if the metadata cache could not be initialized.
pub async fn init_metadata_cache(&self) {
// XXX: this panics because the error is annoying to propagate
let conn = self
.pool
.acquire()
.await
.expect("Failed to acquire a database connection");

let mut repo = PgRepository::from_conn(conn);

self.metadata_cache
.warm_up_and_run(
&self.http_client,
std::time::Duration::from_secs(60 * 15),
&mut repo,
)
.await
.expect("Failed to warm up the metadata cache");
/// Init the metadata cache in the background
pub fn init_metadata_cache(&self) {
let pool = self.pool.clone();
let metadata_cache = self.metadata_cache.clone();
let http_client = self.http_client.clone();

tokio::spawn(
async move {
let conn = match pool.acquire().await {
Ok(conn) => conn,
Err(e) => {
tracing::error!(
error = &e as &dyn std::error::Error,
"Failed to acquire a database connection"
);
return;
}
};

let mut repo = PgRepository::from_conn(conn);

if let Err(e) = metadata_cache
.warm_up_and_run(
&http_client,
std::time::Duration::from_secs(60 * 15),
&mut repo,
)
.await
{
tracing::error!(
error = &e as &dyn std::error::Error,
"Failed to warm up the metadata cache"
);
}
}
.instrument(tracing::info_span!("metadata_cache.background_warmup")),
);
}
}

Expand Down
3 changes: 1 addition & 2 deletions crates/cli/src/commands/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ impl Options {
conn_acquisition_histogram: None,
};
s.init_metrics();
// XXX: this might panic
s.init_metadata_cache().await;
s.init_metadata_cache();
s
};

Expand Down
Loading