@@ -22,7 +22,7 @@ use sqlx::{
22
22
postgres:: { PgConnectOptions , PgPoolOptions } ,
23
23
ConnectOptions , PgConnection , PgPool ,
24
24
} ;
25
- use tracing:: log:: LevelFilter ;
25
+ use tracing:: { log:: LevelFilter , Instrument } ;
26
26
27
27
pub async fn password_manager_from_config (
28
28
config : & PasswordsConfig ,
@@ -99,6 +99,27 @@ pub fn mailer_from_config(
99
99
Ok ( Mailer :: new ( templates. clone ( ) , transport, from, reply_to) )
100
100
}
101
101
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
+
102
123
pub async fn policy_factory_from_config (
103
124
config : & PolicyConfig ,
104
125
matrix_config : & MatrixConfig ,
0 commit comments