Compare commits
2 commits
84adeddec6
...
c8854fcbd4
Author | SHA1 | Date | |
---|---|---|---|
|
c8854fcbd4 | ||
|
8bc9126264 |
1 changed files with 24 additions and 13 deletions
25
src/main.rs
25
src/main.rs
|
@ -3,8 +3,8 @@ use eyre::{eyre, OptionExt, WrapErr};
|
|||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::convert::TryFrom;
|
||||
use std::{fmt,env};
|
||||
use std::time::{Duration, SystemTime};
|
||||
use std::{env, fmt};
|
||||
|
||||
const RESTIC_ALARM_BUCKET: &str = "restic-alarm-state";
|
||||
const RESTIC_ALARM_STATE_FILE: &str = "state.toml";
|
||||
|
@ -18,8 +18,8 @@ struct SmtpConfig {
|
|||
|
||||
impl SmtpConfig {
|
||||
fn mailer(&self) -> Result<lettre::SmtpTransport, lettre::transport::smtp::Error> {
|
||||
use lettre::SmtpTransport;
|
||||
use lettre::transport::smtp::authentication::Credentials;
|
||||
use lettre::SmtpTransport;
|
||||
Ok(SmtpTransport::relay(&self.address)?
|
||||
.credentials(Credentials::new(
|
||||
self.username.to_owned(),
|
||||
|
@ -32,9 +32,13 @@ impl SmtpConfig {
|
|||
let address = env::var("SMTP_ADDRESS")?;
|
||||
let username = env::var("SMTP_USERNAME")?;
|
||||
let password = env::var("SMTP_PASSWORD")?;
|
||||
let smtp = SmtpConfig { address, username, password };
|
||||
let smtp = SmtpConfig {
|
||||
address,
|
||||
username,
|
||||
password,
|
||||
};
|
||||
if !smtp.mailer()?.test_connection()? {
|
||||
return Err(eyre!("Unable to contact the SMTP relay"))
|
||||
return Err(eyre!("Unable to contact the SMTP relay"));
|
||||
}
|
||||
Ok(smtp)
|
||||
}
|
||||
|
@ -89,15 +93,17 @@ impl fmt::Display for AlertStatus {
|
|||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let days = |d: Duration| d.as_secs() / (3600 * 24);
|
||||
let hours = |d: Duration| (d.as_secs() / 3600) % 24;
|
||||
let minutes = |d: Duration| (d.as_secs() / 60) % 60;
|
||||
write!(
|
||||
f,
|
||||
"{}: inactivity: {} days, {} hours, last_alert: {}",
|
||||
"{}: inactivity: {}d{}h{}m, last_alert: {}",
|
||||
(if self.alert { "ALERT" } else { "no alert" }),
|
||||
days(self.inactivity),
|
||||
hours(self.inactivity),
|
||||
minutes(self.inactivity),
|
||||
match self.last_alert {
|
||||
None => "none".to_owned(),
|
||||
Some(d) => format!("{} days, {} hours", days(d), hours(d)),
|
||||
Some(d) => format!("{}d{}h{}m", days(d), hours(d), minutes(d)),
|
||||
}
|
||||
)
|
||||
}
|
||||
|
@ -275,7 +281,12 @@ struct RepoInfo {
|
|||
// (e.g. if it fails to parse).
|
||||
// So the error must not be propagated to the toplevel, which would abort the
|
||||
// alert for remaining repositories; it should instead just be reported/logged.
|
||||
async fn check_repo(smtp: &SmtpConfig, client: &s3::Client, state: &mut State, repo: &str) -> eyre::Result<RepoInfo> {
|
||||
async fn check_repo(
|
||||
smtp: &SmtpConfig,
|
||||
client: &s3::Client,
|
||||
state: &mut State,
|
||||
repo: &str,
|
||||
) -> eyre::Result<RepoInfo> {
|
||||
let config = read_repo_config(client, repo).await?;
|
||||
let alert_status = if let Some(inactivity) = repo_last_snapshot(client, repo).await? {
|
||||
let last_alert = state.last_alert(repo);
|
||||
|
|
Loading…
Reference in a new issue