1
0
Fork 0

Handle interrupt

This commit is contained in:
Gregory Eremin 2015-02-12 03:05:07 +07:00
parent f01ebc27cf
commit 231d1c6041
2 changed files with 24 additions and 0 deletions

View File

@ -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()...)

View File

@ -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())
}
}
}