@@ -55,7 +55,7 @@ impl Mailer {
55
55
let running = running. clone ( ) ;
56
56
57
57
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 ( ) ) ;
59
59
for ( j, file) in chunk. iter ( ) . enumerate ( ) {
60
60
if !running. load ( Ordering :: SeqCst ) {
61
61
warn ! ( "进程组 {} 收到中断信号,正在退出..." , i + 1 ) ;
@@ -73,6 +73,7 @@ impl Mailer {
73
73
}
74
74
Err ( e) => {
75
75
error ! ( "进程组 {} 文件 {} 发送失败: {}" , i + 1 , j + 1 , e) ;
76
+ group_stats. 3 . push ( ( e. to_string ( ) , file. clone ( ) ) ) ;
76
77
}
77
78
}
78
79
}
@@ -87,10 +88,13 @@ impl Mailer {
87
88
let mut total_send_duration = Duration :: default ( ) ;
88
89
89
90
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 {
91
92
total_sent += sent;
92
93
total_parse_duration += parse_duration;
93
94
total_send_duration += send_duration;
95
+ for ( error_type, file_path) in errors {
96
+ stats. increment_error ( & error_type, & file_path) ;
97
+ }
94
98
}
95
99
}
96
100
@@ -130,14 +134,12 @@ impl Mailer {
130
134
131
135
current_batch. push ( file. clone ( ) ) ;
132
136
133
- // 当达到批处理大小或是最后一个文件时,发送这一批邮件
134
137
if current_batch. len ( ) >= config. batch_size || j == chunk. len ( ) - 1 {
135
138
info ! ( "进程组 {} 开始发送第 {}/{} 批,包含 {} 封邮件" ,
136
139
i + 1 , j / config. batch_size + 1 ,
137
140
( chunk. len( ) + config. batch_size - 1 ) / config. batch_size,
138
141
current_batch. len( ) ) ;
139
142
140
- // 为每个批次创建新的SMTP客户端
141
143
info ! ( "连接SMTP服务器: {}:{}" , config. smtp_server, config. port) ;
142
144
let client_result = match timeout ( Duration :: from_secs ( config. smtp_timeout ) ,
143
145
SmtpClientBuilder :: new ( config. smtp_server . as_str ( ) , config. port )
@@ -148,13 +150,14 @@ impl Mailer {
148
150
Ok ( result) => result,
149
151
Err ( _) => {
150
152
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
+ }
152
156
current_batch. clear ( ) ;
153
157
continue ;
154
158
}
155
159
} ;
156
160
157
- // 发送这一批邮件
158
161
match client_result {
159
162
Ok ( mut client) => {
160
163
match Self :: send_batch_emails ( & config, & current_batch, & mut client) . await {
@@ -167,13 +170,17 @@ impl Mailer {
167
170
}
168
171
Err ( e) => {
169
172
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
+ }
171
176
}
172
177
}
173
178
}
174
179
Err ( e) => {
175
180
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
+ }
177
184
}
178
185
}
179
186
@@ -193,8 +200,8 @@ impl Mailer {
193
200
total_sent += sent;
194
201
stats. parse_durations . extend ( parse_durations) ;
195
202
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 ) ;
198
205
}
199
206
}
200
207
}
@@ -251,10 +258,10 @@ impl Mailer {
251
258
async fn send_single_email ( config : & Config , file_path : & str ) -> Result < ( Duration , Duration ) > {
252
259
info ! ( "开始读取文件: {}" , file_path) ;
253
260
let parse_start = Instant :: now ( ) ;
254
- let content = fs:: read_to_string ( file_path) ?;
261
+ let content = fs:: read ( file_path) ?;
255
262
256
263
info ! ( "解析邮件内容" ) ;
257
- let message = match MessageParser :: default ( ) . parse ( content. as_bytes ( ) ) {
264
+ let message = match MessageParser :: default ( ) . parse ( & content) {
258
265
Some ( msg) => msg,
259
266
None => {
260
267
error ! ( "无法解析邮件文件: {}" , file_path) ;
@@ -319,10 +326,10 @@ impl Mailer {
319
326
for file_path in files {
320
327
info ! ( "开始读取文件: {}" , file_path) ;
321
328
let parse_start = Instant :: now ( ) ;
322
- let content = fs:: read_to_string ( file_path) ?;
329
+ let content = fs:: read ( file_path) ?;
323
330
324
331
info ! ( "解析邮件内容" ) ;
325
- let message = match MessageParser :: default ( ) . parse ( content. as_bytes ( ) ) {
332
+ let message = match MessageParser :: default ( ) . parse ( & content) {
326
333
Some ( msg) => msg,
327
334
None => {
328
335
error ! ( "无法解析邮件文件: {}" , file_path) ;
0 commit comments