diff options
| author | Carl Hetherington <cth@carlh.net> | 2025-02-16 00:52:53 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2025-02-17 00:30:55 +0100 |
| commit | e9167bc88529bf5eb71bdefa4e69fdbac790a391 (patch) | |
| tree | fee677aa0457c07a7ef4677defaebc23ece3da28 | |
| parent | 410624e0548f65b795b192367f9272eb6f5d7bdb (diff) | |
Add retry for email sending (#2963).2963-retry-email
| -rw-r--r-- | src/lib/email.cc | 20 | ||||
| -rw-r--r-- | src/lib/email.h | 1 | ||||
| -rw-r--r-- | src/lib/kdm_with_metadata.cc | 2 | ||||
| -rw-r--r-- | test/email_test.cc | 43 | ||||
| -rw-r--r-- | test/smtp_server.cc | 79 | ||||
| -rw-r--r-- | test/smtp_server.h | 23 | ||||
| -rw-r--r-- | test/wscript | 2 |
7 files changed, 169 insertions, 1 deletions
diff --git a/src/lib/email.cc b/src/lib/email.cc index 028624c69..f0ff9f910 100644 --- a/src/lib/email.cc +++ b/src/lib/email.cc @@ -21,6 +21,7 @@ #include "compose.hpp" #include "config.h" +#include "dcpomatic_log.h" #include "email.h" #include "exceptions.h" #include "util.h" @@ -110,6 +111,25 @@ Email::get_data(void* ptr, size_t size, size_t nmemb) void +Email::send_with_retry(string server, int port, EmailProtocol protocol, int retries, string user, string password) +{ + int this_try = 0; + while (true) { + try { + send(server, port, protocol, user, password); + return; + } catch (NetworkError& e) { + LOG_ERROR("Error %1 when trying to send email on attempt %2 of 3", e.what(), this_try + 1, retries); + if (this_try == (retries - 1)) { + throw; + } + } + ++this_try; + } +} + + +void Email::send(string server, int port, EmailProtocol protocol, string user, string password) { _email = "Date: " + rfc_2822_date(time(nullptr)) + "\r\n" diff --git a/src/lib/email.h b/src/lib/email.h index ac4703453..0ef68379b 100644 --- a/src/lib/email.h +++ b/src/lib/email.h @@ -35,6 +35,7 @@ public: void add_attachment (boost::filesystem::path file, std::string name, std::string mime_type); void send (std::string server, int port, EmailProtocol protocol, std::string user = "", std::string password = ""); + void send_with_retry(std::string server, int port, EmailProtocol protocol, int retries, std::string user = "", std::string password = ""); std::string notes () const { return _notes; diff --git a/src/lib/kdm_with_metadata.cc b/src/lib/kdm_with_metadata.cc index d09f6930b..b6e027ef3 100644 --- a/src/lib/kdm_with_metadata.cc +++ b/src/lib/kdm_with_metadata.cc @@ -277,7 +277,7 @@ send_emails( }; try { - email.send(config->mail_server(), config->mail_port(), config->mail_protocol(), config->mail_user(), config->mail_password()); + email.send_with_retry(config->mail_server(), config->mail_port(), config->mail_protocol(), 5, config->mail_user(), config->mail_password()); } catch (...) { log_details(email); throw; diff --git a/test/email_test.cc b/test/email_test.cc new file mode 100644 index 000000000..2402c1d07 --- /dev/null +++ b/test/email_test.cc @@ -0,0 +1,43 @@ +/* + Copyright (C) 2025 Carl Hetherington <cth@carlh.net> + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>. + +*/ + + +#include "lib/email.h" +#include "smtp_server.h" +#include "test.h" +#include <boost/test/unit_test.hpp> +#include <boost/thread.hpp> + + +auto constexpr port = 31925; + + +BOOST_AUTO_TEST_CASE(email_retry_test) +{ + boost::thread thread([]() { + run_smtp_server(port, true); + run_smtp_server(port, true); + run_smtp_server(port, false); + }); + + Email email("carl@crunchcinema.com", { "bob@snacks.com" }, "Louder crisps - possible?", "These crisps just aren't loud enough. People can still hear the film."); + email.send_with_retry("localhost", port, EmailProtocol::PLAIN, 3); +} + diff --git a/test/smtp_server.cc b/test/smtp_server.cc new file mode 100644 index 000000000..c6802bded --- /dev/null +++ b/test/smtp_server.cc @@ -0,0 +1,79 @@ +/* + Copyright (C) 2025 Carl Hetherington <cth@carlh.net> + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>. + +*/ + + +#include "smtp_server.h" +#include <boost/asio.hpp> +#include <iostream> +#include <string> + + +using std::string; +using boost::asio::ip::tcp; + + +void +run_smtp_server(int port, bool fail) +{ + boost::asio::io_context context; + tcp::acceptor acceptor(context, tcp::endpoint(tcp::v4(), port)); + tcp::socket socket(context); + acceptor.accept(socket); + + auto send = [&socket](string message) { + boost::system::error_code error; + boost::asio::write(socket, boost::asio::buffer(message + "\r\n"), boost::asio::transfer_all(), error); + }; + + auto receive = [&socket]() { + boost::asio::streambuf buffer; + boost::asio::read_until(socket, buffer, "\n"); + return string{ + std::istreambuf_iterator<char>(&buffer), + std::istreambuf_iterator<char>() + }; + }; + + send("220 smtp.example.com ESMTP Postfix"); + /* EHLO */ + receive(); + send("250-smtp.example.com Hello mate [127.0.0.1]"); + send("250-SIZE 14680064"); + send("250-PIPELINING"); + send("250 HELP"); + /* MAIL FROM */ + receive(); + send("250 Ok"); + /* RCPT TO */ + if (fail) { + return; + } + receive(); + send("250 Ok"); + /* DATA */ + receive(); + send("354 End data with <CR><LF>.<CR><LF>"); + /* Email body */ + receive(); + send("250 Ok"); + /* QUIT */ + receive(); +} + diff --git a/test/smtp_server.h b/test/smtp_server.h new file mode 100644 index 000000000..f3cc1d0a9 --- /dev/null +++ b/test/smtp_server.h @@ -0,0 +1,23 @@ +/* + Copyright (C) 2025 Carl Hetherington <cth@carlh.net> + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>. + +*/ + + +extern void run_smtp_server(int port, bool fail); + diff --git a/test/wscript b/test/wscript index b04e244be..7b4e484d6 100644 --- a/test/wscript +++ b/test/wscript @@ -81,6 +81,7 @@ def build(bld): dcp_subtitle_test.cc digest_test.cc dkdm_recipient_list_test.cc + email_test.cc empty_caption_test.cc empty_test.cc encryption_test.cc @@ -154,6 +155,7 @@ def build(bld): shuffler_test.cc skip_frame_test.cc socket_test.cc + smtp_server.cc srt_subtitle_test.cc ssa_subtitle_test.cc stream_test.cc |
