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 {
buildCtx, cancelCtx := context.WithCancel(context.Background())
buildCtx, cancelBuildCtx := context.WithCancel(context.Background())
// Handle interrupts for this build
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
defer func() {
cancelBuildCtx()
signal.Stop(sigCh)
close(sigCh)
}()
go func() {
sig := <-sigCh
c.Ui.Error(fmt.Sprintf("Cancelling build after receiving %s", sig))
cancelCtx()
select {
case sig := <-sigCh:
c.Ui.Error(fmt.Sprintf("Cancelling build after receiving %s", sig))
cancelBuildCtx()
case <-buildCtx.Done():
}
}()
return c.RunContext(buildCtx, args)