@@ -22,7 +22,7 @@ use sqlx::{
2222 postgres:: { PgConnectOptions , PgPoolOptions } ,
2323 ConnectOptions , PgConnection , PgPool ,
2424} ;
25- use tracing:: log:: LevelFilter ;
25+ use tracing:: { log:: LevelFilter , Instrument } ;
2626
2727pub async fn password_manager_from_config (
2828 config : & PasswordsConfig ,
@@ -99,6 +99,27 @@ pub fn mailer_from_config(
9999 Ok ( Mailer :: new ( templates. clone ( ) , transport, from, reply_to) )
100100}
101101
102+ /// Test the connection to the mailer in a background task
103+ pub fn test_mailer_in_background ( mailer : & Mailer , timeout : Duration ) {
104+ let mailer = mailer. clone ( ) ;
105+
106+ let span = tracing:: info_span!( "cli.test_mailer" ) ;
107+ tokio:: spawn ( async move {
108+ match tokio:: time:: timeout ( timeout, mailer. test_connection ( ) ) . await {
109+ Ok ( Ok ( ( ) ) ) => { }
110+ Ok ( Err ( err) ) => {
111+ tracing:: warn!(
112+ error = & err as & dyn std:: error:: Error ,
113+ "Could not connect to the mail backend, tasks sending mails may fail!"
114+ ) ;
115+ }
116+ Err ( _) => {
117+ tracing:: warn!( "Timed out while testing the mail backend connection, tasks sending mails may fail!" ) ;
118+ }
119+ }
120+ } . instrument ( span) ) ;
121+ }
122+
102123pub async fn policy_factory_from_config (
103124 config : & PolicyConfig ,
104125 matrix_config : & MatrixConfig ,
0 commit comments