@@ -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) ) ;
@@ -398,15 +399,57 @@ impl ExclusiveJob for Cleanup {
398399 }
399400}
400401
401- /// Accounts whose purge keeps failing, from [`FAILING_PURGES_COUNT`].
402- static FAILING_PURGES : Gauge = Gauge :: new (
403- "piquel.auth.account_purge.failing" ,
404- "{account}" ,
405- "Accounts scheduled for deletion whose purge failed at least 3 times in a row." ,
402+ /// A count the replica running a periodic job takes after each run, which its
403+ /// gauge reports. Every replica runs the job in turn, so each reports the count
404+ /// of its own latest run, at most one run older than the newest; one whose count
405+ /// is older than two runs, because it stopped running the job, reports none.
406+ /// Alerts read the minimum across replicas, or wait one run, before taking a
407+ /// count as current.
408+ pub ( crate ) struct JobCount {
409+ /// The gauge reporting it.
410+ gauge : Gauge ,
411+ /// How often the job runs.
412+ every : Duration ,
413+ /// The latest count on this replica, and when it was taken.
414+ counted : Mutex < Option < ( u64 , Instant ) > > ,
415+ }
416+
417+ impl JobCount {
418+ /// A count `gauge` reports, of a job that runs `every` so often.
419+ pub ( crate ) const fn new ( gauge : Gauge , every : Duration ) -> Self {
420+ Self {
421+ gauge,
422+ every,
423+ counted : Mutex :: new ( None ) ,
424+ }
425+ }
426+
427+ /// Registers the gauge.
428+ pub ( crate ) fn observe ( & ' static self ) {
429+ self . gauge . observe ( || {
430+ let counted = * self . counted . lock ( ) . unwrap_or_else ( PoisonError :: into_inner) ;
431+ counted
432+ . filter ( |( _, at) | at. elapsed ( ) < self . every * 2 )
433+ . map ( |( count, _) | ( count, ( ) ) )
434+ } ) ;
435+ }
436+
437+ /// Keeps `count`, taken now.
438+ pub ( crate ) fn set ( & self , count : u64 ) {
439+ * self . counted . lock ( ) . unwrap_or_else ( PoisonError :: into_inner) =
440+ Some ( ( count, Instant :: now ( ) ) ) ;
441+ }
442+ }
443+
444+ /// Accounts whose purge keeps failing, counted after each purge run.
445+ static FAILING_PURGES : JobCount = JobCount :: new (
446+ Gauge :: new (
447+ "piquel.auth.account_purge.failing" ,
448+ "{account}" ,
449+ "Accounts scheduled for deletion whose purge failed at least 3 times in a row." ,
450+ ) ,
451+ AccountPurge :: EVERY ,
406452) ;
407- /// The latest count of [`FAILING_PURGES`] on this replica, and when it was taken.
408- /// The replica running the purge counts after each run.
409- static FAILING_PURGES_COUNT : Mutex < Option < ( u64 , Instant ) > > = Mutex :: new ( None ) ;
410453
411454/// Purges the accounts whose deletion grace period ended, each in a transaction of
412455/// its own, attributed to [`JobName::AccountPurge`]. One run purges at most
@@ -425,22 +468,6 @@ impl AccountPurge {
425468 /// Failures in a row after which [`FAILING_PURGES`] counts an account.
426469 const FAILING_ATTEMPTS : u16 = 3 ;
427470
428- /// Registers [`FAILING_PURGES`]. Every replica runs the job in turn, so each
429- /// reports the count of its own latest run, at most one run older than the
430- /// newest; one whose count is older than two runs, because it stopped running
431- /// the job, reports none. Alerts read the minimum across replicas, or wait one
432- /// run, before taking a count as current.
433- fn observe ( ) {
434- FAILING_PURGES . observe ( || {
435- let counted = * FAILING_PURGES_COUNT
436- . lock ( )
437- . unwrap_or_else ( PoisonError :: into_inner) ;
438- counted
439- . filter ( |( _, at) | at. elapsed ( ) < Self :: EVERY * 2 )
440- . map ( |( count, _) | ( count, ( ) ) )
441- } ) ;
442- }
443-
444471 /// Purges `user_id` in `tx` if it is still scheduled for deletion and due,
445472 /// rechecked under the account lock, since it may have been restored meanwhile.
446473 pub ( crate ) async fn purge (
@@ -496,9 +523,7 @@ impl ExclusiveJob for AccountPurge {
496523 . deletions ( )
497524 . count_failing ( Self :: FAILING_ATTEMPTS )
498525 . await ?;
499- * FAILING_PURGES_COUNT
500- . lock ( )
501- . unwrap_or_else ( PoisonError :: into_inner) = Some ( ( failing, Instant :: now ( ) ) ) ;
526+ FAILING_PURGES . set ( failing) ;
502527 let mut tx = connection. begin ( ) . await ?;
503528 let context = EventContext :: system ( JobName :: AccountPurge ) ;
504529 let mut alerts = Alerts :: new ( & mut tx, & context) ;
0 commit comments