chore: migrate authifier into codebase (#658)
Co-authored-by: izzy <me@insrt.uk> Signed-off-by: Zomatree <me@zomatree.live> Signed-off-by: izzy <me@insrt.uk>
This commit is contained in:
@@ -15,6 +15,7 @@ log = { workspace = true }
|
||||
|
||||
# Async
|
||||
tokio = { workspace = true }
|
||||
futures = { workspace = true }
|
||||
|
||||
# Redis
|
||||
redis-kiss = { workspace = true }
|
||||
|
||||
@@ -1,23 +1,51 @@
|
||||
use revolt_config::configure;
|
||||
use revolt_database::{DatabaseInfo, AMQP};
|
||||
use std::{future::Future, panic::AssertUnwindSafe, time::Duration};
|
||||
|
||||
use futures::FutureExt;
|
||||
use revolt_config::{capture_error, configure};
|
||||
use revolt_database::{Database, DatabaseInfo, AMQP};
|
||||
use revolt_result::Result;
|
||||
use tasks::{acks, file_deletion, prune_dangling_files, prune_members};
|
||||
use tokio::try_join;
|
||||
use tasks::*;
|
||||
use tokio::{join, time::sleep};
|
||||
|
||||
pub mod tasks;
|
||||
|
||||
pub async fn cron_task_wrapper<Fut: Future<Output = Result<()>>>(
|
||||
func: fn(Database, AMQP) -> Fut,
|
||||
db: Database,
|
||||
amqp: AMQP,
|
||||
) {
|
||||
loop {
|
||||
let wrapper = AssertUnwindSafe(func(db.clone(), amqp.clone()));
|
||||
|
||||
match wrapper.catch_unwind().await {
|
||||
Ok(Ok(())) => {
|
||||
log::error!("cron unexpectedly finshed, Retrying after 60s");
|
||||
}
|
||||
Ok(Err(error)) => {
|
||||
log::error!("cron task failed unexpectedly: {error:?}\nRetrying after 60s");
|
||||
capture_error(&error);
|
||||
}
|
||||
_ => {
|
||||
log::error!("cron task failed unexpectedly\nRetrying after 60s");
|
||||
}
|
||||
}
|
||||
|
||||
sleep(Duration::from_secs(60)).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
async fn main() {
|
||||
configure!(crond);
|
||||
|
||||
let db = DatabaseInfo::Auto.connect().await.expect("database");
|
||||
let amqp = AMQP::new_auto().await;
|
||||
|
||||
try_join!(
|
||||
file_deletion::task(db.clone()),
|
||||
prune_dangling_files::task(db.clone()),
|
||||
prune_members::task(db.clone()),
|
||||
acks::task(db.clone(), amqp.clone()),
|
||||
)
|
||||
.map(|_| ())
|
||||
join!(
|
||||
cron_task_wrapper(file_deletion::task, db.clone(), amqp.clone()),
|
||||
cron_task_wrapper(prune_dangling_files::task, db.clone(), amqp.clone()),
|
||||
cron_task_wrapper(prune_members::task, db.clone(), amqp.clone()),
|
||||
cron_task_wrapper(delete_accounts::task, db.clone(), amqp.clone()),
|
||||
cron_task_wrapper(acks::task, db.clone(), amqp.clone()),
|
||||
);
|
||||
}
|
||||
|
||||
23
crates/daemons/crond/src/tasks/delete_accounts.rs
Normal file
23
crates/daemons/crond/src/tasks/delete_accounts.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use revolt_database::Database;
|
||||
use revolt_result::Result;
|
||||
use tokio::time::sleep;
|
||||
|
||||
pub async fn task(db: Database, _: revolt_database::AMQP) -> Result<()> {
|
||||
loop {
|
||||
let accounts = db.fetch_accounts_due_for_deletion().await?;
|
||||
let count = accounts.len();
|
||||
|
||||
for mut account in accounts {
|
||||
let mut user = db.fetch_user(&account.id).await?;
|
||||
|
||||
user.delete(&db).await?;
|
||||
account.mark_deleted(&db).await?;
|
||||
}
|
||||
|
||||
log::info!("Deleted {count} accounts.");
|
||||
|
||||
sleep(Duration::from_hours(1)).await
|
||||
}
|
||||
}
|
||||
@@ -6,21 +6,17 @@ use revolt_files::delete_from_s3;
|
||||
use revolt_result::Result;
|
||||
use tokio::time::sleep;
|
||||
|
||||
pub async fn task(db: Database) -> Result<()> {
|
||||
pub async fn task(db: Database, _: revolt_database::AMQP) -> Result<()> {
|
||||
loop {
|
||||
let files = db.fetch_deleted_attachments().await?;
|
||||
|
||||
for file in files {
|
||||
if let Some(hash) = &file.hash {
|
||||
let count = db
|
||||
.count_file_hash_references(hash)
|
||||
.await?;
|
||||
let count = db.count_file_hash_references(hash).await?;
|
||||
|
||||
// No other files reference this file on disk anymore
|
||||
if count <= 1 {
|
||||
let file_hash = db
|
||||
.fetch_attachment_hash(hash)
|
||||
.await?;
|
||||
let file_hash = db.fetch_attachment_hash(hash).await?;
|
||||
|
||||
// Delete from S3
|
||||
delete_from_s3(&file_hash.bucket_id, &file_hash.path).await?;
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
pub mod delete_accounts;
|
||||
pub mod acks;
|
||||
pub mod file_deletion;
|
||||
pub mod prune_dangling_files;
|
||||
pub mod prune_members;
|
||||
pub mod prune_mfa_tickets;
|
||||
|
||||
@@ -6,7 +6,7 @@ use tokio::time::sleep;
|
||||
|
||||
use log::info;
|
||||
|
||||
pub async fn task(db: Database) -> Result<()> {
|
||||
pub async fn task(db: Database, _: revolt_database::AMQP) -> Result<()> {
|
||||
loop {
|
||||
// This could just be a single database query
|
||||
// ... but timestamps are inconsistently serialised
|
||||
|
||||
@@ -5,7 +5,7 @@ use revolt_database::Database;
|
||||
use revolt_result::Result;
|
||||
use tokio::time::sleep;
|
||||
|
||||
pub async fn task(db: Database) -> Result<()> {
|
||||
pub async fn task(db: Database, _: revolt_database::AMQP) -> Result<()> {
|
||||
loop {
|
||||
let success = db.remove_dangling_members().await;
|
||||
if let Err(s) = success {
|
||||
|
||||
14
crates/daemons/crond/src/tasks/prune_mfa_tickets.rs
Normal file
14
crates/daemons/crond/src/tasks/prune_mfa_tickets.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use revolt_database::Database;
|
||||
use revolt_result::Result;
|
||||
use tokio::time::sleep;
|
||||
|
||||
pub async fn task(db: Database) -> Result<()> {
|
||||
loop {
|
||||
let count = db.delete_expired_tickets().await?;
|
||||
log::info!("Pruned {count} expired MFA tickets");
|
||||
|
||||
sleep(Duration::from_mins(5)).await
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user