BuildCommand.Run: avoid triggering a cancellation on termination

This commit is contained in:
Adrien Delorme 2019-05-06 12:26:22 +02:00
parent 0094d2878c
commit dd2785ff08
1 changed files with 8 additions and 5 deletions

View File

@ -26,19 +26,22 @@ type BuildCommand struct {
} }
func (c *BuildCommand) Run(args []string) int { func (c *BuildCommand) Run(args []string) int {
buildCtx, cancelCtx := context.WithCancel(context.Background()) buildCtx, cancelBuildCtx := context.WithCancel(context.Background())
// Handle interrupts for this build // Handle interrupts for this build
sigCh := make(chan os.Signal, 1) sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM) signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
defer func() { defer func() {
cancelBuildCtx()
signal.Stop(sigCh) signal.Stop(sigCh)
close(sigCh) close(sigCh)
}() }()
go func() { go func() {
sig := <-sigCh select {
case sig := <-sigCh:
c.Ui.Error(fmt.Sprintf("Cancelling build after receiving %s", sig)) c.Ui.Error(fmt.Sprintf("Cancelling build after receiving %s", sig))
cancelCtx() cancelBuildCtx()
case <-buildCtx.Done():
}
}() }()
return c.RunContext(buildCtx, args) return c.RunContext(buildCtx, args)