diff --git a/core/app.go b/core/app.go index c33dc11..93f2d24 100644 --- a/core/app.go +++ b/core/app.go @@ -2,6 +2,10 @@ package core import ( "fmt" + "log" + "os" + "os/signal" + "syscall" "github.com/localhots/yeast/chain" "github.com/localhots/yeast/unit" @@ -43,6 +47,17 @@ func (a *App) Call(chainName string, data []byte) (resp []byte, err error) { } } +func (a *App) HandleInterrupt() { + shutdown := make(chan os.Signal) + signal.Notify(shutdown, os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGINT) + go func() { + <-shutdown + log.Print("Interrupt recieved. Shutting down...") + a.sv.Shutdown() + os.Exit(0) + }() +} + func (a *App) BootChain(name string) { if c, ok := a.chains.Chain(name); ok { a.sv.Start(c.Units()...) diff --git a/core/supervisor.go b/core/supervisor.go index 55a3d79..94ab344 100644 --- a/core/supervisor.go +++ b/core/supervisor.go @@ -38,3 +38,12 @@ func (s *Supervisor) Start(units ...string) { time.Sleep(200 * time.Millisecond) // Don't spawn processes too fast } } + +func (s *Supervisor) Shutdown() { + for name, cmd := range s.procs { + log.Printf("Stopping unit: %s", name) + if err := cmd.Process.Kill(); err != nil { + log.Printf("Failed to kill unit: %s (%s)", name, err.Error()) + } + } +}