command/build: Cleanly exit after being interrupted
This commit is contained in:
parent
256b97ce49
commit
2dd5a982e5
@ -115,11 +115,17 @@ func (c Command) Run(env packer.Environment, args []string) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle signals
|
// Handle signals
|
||||||
|
var interruptWg sync.WaitGroup
|
||||||
|
interrupted := false
|
||||||
sigCh := make(chan os.Signal, 1)
|
sigCh := make(chan os.Signal, 1)
|
||||||
signal.Notify(sigCh, os.Interrupt)
|
signal.Notify(sigCh, os.Interrupt)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
<-sigCh
|
<-sigCh
|
||||||
|
interruptWg.Add(1)
|
||||||
|
defer interruptWg.Done()
|
||||||
|
interrupted = true
|
||||||
|
|
||||||
log.Println("Interrupted! Cancelling builds...")
|
log.Println("Interrupted! Cancelling builds...")
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
@ -137,7 +143,15 @@ func (c Command) Run(env packer.Environment, args []string) int {
|
|||||||
wg.Wait()
|
wg.Wait()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// Wait for both the builds to complete and the interrupt handler,
|
||||||
|
// if it is interrupted.
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
interruptWg.Wait()
|
||||||
|
|
||||||
|
if interrupted {
|
||||||
|
env.Ui().Say("Cleanly cancelled builds after being interrupted.")
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
// Output all the artifacts
|
// Output all the artifacts
|
||||||
env.Ui().Say("\n==> The build completed! The artifacts created were:")
|
env.Ui().Say("\n==> The build completed! The artifacts created were:")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user