Skip to content

Commit 7ef67dc

Browse files
committedJan 26, 2025
更新了对错误处理失败的日志记录
1 parent 5a6e63a commit 7ef67dc

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed
 

‎rsendmail/src/mailer.rs

+21-14
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl Mailer {
5555
let running = running.clone();
5656

5757
let handle = task::spawn(async move {
58-
let mut group_stats = (0, Duration::default(), Duration::default());
58+
let mut group_stats = (0, Duration::default(), Duration::default(), Vec::new());
5959
for (j, file) in chunk.iter().enumerate() {
6060
if !running.load(Ordering::SeqCst) {
6161
warn!("进程组 {} 收到中断信号,正在退出...", i + 1);
@@ -73,6 +73,7 @@ impl Mailer {
7373
}
7474
Err(e) => {
7575
error!("进程组 {} 文件 {} 发送失败: {}", i + 1, j + 1, e);
76+
group_stats.3.push((e.to_string(), file.clone()));
7677
}
7778
}
7879
}
@@ -87,10 +88,13 @@ impl Mailer {
8788
let mut total_send_duration = Duration::default();
8889

8990
for handle in handles {
90-
if let Ok((sent, parse_duration, send_duration)) = handle.await {
91+
if let Ok((sent, parse_duration, send_duration, errors)) = handle.await {
9192
total_sent += sent;
9293
total_parse_duration += parse_duration;
9394
total_send_duration += send_duration;
95+
for (error_type, file_path) in errors {
96+
stats.increment_error(&error_type, &file_path);
97+
}
9498
}
9599
}
96100

@@ -130,14 +134,12 @@ impl Mailer {
130134

131135
current_batch.push(file.clone());
132136

133-
// 当达到批处理大小或是最后一个文件时,发送这一批邮件
134137
if current_batch.len() >= config.batch_size || j == chunk.len() - 1 {
135138
info!("进程组 {} 开始发送第 {}/{} 批,包含 {} 封邮件",
136139
i + 1, j / config.batch_size + 1,
137140
(chunk.len() + config.batch_size - 1) / config.batch_size,
138141
current_batch.len());
139142

140-
// 为每个批次创建新的SMTP客户端
141143
info!("连接SMTP服务器: {}:{}", config.smtp_server, config.port);
142144
let client_result = match timeout(Duration::from_secs(config.smtp_timeout),
143145
SmtpClientBuilder::new(config.smtp_server.as_str(), config.port)
@@ -148,13 +150,14 @@ impl Mailer {
148150
Ok(result) => result,
149151
Err(_) => {
150152
error!("SMTP连接超时");
151-
group_stats.3.push("SMTP连接超时".to_string());
153+
for file in &current_batch {
154+
group_stats.3.push(("SMTP连接超时".to_string(), file.clone()));
155+
}
152156
current_batch.clear();
153157
continue;
154158
}
155159
};
156160

157-
// 发送这一批邮件
158161
match client_result {
159162
Ok(mut client) => {
160163
match Self::send_batch_emails(&config, &current_batch, &mut client).await {
@@ -167,13 +170,17 @@ impl Mailer {
167170
}
168171
Err(e) => {
169172
error!("批量发送失败: {}", e);
170-
group_stats.3.push(e.to_string());
173+
for file in &current_batch {
174+
group_stats.3.push((e.to_string(), file.clone()));
175+
}
171176
}
172177
}
173178
}
174179
Err(e) => {
175180
error!("SMTP连接失败: {}", e);
176-
group_stats.3.push("SMTP连接失败".to_string());
181+
for file in &current_batch {
182+
group_stats.3.push(("SMTP连接失败".to_string(), file.clone()));
183+
}
177184
}
178185
}
179186

@@ -193,8 +200,8 @@ impl Mailer {
193200
total_sent += sent;
194201
stats.parse_durations.extend(parse_durations);
195202
stats.send_durations.extend(send_durations);
196-
for error_type in errors {
197-
stats.increment_error(&error_type);
203+
for (error_type, file_path) in errors {
204+
stats.increment_error(&error_type, &file_path);
198205
}
199206
}
200207
}
@@ -251,10 +258,10 @@ impl Mailer {
251258
async fn send_single_email(config: &Config, file_path: &str) -> Result<(Duration, Duration)> {
252259
info!("开始读取文件: {}", file_path);
253260
let parse_start = Instant::now();
254-
let content = fs::read_to_string(file_path)?;
261+
let content = fs::read(file_path)?;
255262

256263
info!("解析邮件内容");
257-
let message = match MessageParser::default().parse(content.as_bytes()) {
264+
let message = match MessageParser::default().parse(&content) {
258265
Some(msg) => msg,
259266
None => {
260267
error!("无法解析邮件文件: {}", file_path);
@@ -319,10 +326,10 @@ impl Mailer {
319326
for file_path in files {
320327
info!("开始读取文件: {}", file_path);
321328
let parse_start = Instant::now();
322-
let content = fs::read_to_string(file_path)?;
329+
let content = fs::read(file_path)?;
323330

324331
info!("解析邮件内容");
325-
let message = match MessageParser::default().parse(content.as_bytes()) {
332+
let message = match MessageParser::default().parse(&content) {
326333
Some(msg) => msg,
327334
None => {
328335
error!("无法解析邮件文件: {}", file_path);

‎rsendmail/src/stats.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub struct Stats {
1111
pub parse_errors: usize,
1212
pub send_errors: usize,
1313
pub error_details: HashMap<String, usize>,
14+
pub failed_files: HashMap<String, Vec<String>>,
1415
}
1516

1617
impl Stats {
@@ -23,6 +24,7 @@ impl Stats {
2324
parse_errors: 0,
2425
send_errors: 0,
2526
error_details: HashMap::new(),
27+
failed_files: HashMap::new(),
2628
}
2729
}
2830

@@ -50,8 +52,11 @@ impl Stats {
5052
self.send_errors += 1;
5153
}
5254

53-
pub fn increment_error(&mut self, error_type: &str) {
55+
pub fn increment_error(&mut self, error_type: &str, file_path: &str) {
5456
*self.error_details.entry(error_type.to_string()).or_insert(0) += 1;
57+
self.failed_files.entry(error_type.to_string())
58+
.or_insert_with(Vec::new)
59+
.push(file_path.to_string());
5560
self.send_errors += 1;
5661
}
5762

@@ -75,10 +80,14 @@ impl fmt::Display for Stats {
7580
writeln!(f, " 发送失败详情:")?;
7681
for (error_type, count) in &self.error_details {
7782
writeln!(f, " {}: {} 封", error_type, count)?;
83+
if let Some(files) = self.failed_files.get(error_type) {
84+
for file in files {
85+
writeln!(f, " - {}", file)?;
86+
}
87+
}
7888
}
7989
}
8090

81-
8291
// 计算总的解析和发送时间
8392
let total_parse_duration: Duration = self.parse_durations.iter().sum();
8493
let total_send_duration: Duration = self.send_durations.iter().sum();

0 commit comments

Comments
 (0)
Failed to load comments.