@@ -99,11 +99,12 @@ impl Jobs {
9999 ) -> Result < Self , StartupError > {
100100 let mut jobs = Self :: new ( ) ;
101101 jobs. exclusive ( Cleanup :: new ( ) , database. clone ( ) ) ;
102- AccountPurge :: observe ( ) ;
102+ FAILING_PURGES . observe ( ) ;
103103 jobs. exclusive ( AccountPurge , database. clone ( ) ) ;
104104 jobs. exclusive ( Sealer :: new ( & config. audit ) , database. clone ( ) ) ;
105105 let audit_keys = AuditKeys :: for_server ( & config. passkey . origin , config. audit . verifier ( ) )
106106 . map_err ( StartupError :: AuditKeys ) ?;
107+ Verifier :: observe ( ) ;
107108 jobs. exclusive ( Verifier :: new ( audit_keys) , database. clone ( ) ) ;
108109 let ( store, reloaded) = ( store. clone ( ) , keys. clone ( ) ) ;
109110 jobs. spawn ( |shutdown| store. run ( reloaded, shutdown) ) ;
@@ -376,15 +377,57 @@ impl ExclusiveJob for Cleanup {
376377 }
377378}
378379
379- /// Accounts whose purge keeps failing, from [`FAILING_PURGES_COUNT`].
380- static FAILING_PURGES : Gauge = Gauge :: new (
381- "piquel.auth.account_purge.failing" ,
382- "{account}" ,
383- "Accounts scheduled for deletion whose purge failed at least 3 times in a row." ,
380+ /// A count the replica running a periodic job takes after each run, which its
381+ /// gauge reports. Every replica runs the job in turn, so each reports the count
382+ /// of its own latest run, at most one run older than the newest; one whose count
383+ /// is older than two runs, because it stopped running the job, reports none.
384+ /// Alerts read the minimum across replicas, or wait one run, before taking a
385+ /// count as current.
386+ pub ( crate ) struct JobCount {
387+ /// The gauge reporting it.
388+ gauge : Gauge ,
389+ /// How often the job runs.
390+ every : Duration ,
391+ /// The latest count on this replica, and when it was taken.
392+ counted : Mutex < Option < ( u64 , Instant ) > > ,
393+ }
394+
395+ impl JobCount {
396+ /// A count `gauge` reports, of a job that runs `every` so often.
397+ pub ( crate ) const fn new ( gauge : Gauge , every : Duration ) -> Self {
398+ Self {
399+ gauge,
400+ every,
401+ counted : Mutex :: new ( None ) ,
402+ }
403+ }
404+
405+ /// Registers the gauge.
406+ pub ( crate ) fn observe ( & ' static self ) {
407+ self . gauge . observe ( || {
408+ let counted = * self . counted . lock ( ) . unwrap_or_else ( PoisonError :: into_inner) ;
409+ counted
410+ . filter ( |( _, at) | at. elapsed ( ) < self . every * 2 )
411+ . map ( |( count, _) | ( count, ( ) ) )
412+ } ) ;
413+ }
414+
415+ /// Keeps `count`, taken now.
416+ pub ( crate ) fn set ( & self , count : u64 ) {
417+ * self . counted . lock ( ) . unwrap_or_else ( PoisonError :: into_inner) =
418+ Some ( ( count, Instant :: now ( ) ) ) ;
419+ }
420+ }
421+
422+ /// Accounts whose purge keeps failing, counted after each purge run.
423+ static FAILING_PURGES : JobCount = JobCount :: new (
424+ Gauge :: new (
425+ "piquel.auth.account_purge.failing" ,
426+ "{account}" ,
427+ "Accounts scheduled for deletion whose purge failed at least 3 times in a row." ,
428+ ) ,
429+ AccountPurge :: EVERY ,
384430) ;
385- /// The latest count of [`FAILING_PURGES`] on this replica, and when it was taken.
386- /// The replica running the purge counts after each run.
387- static FAILING_PURGES_COUNT : Mutex < Option < ( u64 , Instant ) > > = Mutex :: new ( None ) ;
388431
389432/// Purges the accounts whose deletion grace period ended, each in a transaction of
390433/// its own, attributed to [`JobName::AccountPurge`]. One run purges at most
@@ -403,22 +446,6 @@ impl AccountPurge {
403446 /// Failures in a row after which [`FAILING_PURGES`] counts an account.
404447 const FAILING_ATTEMPTS : u16 = 3 ;
405448
406- /// Registers [`FAILING_PURGES`]. Every replica runs the job in turn, so each
407- /// reports the count of its own latest run, at most one run older than the
408- /// newest; one whose count is older than two runs, because it stopped running
409- /// the job, reports none. Alerts read the minimum across replicas, or wait one
410- /// run, before taking a count as current.
411- fn observe ( ) {
412- FAILING_PURGES . observe ( || {
413- let counted = * FAILING_PURGES_COUNT
414- . lock ( )
415- . unwrap_or_else ( PoisonError :: into_inner) ;
416- counted
417- . filter ( |( _, at) | at. elapsed ( ) < Self :: EVERY * 2 )
418- . map ( |( count, _) | ( count, ( ) ) )
419- } ) ;
420- }
421-
422449 /// Purges `user_id` in `tx` if it is still scheduled for deletion and due,
423450 /// rechecked under the account lock, since it may have been restored meanwhile.
424451 pub ( crate ) async fn purge (
@@ -474,9 +501,7 @@ impl ExclusiveJob for AccountPurge {
474501 . deletions ( )
475502 . count_failing ( Self :: FAILING_ATTEMPTS )
476503 . await ?;
477- * FAILING_PURGES_COUNT
478- . lock ( )
479- . unwrap_or_else ( PoisonError :: into_inner) = Some ( ( failing, Instant :: now ( ) ) ) ;
504+ FAILING_PURGES . set ( failing) ;
480505 let mut tx = connection. begin ( ) . await ?;
481506 let context = EventContext :: system ( JobName :: AccountPurge ) ;
482507 let mut alerts = Alerts :: new ( & mut tx, & context) ;
0 commit comments