Skip to content

Commit

Permalink
Gracefully clean up on SIGTERM
Browse files Browse the repository at this point in the history
  • Loading branch information
mwhooker committed Sep 8, 2017
1 parent 6f3def8 commit fdaf4ed
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 6 deletions.
3 changes: 2 additions & 1 deletion command/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"strings"
"sync"
"syscall"

"github.com/hashicorp/packer/helper/enumflag"
"github.com/hashicorp/packer/packer"
Expand Down Expand Up @@ -144,7 +145,7 @@ func (c BuildCommand) Run(args []string) int {

// Handle interrupts for this build
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(sigCh)
go func(b packer.Build) {
<-sigCh
Expand Down
3 changes: 2 additions & 1 deletion command/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"regexp"
"strings"
"syscall"

"github.com/hashicorp/atlas-go/archive"
"github.com/hashicorp/atlas-go/v1"
Expand Down Expand Up @@ -267,7 +268,7 @@ func (c *PushCommand) Run(args []string) int {

// Make a ctrl-C channel
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(sigCh)

err = nil
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"path/filepath"
"runtime"
"sync"
"syscall"
"time"

"github.com/hashicorp/go-uuid"
Expand Down Expand Up @@ -86,6 +87,7 @@ func realMain() int {
wrapConfig.Writer = io.MultiWriter(logTempFile, logWriter)
wrapConfig.Stdout = outW
wrapConfig.DetectDuration = 500 * time.Millisecond
wrapConfig.ForwardSignals = []os.Signal{syscall.SIGTERM}
exitStatus, err := panicwrap.Wrap(&wrapConfig)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't start Packer: %s", err)
Expand Down
6 changes: 4 additions & 2 deletions packer/plugin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package plugin
import (
"errors"
"fmt"
packrpc "github.com/hashicorp/packer/packer/rpc"
"io/ioutil"
"log"
"math/rand"
Expand All @@ -20,7 +19,10 @@ import (
"runtime"
"strconv"
"sync/atomic"
"syscall"
"time"

packrpc "github.com/hashicorp/packer/packer/rpc"
)

// This is a count of the number of interrupts the process has received.
Expand Down Expand Up @@ -87,7 +89,7 @@ func Server() (*packrpc.Server, error) {

// Eat the interrupts
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
go func() {
var count int32 = 0
for {
Expand Down
2 changes: 1 addition & 1 deletion packer/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (rw *BasicUi) Ask(query string) (string, error) {
rw.scanner = bufio.NewScanner(rw.Reader)
}
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(sigCh)

log.Printf("ui: ask: %s", query)
Expand Down
3 changes: 2 additions & 1 deletion stdin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"
"os/signal"
"syscall"
)

// setupStdin switches out stdin for a pipe. We do this so that we can
Expand All @@ -26,7 +27,7 @@ func setupStdin() {
// Register a signal handler for interrupt in order to close the
// writer end of our pipe so that readers get EOF downstream.
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)

go func() {
defer signal.Stop(ch)
Expand Down

0 comments on commit fdaf4ed

Please sign in to comment.