auto restart server

This commit is contained in:
Ian Gulliver
2026-10-18 23:07:11 -07:00
parent 374ccf8b5e
commit c64987f2eb
7 changed files with 40 additions and 44 deletions
+33
View File
@@ -39,6 +39,9 @@ func serveCmd(args []string) {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
ctx, replaced := context.WithCancel(ctx)
defer replaced()
go watchExecutable(ctx, replaced)
hs := &http.Server{
Addr: *listenAddr,
@@ -59,3 +62,33 @@ func serveCmd(args []string) {
fatal("serve", "err", err)
}
}
func watchExecutable(ctx context.Context, replaced context.CancelFunc) {
exe, err := os.Executable()
if err != nil {
fatal("executable path", "err", err)
}
started, err := os.Stat(exe)
if err != nil {
fatal("stat executable", "path", exe, "err", err)
}
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
}
now, err := os.Stat(exe)
if err != nil {
continue
}
if os.SameFile(started, now) {
continue
}
slog.Info("executable replaced, exiting", "path", exe)
replaced()
return
}
}