cmd/ipfs2: Handle SIGTERM
Matt Bell committed
Nov 9, 2014 at 00:57 UTC
b3ea49954a173177c84155b66a887d223c4f07ac
1 file changed
+16
cmd/ipfs2/main.go
+16
@@ -4,6 +4,7 @@ import (
4
"fmt"
5
"io"
6
"os"
7
+ "os/signal"
8
"runtime/pprof"
9
10
logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-logging"
@@ -31,6 +32,8 @@ const (
32
var ofi io.WriteCloser
33
34
func main() {
35
+ handleInterrupt()
36
+
37
args := os.Args[1:]
38
req, root := createRequest(args)
39
handleOptions(req, root)
@@ -299,6 +302,19 @@ func writeHeapProfileToFile() error {
302
return pprof.WriteHeapProfile(mprof)
303
}
304
305
+// listen for and handle SIGTERM
306
+func handleInterrupt() {
307
+ c := make(chan os.Signal, 1)
308
+ signal.Notify(c, os.Interrupt)
309
+
310
+ go func() {
311
+ for _ = range c {
312
+ log.Info("Received interrupt signal, terminating...")
313
+ exit(0)
314
+ }
315
+ }()
316
+}
317
+
318
func exit(code int) {
319
if u.Debug {
320
pprof.StopCPUProfile()