Skip to content

Commit

Permalink
Clean up events
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgb committed May 17, 2024
1 parent 68bc278 commit 80f3cce
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion plane/src/cleanup.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::database::PlaneDatabase;
use crate::database::{subscribe::EventSubscriptionManager, PlaneDatabase};
use anyhow::Result;

const CLEANUP_LOOP_INTERVAL_SECONDS: u64 = 60 * 15;
Expand All @@ -8,6 +8,7 @@ pub async fn run_cleanup(db: &PlaneDatabase, min_age_days: Option<i32>) -> Resul

if let Some(min_age_days) = min_age_days {
db.backend().cleanup(min_age_days).await?;
EventSubscriptionManager::clean_up_events(&db.pool, min_age_days).await?;
}

db.clean_up_tokens().await?;
Expand Down
2 changes: 1 addition & 1 deletion plane/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub async fn connect(db: &str) -> sqlx::Result<PlaneDatabase> {

#[derive(Clone)]
pub struct PlaneDatabase {
pool: PgPool,
pub pool: PgPool,
subscription_manager: Arc<OnceLock<EventSubscriptionManager>>,
}

Expand Down
18 changes: 18 additions & 0 deletions plane/src/database/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,24 @@ impl EventSubscriptionManager {
}
}

pub async fn clean_up_events(db: &PgPool, min_age_days: i32) -> Result<(), sqlx::Error> {
sqlx::query!(
r#"
delete from event
where id in (
select id
from event
where created_at < now() - make_interval(days => $1)
)
"#,
min_age_days
)
.execute(db)
.await?;

Ok(())
}

pub async fn get_events_since(
db: &PgPool,
since: i32,
Expand Down

0 comments on commit 80f3cce

Please sign in to comment.