f-78: agent: attendre la fin du watchdog avant de fermer la base à l'arrêt #78

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-19 23:46:22 +02:00
commit 54e0faac55
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
2 changed files with 64 additions and 9 deletions

View file

@ -111,11 +111,18 @@ func run(confFile string) int {
go adminSrv.Start()
}
watchdogStopped := make(chan struct{})
if cfg.Watchdog.Enabled {
wlog := log.With(slog.String("component", "watchdog"))
go watchdog.New(db, cfg, notify.NewStderr(wlog), wlog,
w := watchdog.New(db, cfg, notify.NewStderr(wlog), wlog,
time.Duration(cfg.Watchdog.IntervalSeconds)*time.Second,
).Run(ctx)
)
go func() {
defer close(watchdogStopped)
w.Run(ctx)
}()
} else {
close(watchdogStopped)
}
<-ctx.Done()
@ -125,7 +132,7 @@ func run(confFile string) int {
if adminSrv != nil {
servers["admin"] = adminSrv
}
closeDB = shutdown(log, q, servers, shutdownTimeout)
closeDB = shutdown(log, q, servers, watchdogStopped, shutdownTimeout)
return 0
}
@ -149,7 +156,7 @@ type httpShutdowner interface {
Shutdown(context.Context) error
}
func shutdown(log *slog.Logger, q *worker.Queue, servers map[string]httpShutdowner, timeout time.Duration) bool {
func shutdown(log *slog.Logger, q *worker.Queue, servers map[string]httpShutdowner, watchdogStopped <-chan struct{}, timeout time.Duration) bool {
log.Info("shutting down", "timeout", timeout)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
@ -170,9 +177,16 @@ func shutdown(log *slog.Logger, q *worker.Queue, servers map[string]httpShutdown
select {
case <-drained:
log.Info("workers drained")
return true
case <-ctx.Done():
log.Error("workers still running after timeout, leaving database untouched")
return false
}
select {
case <-watchdogStopped:
return true
case <-ctx.Done():
log.Error("watchdog still running after timeout, leaving database untouched")
return false
}
}