Skip to content

Commit

Permalink
No-show auto expiration task
Browse files Browse the repository at this point in the history
  • Loading branch information
Celeo committed Nov 19, 2024
1 parent 2c556c8 commit 782967f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions vzdv-site/templates/changelog.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<div class="card-text">
<ul>
<li>Modifications to the Discord no-show integration</li>
<li>No-shows now automatically expire</li>
</ul>
</div>
</div>
Expand Down
24 changes: 23 additions & 1 deletion vzdv-tasks/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use tokio::time;
use vzdv::general_setup;

mod activity;
mod no_show_expiration;
mod roster;
mod solo_cert;

Expand Down Expand Up @@ -114,15 +115,36 @@ async fn main() {
error!("Error checking for solo cert expiration: {e}");
}
}
debug!("Waiting 30 minutes for next roster sync");
debug!("Waiting 30 minutes for next solo cert expiration check");
time::sleep(Duration::from_secs(60 * 30)).await;
}
})
};

let no_show_expiration_handle = {
let db: sqlx::Pool<sqlx::Sqlite> = db.clone();
tokio::spawn(async move {
debug!("Waiting 15 seconds before starting no-show expiration check");
time::sleep(Duration::from_secs(15)).await;
loop {
match no_show_expiration::check_expired(&db).await {
Ok(_) => {
debug!("No-show expiration checked");
}
Err(e) => {
error!("Error checking for no-show expiration: {e}");
}
}
debug!("Waiting 12 hours for next no-show expiration check");
time::sleep(Duration::from_secs(60 * 60 * 12)).await;
}
})
};

roster_handle.await.unwrap();
activity_handle.await.unwrap();
solo_cert_handle.await.unwrap();
no_show_expiration_handle.await.unwrap();

db.close().await;
}
35 changes: 35 additions & 0 deletions vzdv-tasks/src/no_show_expiration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use anyhow::{anyhow, Result};
use chrono::{Months, Utc};
use log::info;
use sqlx::{Pool, Sqlite};
use vzdv::sql::{self, NoShow};

pub async fn check_expired(db: &Pool<Sqlite>) -> Result<()> {
let no_shows: Vec<NoShow> = sqlx::query_as(sql::GET_ALL_NO_SHOW).fetch_all(db).await?;
let now = Utc::now();

for entry in no_shows {
let has_expired = entry
.created_date
.checked_add_months(Months::new(6))
.ok_or_else(|| anyhow!("could not compute no-show expiration date"))?
< now;
if has_expired {
info!("No-show {} has expired", entry.id,);
sqlx::query(sql::DELETE_NO_SHOW_ENTRY)
.bind(entry.id)
.execute(db)
.await?;
info!(
"No-show {} ({}) for {} from {} created {} deleted",
entry.id,
entry.entry_type,
entry.cid,
entry.reported_by,
entry.created_date.to_rfc3339()
);
}
}

Ok(())
}
1 change: 1 addition & 0 deletions vzdv-tasks/src/solo_cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub async fn check_expired(db: &Pool<Sqlite>) -> Result<()> {
.fetch_all(db)
.await?;
let now = Utc::now();

for cert in solo_certs {
if cert.expiration_date < now {
info!(
Expand Down

0 comments on commit 782967f

Please sign in to comment.