Handle interrupt
This commit is contained in:
parent
f01ebc27cf
commit
231d1c6041
15
core/app.go
15
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()...)
|
||||
|
@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user