Skip to content

Commit 8948d24

Browse files
authored
Merge pull request #3918 from element-hq/quenting/dont-crash-mail-unavailable
Don't prevent starting up if the mail backend is unavailable
2 parents 8a83573 + 3f494a6 commit 8948d24

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

crates/cli/src/commands/server.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::{
2828
util::{
2929
database_pool_from_config, mailer_from_config, password_manager_from_config,
3030
policy_factory_from_config, site_config_from_config, templates_from_config,
31+
test_mailer_in_background,
3132
},
3233
};
3334

@@ -157,7 +158,7 @@ impl Options {
157158

158159
if !self.no_worker {
159160
let mailer = mailer_from_config(&config.email, &templates)?;
160-
mailer.test_connection().await?;
161+
test_mailer_in_background(&mailer, Duration::from_secs(30));
161162

162163
info!("Starting task worker");
163164
mas_tasks::init(

crates/cli/src/commands/worker.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// SPDX-License-Identifier: AGPL-3.0-only
55
// Please see LICENSE in the repository root for full details.
66

7-
use std::process::ExitCode;
7+
use std::{process::ExitCode, time::Duration};
88

99
use clap::Parser;
1010
use figment::Figment;
@@ -17,7 +17,7 @@ use crate::{
1717
lifecycle::LifecycleManager,
1818
util::{
1919
database_pool_from_config, mailer_from_config, site_config_from_config,
20-
templates_from_config,
20+
templates_from_config, test_mailer_in_background,
2121
},
2222
};
2323

@@ -55,7 +55,7 @@ impl Options {
5555
templates_from_config(&config.templates, &site_config, &url_builder).await?;
5656

5757
let mailer = mailer_from_config(&config.email, &templates)?;
58-
mailer.test_connection().await?;
58+
test_mailer_in_background(&mailer, Duration::from_secs(30));
5959

6060
let http_client = mas_http::reqwest_client();
6161
let conn = SynapseConnection::new(

crates/cli/src/util.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

2727
pub 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+
102123
pub async fn policy_factory_from_config(
103124
config: &PolicyConfig,
104125
matrix_config: &MatrixConfig,

0 commit comments

Comments
 (0)