1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-11-20 12:02:22 +03:00

Refactor and simplify the templates hot-reload logic

This commit is contained in:
Quentin Gliech
2022-12-15 15:54:07 +01:00
parent ee42250660
commit 808a8218fd
4 changed files with 69 additions and 83 deletions

View File

@@ -28,7 +28,7 @@ use sqlx::{
postgres::{PgConnectOptions, PgPoolOptions},
ConnectOptions, PgPool,
};
use tracing::log::LevelFilter;
use tracing::{error, info, log::LevelFilter};
pub async fn password_manager_from_config(
config: &PasswordsConfig,
@@ -168,3 +168,61 @@ pub async fn database_from_config(config: &DatabaseConfig) -> Result<PgPool, any
.await
.context("could not connect to the database")
}
/// Watch for changes in the templates folders
pub async fn watch_templates(templates: &Templates) -> anyhow::Result<()> {
use watchman_client::{prelude::*, SubscriptionData};
let client = Connector::new()
.connect()
.await
.context("could not connect to watchman")?;
let templates = templates.clone();
// Find which root we're supposed to watch
let root = templates.watch_root();
// Create a subscription on the root
let resolved = client
.resolve_root(CanonicalPath::canonicalize(root)?)
.await?;
// Only look for *.txt, *.html and *.subject files
let request = SubscribeRequest {
expression: Some(Expr::Suffix(vec![
"txt".into(),
"html".into(),
"subject".into(),
])),
..SubscribeRequest::default()
};
let (mut subscription, _) = client.subscribe::<NameOnly>(&resolved, request).await?;
tokio::spawn(async move {
loop {
let event = match subscription.next().await {
Ok(event) => event,
Err(error) => {
error!(%error, "Stopped watching templates because of an error in the watchman subscription");
break;
}
};
if let SubscriptionData::FilesChanged(QueryResult {
files: Some(files), ..
}) = event
{
let files: Vec<_> = files.into_iter().map(|f| f.name.into_inner()).collect();
info!(?files, "Files changed, reloading templates");
templates.clone().reload().await.unwrap_or_else(|err| {
error!(?err, "Error while reloading templates");
});
}
}
});
Ok(())
}