@@ -13,6 +13,7 @@ import (
13
13
pg "github.com/djthorpe/go-pg"
14
14
httpresponse "github.com/mutablelogic/go-server/pkg/httpresponse"
15
15
schema "github.com/mutablelogic/go-server/pkg/pgqueue/schema"
16
+ ref "github.com/mutablelogic/go-server/pkg/ref"
16
17
types "github.com/mutablelogic/go-server/pkg/types"
17
18
)
18
19
@@ -462,26 +463,27 @@ func (manager *Manager) RunTaskLoop(ctx context.Context, ch chan<- *schema.Task)
462
463
463
464
// RunNotificationLoop runs a loop to process database notifications, until the context is cancelled
464
465
// or an error occurs.
465
- func (manager * Manager ) RunNotificationLoop (ctx context.Context , ch chan <- * pg.Notification ) error {
466
+ func (manager * Manager ) RunNotificationLoop (parent context.Context , ch chan <- * pg.Notification ) error {
466
467
// Subscribe to topics
467
468
for _ , topic := range manager .topics {
468
- if err := manager .listener .Listen (ctx , topic ); err != nil {
469
+ if err := manager .listener .Listen (parent , topic ); err != nil {
469
470
return err
470
471
}
471
472
}
472
473
defer func () {
474
+ ctx := ref .WithProvider (context .Background (), ref .Provider (parent ))
473
475
for _ , topic := range manager .topics {
474
- manager .listener .Unlisten (context . TODO () , topic )
476
+ manager .listener .Unlisten (ctx , topic )
475
477
}
476
478
}()
477
479
478
480
// Loop until context is cancelled
479
481
for {
480
482
select {
481
- case <- ctx .Done ():
483
+ case <- parent .Done ():
482
484
return nil
483
485
default :
484
- if notification , err := manager .listener .WaitForNotification (ctx ); err != nil {
486
+ if notification , err := manager .listener .WaitForNotification (parent ); err != nil {
485
487
if ! errors .Is (err , context .Canceled ) && ! errors .Is (err , context .DeadlineExceeded ) {
486
488
return err
487
489
}
0 commit comments