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 {
506
506
impl InsertableJob for ExpireInactiveUserSessionsJob {
507
507
const QUEUE_NAME : & ' static str = "expire-inactive-user-sessions" ;
508
508
}
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 7
7
//! Database-related tasks
8
8
9
9
use async_trait:: async_trait;
10
- use mas_storage:: queue:: CleanupExpiredTokensJob ;
10
+ use mas_storage:: queue:: { CleanupExpiredTokensJob , PruneStalePolicyDataJob } ;
11
11
use tracing:: { debug, info} ;
12
12
13
13
use crate :: {
@@ -38,3 +38,28 @@ impl RunnableJob for CleanupExpiredTokensJob {
38
38
Ok ( ( ) )
39
39
}
40
40
}
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(
143
143
. register_handler :: < mas_storage:: queue:: ExpireInactiveCompatSessionsJob > ( )
144
144
. register_handler :: < mas_storage:: queue:: ExpireInactiveOAuthSessionsJob > ( )
145
145
. register_handler :: < mas_storage:: queue:: ExpireInactiveUserSessionsJob > ( )
146
+ . register_handler :: < mas_storage:: queue:: PruneStalePolicyDataJob > ( )
146
147
. add_schedule (
147
148
"cleanup-expired-tokens" ,
148
149
"0 0 * * * *" . parse ( ) ?,
@@ -153,6 +154,12 @@ pub async fn init(
153
154
// Run this job every 15 minutes
154
155
"30 */15 * * * *" . parse ( ) ?,
155
156
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 ,
156
163
) ;
157
164
158
165
task_tracker. spawn ( worker. run ( ) ) ;
You can’t perform that action at this time.
0 commit comments