Browse Source

:gear: Handle termination signals gracefully

Ettore Di Giacinto 3 years ago
parent
commit
9307c34028
8 changed files with 23 additions and 1 deletions
  1. 2 0
      cmd/api.go
  2. 1 0
      cmd/dns.go
  3. 2 0
      cmd/file.go
  4. 1 0
      cmd/join.go
  5. 1 1
      cmd/main.go
  6. 2 0
      cmd/proxy.go
  7. 2 0
      cmd/service.go
  8. 12 0
      cmd/util.go

+ 2 - 0
cmd/api.go

@@ -57,6 +57,8 @@ A simple UI interface is available to display network data.`,
 			displayStart(ll)
 
 			ctx := context.Background()
+			go handleStopSignals()
+
 			// Start the node to the network, using our ledger
 			if err := e.Start(ctx); err != nil {
 				return err

+ 1 - 0
cmd/dns.go

@@ -73,6 +73,7 @@ func DNS() cli.Command {
 			}
 
 			displayStart(ll)
+			go handleStopSignals()
 
 			ctx := context.Background()
 			// Start the node to the network, using our ledger

+ 2 - 0
cmd/file.go

@@ -91,6 +91,7 @@ This is also the ID used to refer when receiving it.`,
 			}
 
 			displayStart(ll)
+			go handleStopSignals()
 
 			// Start the node to the network, using our ledger
 			if err := e.Start(context.Background()); err != nil {
@@ -139,6 +140,7 @@ func FileReceive() cli.Command {
 			}
 
 			displayStart(ll)
+			go handleStopSignals()
 
 			// Start the node to the network, using our ledger
 			if err := e.Start(context.Background()); err != nil {

+ 1 - 0
cmd/join.go

@@ -38,6 +38,7 @@ Useful for setting up relays or hop nodes to improve the network connectivity.`,
 			}
 
 			displayStart(ll)
+			go handleStopSignals()
 
 			// Start the node to the network, using our ledger
 			if err := e.Start(context.Background()); err != nil {

+ 1 - 1
cmd/main.go

@@ -219,7 +219,7 @@ func Main() func(c *cli.Context) error {
 		if c.Bool("api") {
 			go api.API(ctx, c.String("api-listen"), 5*time.Second, 20*time.Second, e, bwc, c.Bool("debug"))
 		}
-
+		go handleStopSignals()
 		return e.Start(ctx)
 	}
 }

+ 2 - 0
cmd/proxy.go

@@ -74,6 +74,8 @@ func Proxy() cli.Command {
 
 			displayStart(ll)
 
+			go handleStopSignals()
+
 			ctx := context.Background()
 			// Start the node to the network, using our ledger
 			if err := e.Start(ctx); err != nil {

+ 2 - 0
cmd/service.go

@@ -86,6 +86,7 @@ For example, '192.168.1.1:80', or '127.0.0.1:22'.`,
 			}
 
 			displayStart(ll)
+			go handleStopSignals()
 
 			// Join the node to the network, using our ledger
 			if err := e.Start(context.Background()); err != nil {
@@ -148,6 +149,7 @@ to the service over the network`,
 				return err
 			}
 			displayStart(ll)
+			go handleStopSignals()
 
 			// starts the node
 			return e.Start(context.Background())

+ 12 - 0
cmd/util.go

@@ -17,7 +17,10 @@ package cmd
 
 import (
 	"encoding/json"
+	"os"
+	"os/signal"
 	"runtime"
+	"syscall"
 	"time"
 
 	"github.com/ipfs/go-log"
@@ -480,3 +483,12 @@ func cliToOpts(c *cli.Context) ([]node.Option, []vpn.Option, *logger.Logger) {
 
 	return nodeOpts, vpnOpts, llger
 }
+
+func handleStopSignals() {
+	s := make(chan os.Signal, 10)
+	signal.Notify(s, os.Interrupt, syscall.SIGTERM)
+
+	for range s {
+		os.Exit(0)
+	}
+}