Fix #986: Ensure ctrl-c always kills ipfs-cluster-follow

This commit is contained in:
Hector Sanjuan 2019-12-19 18:34:18 +01:00
parent d12bc7daf7
commit bf2c950016
3 changed files with 46 additions and 32 deletions

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"os/signal"
"path/filepath"
"strings"
"time"
@ -351,6 +352,12 @@ func runCmd(c *cli.Context) error {
metricsCfg.EnableStats = false
cfgs.Metrics = &metricsCfg
// We are going to run a cluster peer and should do an
// oderly shutdown if we are interrupted: cancel default
// signal handling and leave things to HandleSignals.
signal.Stop(signalChan)
close(signalChan)
cluster, err := ipfscluster.NewCluster(
ctx,
host,

View File

@ -4,8 +4,10 @@ package main
import (
"fmt"
"os"
"os/signal"
"os/user"
"path/filepath"
"syscall"
"github.com/ipfs/ipfs-cluster/api/rest/client"
"github.com/ipfs/ipfs-cluster/cmdutils"
@ -25,8 +27,26 @@ const (
logLevel = "info"
)
// We store a commit id here
var commit string
// Default location for the configurations and data
var (
// DefaultFolder is the name of the cluster folder
DefaultFolder = ".ipfs-cluster-follow"
// DefaultPath is set on init() to $HOME/DefaultFolder
// and holds all the ipfs-cluster data
DefaultPath string
// The name of the configuration file inside DefaultPath
DefaultConfigFile = "service.json"
// The name of the identity file inside DefaultPath
DefaultIdentityFile = "identity.json"
)
var (
commit string
logger = logging.Logger("clusterfollow")
configPath string
identityPath string
signalChan = make(chan os.Signal, 20)
)
// Description provides a short summary of the functionality of this tool
var Description = fmt.Sprintf(`
@ -95,26 +115,6 @@ $ %s <clusterName> list --help
programName,
)
var logger = logging.Logger("clusterfollow")
// Default location for the configurations and data
var (
// DefaultFolder is the name of the cluster folder
DefaultFolder = ".ipfs-cluster-follow"
// DefaultPath is set on init() to $HOME/DefaultFolder
// and holds all the ipfs-cluster data
DefaultPath string
// The name of the configuration file inside DefaultPath
DefaultConfigFile = "service.json"
// The name of the identity file inside DefaultPath
DefaultIdentityFile = "identity.json"
)
var (
configPath string
identityPath string
)
func init() {
// Set build information.
if build, err := semver.NewBuildVersion(commit); err == nil {
@ -135,6 +135,23 @@ func init() {
}
DefaultPath = filepath.Join(home, DefaultFolder)
// This will abort the program on signal. We close the signal channel
// when launching the peer so that we can do an orderly shutdown in
// that case though.
go func() {
signal.Notify(
signalChan,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGHUP,
)
_, ok := <-signalChan // channel closed.
if !ok {
return
}
os.Exit(1)
}()
}
func main() {

View File

@ -150,19 +150,9 @@ func WaitForIPFS(ctx context.Context) error {
return errors.Wrap(err, "error creating an ipfshttp instance to wait for IPFS")
}
signalChan := make(chan os.Signal, 20)
signal.Notify(
signalChan,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGHUP,
)
i := 0
for {
select {
case <-signalChan:
return errors.New("interrupted")
case <-ctx.Done():
return ctx.Err()
default: