@@ -28,6 +28,7 @@ import (
2828 "go.senan.xyz/wrtag"
2929 wrtagflag "go.senan.xyz/wrtag/cmd/internal/wrtagflag"
3030 "go.senan.xyz/wrtag/cmd/internal/wrtaglog"
31+ "go.senan.xyz/wrtag/notifications"
3132 "go.senan.xyz/wrtag/researchlink"
3233
3334 _ "github.com/ncruces/go-sqlite3/driver"
@@ -58,7 +59,7 @@ func main() {
5859 wrtagflag .DefaultClient ()
5960 var (
6061 cfg = wrtagflag .Config ()
61- notifications = wrtagflag .Notifications ()
62+ notifs = wrtagflag .Notifications ()
6263 researchLinkQuerier = wrtagflag .ResearchLinks ()
6364 apiKey = flag .String ("web-api-key" , "" , "API key for web interface" )
6465 listenAddr = flag .String ("web-listen-addr" , ":7373" , "Listen address for web interface (optional)" )
@@ -185,11 +186,20 @@ func main() {
185186 return err
186187 }
187188
188- switch job .Status {
189- case StatusComplete :
190- go notifications .Send (context .WithoutCancel (ctx ), notifComplete , jobNotificationMessage (* publicURL , job ))
191- case StatusNeedsInput :
192- go notifications .Send (context .WithoutCancel (ctx ), notifNeedsInput , jobNotificationMessage (* publicURL , job ))
189+ {
190+ ctx := context .WithoutCancel (ctx )
191+
192+ // If we have no action time in the ctx, use the job's updated at
193+ if job .UpdatedTime .Valid && notifications .ActionTime (ctx ).IsZero () {
194+ ctx = notifications .RecordActionTime (ctx , job .UpdatedTime .Time )
195+ }
196+
197+ switch job .Status {
198+ case StatusComplete :
199+ go notifs .Send (ctx , notifComplete , jobNotificationMessage (* publicURL , job ))
200+ case StatusNeedsInput :
201+ go notifs .Send (ctx , notifNeedsInput , jobNotificationMessage (* publicURL , job ))
202+ }
193203 }
194204
195205 return nil
@@ -266,7 +276,9 @@ func main() {
266276 w .WriteHeader (http .StatusOK )
267277 rc .Flush ()
268278
269- for id := range sse .receive (r .Context (), 0 ) {
279+ ctx := r .Context ()
280+
281+ for id := range sse .receive (ctx , 0 ) {
270282 fmt .Fprintf (w , "data: %d\n \n " , id )
271283 rc .Flush ()
272284 }
@@ -276,7 +288,10 @@ func main() {
276288 search := r .URL .Query ().Get ("search" )
277289 filter := JobStatus (r .URL .Query ().Get ("filter" ))
278290 page , _ := strconv .Atoi (r .URL .Query ().Get ("page" ))
279- jl , err := listJobs (r .Context (), filter , search , page )
291+
292+ ctx := r .Context ()
293+
294+ jl , err := listJobs (ctx , filter , search , page )
280295 if err != nil {
281296 respErrf (w , http .StatusInternalServerError , "error listing jobs: %v" , err )
282297 return
@@ -301,8 +316,10 @@ func main() {
301316 }
302317 path = filepath .Clean (path )
303318
319+ ctx := r .Context ()
320+
304321 var job Job
305- if err := sqlb .ScanRow (r . Context () , db , & job , "insert into jobs (source_path, operation, time) values (?, ?, ?) returning *" , path , operationStr , time .Now ()); err != nil {
322+ if err := sqlb .ScanRow (ctx , db , & job , "insert into jobs (source_path, operation, time) values (?, ?, ?) returning *" , path , operationStr , time .Now ()); err != nil {
306323 http .Error (w , fmt .Sprintf ("error saving job: %v" , err ), http .StatusInternalServerError )
307324 return
308325 }
@@ -314,8 +331,11 @@ func main() {
314331
315332 mux .HandleFunc ("GET /jobs/{id}" , func (w http.ResponseWriter , r * http.Request ) {
316333 id , _ := strconv .Atoi (r .PathValue ("id" ))
334+
335+ ctx := r .Context ()
336+
317337 var job Job
318- if err := sqlb .ScanRow (r . Context () , db , & job , "select * from jobs where id=?" , id ); err != nil {
338+ if err := sqlb .ScanRow (ctx , db , & job , "select * from jobs where id=?" , id ); err != nil {
319339 respErrf (w , http .StatusInternalServerError , "error getting job" )
320340 return
321341 }
@@ -332,8 +352,10 @@ func main() {
332352 useMBID = filepath .Base (useMBID ) // accept release URL
333353 }
334354
355+ ctx := r .Context ()
356+
335357 var job Job
336- if err := sqlb .ScanRow (r . Context () , db , & job , "update jobs set confirm=?, use_mbid=?, status=?, updated_time=? where id=? and status<>? returning *" , confirm , useMBID , StatusEnqueued , time .Now (), id , StatusInProgress ); err != nil {
358+ if err := sqlb .ScanRow (ctx , db , & job , "update jobs set confirm=?, use_mbid=?, status=?, updated_time=? where id=? and status<>? returning *" , confirm , useMBID , StatusEnqueued , time .Now (), id , StatusInProgress ); err != nil {
337359 respErrf (w , http .StatusInternalServerError , "error getting job" )
338360 return
339361 }
@@ -345,7 +367,10 @@ func main() {
345367
346368 mux .HandleFunc ("DELETE /jobs/{id}" , func (w http.ResponseWriter , r * http.Request ) {
347369 id , _ := strconv .Atoi (r .PathValue ("id" ))
348- if err := sqlb .Exec (r .Context (), db , "delete from jobs where id=? and status<>?" , id , StatusInProgress ); err != nil {
370+
371+ ctx := r .Context ()
372+
373+ if err := sqlb .Exec (ctx , db , "delete from jobs where id=? and status<>?" , id , StatusInProgress ); err != nil {
349374 respErrf (w , http .StatusInternalServerError , "error getting job" )
350375 return
351376 }
@@ -384,7 +409,9 @@ func main() {
384409 })
385410
386411 mux .HandleFunc ("/{$}" , func (w http.ResponseWriter , r * http.Request ) {
387- jl , err := listJobs (r .Context (), "" , "" , 0 )
412+ ctx := r .Context ()
413+
414+ jl , err := listJobs (ctx , "" , "" , 0 )
388415 if err != nil {
389416 respErrf (w , http .StatusInternalServerError , "error listing jobs: %v" , err )
390417 return
@@ -417,7 +444,9 @@ func main() {
417444 }
418445 path = filepath .Clean (path )
419446
420- if err := sqlb .Exec (r .Context (), db , "insert into jobs (source_path, operation, time) values (?, ?, ?)" , path , operationStr , time .Now ()); err != nil {
447+ ctx := r .Context ()
448+
449+ if err := sqlb .Exec (ctx , db , "insert into jobs (source_path, operation, time) values (?, ?, ?)" , path , operationStr , time .Now ()); err != nil {
421450 http .Error (w , fmt .Sprintf ("error saving job: %v" , err ), http .StatusInternalServerError )
422451 return
423452 }
0 commit comments