packer/plugin: catch interrupts for every server

This commit is contained in:
Mitchell Hashimoto 2013-12-10 14:12:00 -08:00
parent 06d12773eb
commit 4c5d61709d
1 changed files with 12 additions and 21 deletions

View File

@ -92,28 +92,19 @@ func Server() (*packrpc.Server, error) {
return nil, err
}
// Eat the interrupts
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
go func() {
var count int32 = 0
for {
<-ch
newCount := atomic.AddInt32(&count, 1)
log.Printf("Received interrupt signal (count: %d). Ignoring.", newCount)
}
}()
// Serve a single connection
log.Println("Serving a plugin connection...")
return packrpc.NewServer(conn), nil
}
// Registers a signal handler to swallow and count interrupts so that the
// plugin isn't killed. The main host Packer process is responsible
// for killing the plugins when interrupted.
func countInterrupts() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
go func() {
for {
<-ch
newCount := atomic.AddInt32(&Interrupts, 1)
log.Printf("Received interrupt signal (count: %d). Ignoring.", newCount)
}
}()
}
// Tests whether or not the plugin was interrupted or not.
func Interrupted() bool {
return atomic.LoadInt32(&Interrupts) > 0
}