File tree Expand file tree Collapse file tree 3 files changed +41
-1
lines changed
Expand file tree Collapse file tree 3 files changed +41
-1
lines changed Original file line number Diff line number Diff line change @@ -506,3 +506,11 @@ impl ExpireInactiveUserSessionsJob {
506506impl 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+ }
Original file line number Diff line number Diff line change 77//! Database-related tasks
88
99use async_trait:: async_trait;
10- use mas_storage:: queue:: CleanupExpiredTokensJob ;
10+ use mas_storage:: queue:: { CleanupExpiredTokensJob , PruneStalePolicyDataJob } ;
1111use tracing:: { debug, info} ;
1212
1313use 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+ }
Original file line number Diff line number Diff 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 ( ) ) ;
You can’t perform that action at this time.
0 commit comments