small pixel drawing of a pufferfish gore

on ^C, shut services down
Jes Olson j3s@c3f.net
Fri, 27 Dec 2024 00:48:40 -0500
commit

183d99d05cc993a52c42dba4c6cff94482987562

parent

9fc06ef48db3a0b8f045fd636d49eabf8c8c992d

2 files changed, 26 insertions(+), 12 deletions(-)

jump to
M main.gomain.go

@@ -1,22 +1,20 @@

package main import ( + "context" "log" "net/http" + "os" "os/exec" + "os/signal" "path/filepath" + "syscall" "time" "j3s.sh/gore/internal/assets" ) func main() { - http.HandleFunc("GET /", indexHandler) - http.HandleFunc("GET /status", statusHandler) - http.HandleFunc("GET /log", logHandler) - http.Handle("GET /assets/", http.StripPrefix("/assets/", http.FileServer(http.FS(assets.Assets)))) - http.HandleFunc("POST /add", addHandler) - var services []*service s1 := newService("/home/j3s/code/gore/builddir/git.j3s.sh/vore@v0.0.0-20240814184024-9d6fb2a0444f/vore") services = append(services, s1)

@@ -24,10 +22,27 @@

if err := SuperviseServices(services); err != nil { log.Fatal(err) } - log.Println("Starting Gore server on :6043...") - // arbitrarily select 90 seconds for service shutdown - // TODO: make this configurable? - defer killSupervisedServices(90 * time.Second) + + // this is for ensuring services terminate when ^C kills gore + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) + defer stop() + go func() { + <-ctx.Done() + log.Println("Received shutdown signal…") + // arbitrarily select 90 seconds for service shutdown + // TODO: make this configurable? + killSupervisedServices(90 * time.Second) + os.Exit(0) + }() + + // routes + http.HandleFunc("GET /", indexHandler) + http.HandleFunc("GET /status", statusHandler) + http.HandleFunc("GET /log", logHandler) + http.Handle("GET /assets/", http.StripPrefix("/assets/", http.FileServer(http.FS(assets.Assets)))) + http.HandleFunc("POST /add", addHandler) + + log.Println("listening on http://localhost:6043...") log.Fatal(http.ListenAndServe(":6043", nil)) }
M supervise.gosupervise.go

@@ -432,7 +432,7 @@ if cmd.Env == nil {

cmd.Env = os.Environ() // for older gokr-packer versions } if attempt == 0 { - cmd.Env = append(cmd.Env, "GOKRAZY_FIRST_START=1") + cmd.Env = append(cmd.Env, "GORE_FIRST_START=1") } // Designate a subdirectory under /perm/home as $HOME. // This mirrors what gore system daemons and

@@ -673,7 +673,6 @@ services.Lock()

services.S = svc defer services.Unlock() for _, s := range services.S { - fmt.Printf("%+v", s) go supervise(s) }