2个稳定版本
1.0.1 | 2023年1月3日 |
---|
#209 in 电子邮件
9KB
119 行
email-notif-rs
进程状态的通知。对于长运行进程(例如训练ML模型或运行模拟),在完成时收到通知可能很有用。电子邮件是一种方便的接收方式。
这是我的Python脚本 email_notifier
的Rust实现。
安装
此包位于cargo上。将依赖项添加到项目中:
[dependency]
email-notif-rs = "1.0.0"
使用
您需要告诉库从哪里发送电子邮件。我不想在程序中放置电子邮件配置,所以它在您的家目录中的一个配置文件中。发送电子邮件应该是您不介意在JSON文件中明文包含密码的电子邮件(即为发送通知而设置的电子邮件)。
{
"smtp_server": "smtp.example.com",
"sender_email": "[email protected]",
"password": "notVerySecureAtAll",
"recipient_email": "[email protected]",
"port": 587
}
设置配置后,您现在可以从程序中使用库如下
use email_notif::EmailNotifier;
use crate::foo::long_running_process;
fn main() {
EmailNotifier::new("Simulation Run")
.capture(
|em| {
for i in 0..10 {
long_running_process();
em.send_update(format!("iteration {i} complete"));
}
}
);
}
上述代码将在每次迭代完成时发送一个更新通知,并在进程成功完成时发送电子邮件。此外,如果进程导致panic,也会发送电子邮件。(但是,仅限于unwind panic。)
依赖项
~2–12MB
~166K SLoC