@@ -2,16 +2,33 @@ use actix_web::*;
22use chrono:: Utc ;
33use diesel:: prelude:: * ;
44use serde_json:: json;
5+ use std:: sync:: Arc ;
56
67use crate :: errors:: ApiError ;
8+ use crate :: metrics:: { kind_label, repo_label, JobLabels , Metrics } ;
79use crate :: models:: * ;
810use crate :: schema;
911use crate :: Pool ;
1012
1113#[ derive( Clone ) ]
12- pub struct Db ( pub Pool ) ;
14+ pub struct Db {
15+ pub pool : Pool ,
16+ pub metrics : Option < Arc < Metrics > > ,
17+ }
1318
1419impl Db {
20+ fn increment_queued_job_metric ( & self , job : & Job ) {
21+ if let Some ( metrics) = & self . metrics {
22+ metrics
23+ . jobs_queued
24+ . get_or_create ( & JobLabels {
25+ kind : kind_label ( job. kind ) ,
26+ repo : repo_label ( & job. repo ) ,
27+ } )
28+ . inc ( ) ;
29+ }
30+ }
31+
1532 async fn run < Func , T > ( & self , func : Func ) -> Result < T , ApiError >
1633 where
1734 Func : FnOnce (
@@ -20,7 +37,7 @@ impl Db {
2037 Func : Send + ' static ,
2138 T : Send + ' static ,
2239 {
23- let p = self . 0 . clone ( ) ;
40+ let p = self . pool . clone ( ) ;
2441 web:: block ( move || {
2542 let mut conn = p. get ( ) ?;
2643 func ( & mut conn)
@@ -139,6 +156,10 @@ impl Db {
139156 Ok ( job)
140157 } )
141158 . await
159+ . map ( |job| {
160+ self . increment_queued_job_metric ( & job) ;
161+ job
162+ } )
142163 }
143164
144165 pub async fn start_publish_job ( & self , build_id : i32 , repo : String ) -> Result < Job , ApiError > {
@@ -237,6 +258,10 @@ impl Db {
237258 Ok ( job)
238259 } )
239260 . await
261+ . map ( |job| {
262+ self . increment_queued_job_metric ( & job) ;
263+ job
264+ } )
240265 }
241266
242267 pub async fn start_republish_job (
@@ -264,6 +289,10 @@ impl Db {
264289 Ok ( job)
265290 } )
266291 . await
292+ . map ( |job| {
293+ self . increment_queued_job_metric ( & job) ;
294+ job
295+ } )
267296 }
268297
269298 pub async fn start_prune_job ( & self , repo : String ) -> Result < Job , ApiError > {
@@ -279,6 +308,10 @@ impl Db {
279308 . map_err ( ApiError :: from)
280309 } )
281310 . await
311+ . map ( |job| {
312+ self . increment_queued_job_metric ( & job) ;
313+ job
314+ } )
282315 }
283316
284317 /* Checks */
0 commit comments