From 2dd5a982e52f4676de40dd38ffef3dce43cba91e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 4 Jun 2013 08:40:17 -0700 Subject: [PATCH] command/build: Cleanly exit after being interrupted --- command/build/command.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/command/build/command.go b/command/build/command.go index 1b95fbba0..6be166898 100644 --- a/command/build/command.go +++ b/command/build/command.go @@ -115,11 +115,17 @@ func (c Command) Run(env packer.Environment, args []string) int { } // Handle signals + var interruptWg sync.WaitGroup + interrupted := false sigCh := make(chan os.Signal, 1) signal.Notify(sigCh, os.Interrupt) go func() { <-sigCh + interruptWg.Add(1) + defer interruptWg.Done() + interrupted = true + log.Println("Interrupted! Cancelling builds...") var wg sync.WaitGroup @@ -137,7 +143,15 @@ func (c Command) Run(env packer.Environment, args []string) int { wg.Wait() }() + // Wait for both the builds to complete and the interrupt handler, + // if it is interrupted. wg.Wait() + interruptWg.Wait() + + if interrupted { + env.Ui().Say("Cleanly cancelled builds after being interrupted.") + return 1 + } // Output all the artifacts env.Ui().Say("\n==> The build completed! The artifacts created were:")