Skip to content

Commit 8581ca1

Browse files
committed
Prune stale policy data once a day
1 parent 97d2b75 commit 8581ca1

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

crates/storage/src/queue/tasks.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,3 +506,11 @@ impl ExpireInactiveUserSessionsJob {
506506
impl InsertableJob for ExpireInactiveUserSessionsJob {
507507
const QUEUE_NAME: &'static str = "expire-inactive-user-sessions";
508508
}
509+
510+
/// Prune stale policy data
511+
#[derive(Debug, Serialize, Deserialize)]
512+
pub struct PruneStalePolicyDataJob;
513+
514+
impl InsertableJob for PruneStalePolicyDataJob {
515+
const QUEUE_NAME: &'static str = "prune-stale-policy-data";
516+
}

crates/tasks/src/database.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! Database-related tasks
88
99
use async_trait::async_trait;
10-
use mas_storage::queue::CleanupExpiredTokensJob;
10+
use mas_storage::queue::{CleanupExpiredTokensJob, PruneStalePolicyDataJob};
1111
use tracing::{debug, info};
1212

1313
use crate::{
@@ -38,3 +38,28 @@ impl RunnableJob for CleanupExpiredTokensJob {
3838
Ok(())
3939
}
4040
}
41+
42+
#[async_trait]
43+
impl RunnableJob for PruneStalePolicyDataJob {
44+
#[tracing::instrument(name = "job.prune_stale_policy_data", skip_all, err)]
45+
async fn run(&self, state: &State, _context: JobContext) -> Result<(), JobError> {
46+
let mut repo = state.repository().await.map_err(JobError::retry)?;
47+
48+
// Keep the last 10 policy data
49+
let count = repo
50+
.policy_data()
51+
.prune(10)
52+
.await
53+
.map_err(JobError::retry)?;
54+
55+
repo.save().await.map_err(JobError::retry)?;
56+
57+
if count == 0 {
58+
debug!("no stale policy data to prune");
59+
} else {
60+
info!(count, "pruned stale policy data");
61+
}
62+
63+
Ok(())
64+
}
65+
}

crates/tasks/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ pub async fn init(
143143
.register_handler::<mas_storage::queue::ExpireInactiveCompatSessionsJob>()
144144
.register_handler::<mas_storage::queue::ExpireInactiveOAuthSessionsJob>()
145145
.register_handler::<mas_storage::queue::ExpireInactiveUserSessionsJob>()
146+
.register_handler::<mas_storage::queue::PruneStalePolicyDataJob>()
146147
.add_schedule(
147148
"cleanup-expired-tokens",
148149
"0 0 * * * *".parse()?,
@@ -153,6 +154,12 @@ pub async fn init(
153154
// Run this job every 15 minutes
154155
"30 */15 * * * *".parse()?,
155156
mas_storage::queue::ExpireInactiveSessionsJob,
157+
)
158+
.add_schedule(
159+
"prune-stale-policy-data",
160+
// Run once a day
161+
"0 0 2 * * *".parse()?,
162+
mas_storage::queue::PruneStalePolicyDataJob,
156163
);
157164

158165
task_tracker.spawn(worker.run());

0 commit comments

Comments
 (0)