@@ -12,8 +12,10 @@ use mas_storage::{
1212    queue:: { Job ,  QueueJobRepository ,  Worker } , 
1313    Clock , 
1414} ; 
15+ use  opentelemetry_semantic_conventions:: trace:: DB_QUERY_TEXT ; 
1516use  rand:: RngCore ; 
1617use  sqlx:: PgConnection ; 
18+ use  tracing:: Instrument ; 
1719use  ulid:: Ulid ; 
1820use  uuid:: Uuid ; 
1921
@@ -137,6 +139,7 @@ impl QueueJobRepository for PgQueueJobRepository<'_> {
137139        payload :  serde_json:: Value , 
138140        metadata :  serde_json:: Value , 
139141        scheduled_at :  DateTime < Utc > , 
142+         schedule_name :  Option < & str > , 
140143    )  -> Result < ( ) ,  Self :: Error >  { 
141144        let  created_at = clock. now ( ) ; 
142145        let  id = Ulid :: from_datetime_with_source ( created_at. into ( ) ,  rng) ; 
@@ -145,20 +148,47 @@ impl QueueJobRepository for PgQueueJobRepository<'_> {
145148        sqlx:: query!( 
146149            r#" 
147150                INSERT INTO queue_jobs 
148-                     (queue_job_id, queue_name, payload, metadata, created_at, scheduled_at, status) 
149-                 VALUES ($1, $2, $3, $4, $5, $6, 'scheduled') 
151+                     (queue_job_id, queue_name, payload, metadata, created_at, scheduled_at, schedule_name,  status) 
152+                 VALUES ($1, $2, $3, $4, $5, $6, $7,  'scheduled') 
150153            "# , 
151154            Uuid :: from( id) , 
152155            queue_name, 
153156            payload, 
154157            metadata, 
155158            created_at, 
156159            scheduled_at, 
160+             schedule_name, 
157161        ) 
158162        . traced ( ) 
159163        . execute ( & mut  * self . conn ) 
160164        . await ?; 
161165
166+         // If there was a schedule name supplied, update the queue_schedules table 
167+         if  let  Some ( schedule_name)  = schedule_name { 
168+             let  span = tracing:: info_span!( 
169+                 "db.queue_job.schedule_later.update_schedules" , 
170+                 {  DB_QUERY_TEXT  }  = tracing:: field:: Empty , 
171+             ) ; 
172+ 
173+             let  res = sqlx:: query!( 
174+                 r#" 
175+                     UPDATE queue_schedules 
176+                     SET last_scheduled_at = $1, 
177+                         last_scheduled_job_id = $2 
178+                     WHERE schedule_name = $3 
179+                 "# , 
180+                 scheduled_at, 
181+                 Uuid :: from( id) , 
182+                 schedule_name, 
183+             ) 
184+             . record ( & span) 
185+             . execute ( & mut  * self . conn ) 
186+             . instrument ( span) 
187+             . await ?; 
188+ 
189+             DatabaseError :: ensure_affected_rows ( & res,  1 ) ?; 
190+         } 
191+ 
162192        Ok ( ( ) ) 
163193    } 
164194
@@ -315,14 +345,19 @@ impl QueueJobRepository for PgQueueJobRepository<'_> {
315345        let  scheduled_at = now + delay; 
316346        let  new_id = Ulid :: from_datetime_with_source ( now. into ( ) ,  rng) ; 
317347
348+         let  span = tracing:: info_span!( 
349+             "db.queue_job.retry.insert_job" , 
350+             {  DB_QUERY_TEXT  }  = tracing:: field:: Empty 
351+         ) ; 
318352        // Create a new job with the same payload and metadata, but a new ID and 
319353        // increment the attempt 
320354        // We make sure we do this only for 'failed' jobs 
321355        let  res = sqlx:: query!( 
322356            r#" 
323357                INSERT INTO queue_jobs 
324-                     (queue_job_id, queue_name, payload, metadata, created_at, attempt, scheduled_at, status) 
325-                 SELECT $1, queue_name, payload, metadata, $2, attempt + 1, $3, 'scheduled' 
358+                     (queue_job_id, queue_name, payload, metadata, created_at, 
359+                      attempt, scheduled_at, schedule_name, status) 
360+                 SELECT $1, queue_name, payload, metadata, $2, attempt + 1, $3, schedule_name, 'scheduled' 
326361                FROM queue_jobs 
327362                WHERE queue_job_id = $4 
328363                  AND status = 'failed' 
@@ -332,13 +367,39 @@ impl QueueJobRepository for PgQueueJobRepository<'_> {
332367            scheduled_at, 
333368            Uuid :: from( id) , 
334369        ) 
335-         . traced ( ) 
370+         . record ( & span ) 
336371        . execute ( & mut  * self . conn ) 
372+         . instrument ( span) 
337373        . await ?; 
338374
339375        DatabaseError :: ensure_affected_rows ( & res,  1 ) ?; 
340376
377+         // If that job was referenced by a schedule, update the schedule 
378+         let  span = tracing:: info_span!( 
379+             "db.queue_job.retry.update_schedule" , 
380+             {  DB_QUERY_TEXT  }  = tracing:: field:: Empty 
381+         ) ; 
382+         sqlx:: query!( 
383+             r#" 
384+                 UPDATE queue_schedules 
385+                 SET last_scheduled_at = $1, 
386+                     last_scheduled_job_id = $2 
387+                 WHERE last_scheduled_job_id = $3 
388+             "# , 
389+             scheduled_at, 
390+             Uuid :: from( new_id) , 
391+             Uuid :: from( id) , 
392+         ) 
393+         . record ( & span) 
394+         . execute ( & mut  * self . conn ) 
395+         . instrument ( span) 
396+         . await ?; 
397+ 
341398        // Update the old job to point to the new attempt 
399+         let  span = tracing:: info_span!( 
400+             "db.queue_job.retry.update_old_job" , 
401+             {  DB_QUERY_TEXT  }  = tracing:: field:: Empty 
402+         ) ; 
342403        let  res = sqlx:: query!( 
343404            r#" 
344405                UPDATE queue_jobs 
@@ -348,8 +409,9 @@ impl QueueJobRepository for PgQueueJobRepository<'_> {
348409            Uuid :: from( new_id) , 
349410            Uuid :: from( id) , 
350411        ) 
351-         . traced ( ) 
412+         . record ( & span ) 
352413        . execute ( & mut  * self . conn ) 
414+         . instrument ( span) 
353415        . await ?; 
354416
355417        DatabaseError :: ensure_affected_rows ( & res,  1 ) ?; 
0 commit comments