Bound shutdown hooks by their stop context (closes #102)
All checks were successful
check / check (push) Successful in 3m49s

This commit was merged in pull request #130.
This commit is contained in:
2026-08-14 06:18:33 +02:00
parent 0b457ea713
commit 2ee720a9af
11 changed files with 392 additions and 51 deletions

View File

@@ -10,6 +10,7 @@ import (
"go.uber.org/fx"
"sneak.berlin/go/webhooker/internal/config"
"sneak.berlin/go/webhooker/internal/database"
"sneak.berlin/go/webhooker/internal/lifecycle"
"sneak.berlin/go/webhooker/internal/logger"
)
@@ -67,10 +68,9 @@ func NewArchiveSweeper(
}
// registerHooks wires the sweeper's start and stop into the fx
// lifecycle. Both hook contexts are deliberately ignored: see
// start for why the background loop must not inherit the start
// hook's context, and stop for why shutdown blocks on the loop
// rather than on the stop hook's deadline.
// lifecycle. The start hook's context is deliberately ignored
// (see start for why the background loop must not inherit it);
// the stop hook's context is honoured (see stop).
func (s *ArchiveSweeper) registerHooks(lc fx.Lifecycle) {
lc.Append(fx.Hook{
//nolint:contextcheck // Not passing the hook context is
@@ -80,10 +80,8 @@ func (s *ArchiveSweeper) registerHooks(lc fx.Lifecycle) {
return nil
},
OnStop: func(_ context.Context) error {
s.stop()
return nil
OnStop: func(ctx context.Context) error {
return s.stop(ctx)
},
})
}
@@ -113,15 +111,27 @@ func (s *ArchiveSweeper) start() {
)
}
func (s *ArchiveSweeper) stop() {
// stop cancels the sweep loop's context and waits for it to
// exit, bounded by the stop hook's context: a prune wedged on a
// locked archive must not hang the process past fx's stop
// timeout.
func (s *ArchiveSweeper) stop(ctx context.Context) error {
s.log.Info("archive sweeper stopping")
if s.cancel != nil {
s.cancel()
}
s.wg.Wait()
err := lifecycle.WaitForShutdown(
ctx, s.log, "archive sweeper", &s.wg,
)
if err != nil {
return err
}
s.log.Info("archive sweeper stopped")
return nil
}
func (s *ArchiveSweeper) run(ctx context.Context) {