mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2025-10-15 20:06:19 +08:00
better handling of os signals
This commit is contained in:
@@ -11,7 +11,6 @@ import (
|
|||||||
"code.google.com/p/weed-fs/go/util"
|
"code.google.com/p/weed-fs/go/util"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -28,16 +27,10 @@ func runMount(cmd *Command, args []string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
signalChan := make(chan os.Signal, 1)
|
OnInterrupt(func() {
|
||||||
signal.Notify(signalChan, os.Interrupt, os.Kill)
|
fuse.Unmount(*mountOptions.dir)
|
||||||
go func() {
|
c.Close()
|
||||||
for _ = range signalChan {
|
})
|
||||||
// sig is a ^C, handle it
|
|
||||||
fuse.Unmount(*mountOptions.dir)
|
|
||||||
c.Close()
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
err = fs.Serve(c, WFS{})
|
err = fs.Serve(c, WFS{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@@ -7,7 +7,6 @@ import (
|
|||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"runtime/pprof"
|
"runtime/pprof"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -218,16 +217,10 @@ func runServer(cmd *Command, args []string) bool {
|
|||||||
glog.Fatalf(e.Error())
|
glog.Fatalf(e.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// deal with control+c
|
OnInterrupt(func() {
|
||||||
signalChan := make(chan os.Signal, 1)
|
volumeServer.Shutdown()
|
||||||
signal.Notify(signalChan, os.Interrupt, os.Kill)
|
pprof.StopCPUProfile()
|
||||||
go func() {
|
})
|
||||||
for _ = range signalChan {
|
|
||||||
volumeServer.Shutdown()
|
|
||||||
pprof.StopCPUProfile()
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
if e := http.Serve(volumeListener, r); e != nil {
|
if e := http.Serve(volumeListener, r); e != nil {
|
||||||
glog.Fatalf("Fail to serve:%s", e.Error())
|
glog.Fatalf("Fail to serve:%s", e.Error())
|
||||||
|
27
go/weed/signal_handling.go
Normal file
27
go/weed/signal_handling.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
// +build !plan9
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
func OnInterrupt(fn func()) {
|
||||||
|
// deal with control+c,etc
|
||||||
|
signalChan := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(signalChan,
|
||||||
|
os.Interrupt,
|
||||||
|
os.Kill,
|
||||||
|
syscall.SIGHUP,
|
||||||
|
syscall.SIGINT,
|
||||||
|
syscall.SIGTERM,
|
||||||
|
syscall.SIGQUIT)
|
||||||
|
go func() {
|
||||||
|
for _ = range signalChan {
|
||||||
|
fn()
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
8
go/weed/signal_handling_notsupported.go
Normal file
8
go/weed/signal_handling_notsupported.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
// +build plan9
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import ()
|
||||||
|
|
||||||
|
func OnInterrupt(fn func()) {
|
||||||
|
}
|
@@ -6,7 +6,6 @@ import (
|
|||||||
"code.google.com/p/weed-fs/go/weed/weed_server"
|
"code.google.com/p/weed-fs/go/weed/weed_server"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -92,15 +91,9 @@ func runVolume(cmd *Command, args []string) bool {
|
|||||||
glog.Fatalf(e.Error())
|
glog.Fatalf(e.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// deal with control+c
|
OnInterrupt(func() {
|
||||||
signalChan := make(chan os.Signal, 1)
|
volumeServer.Shutdown()
|
||||||
signal.Notify(signalChan, os.Interrupt, os.Kill)
|
})
|
||||||
go func() {
|
|
||||||
for _ = range signalChan {
|
|
||||||
volumeServer.Shutdown()
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
if e := http.Serve(listener, r); e != nil {
|
if e := http.Serve(listener, r); e != nil {
|
||||||
glog.Fatalf("Fail to serve:%s", e.Error())
|
glog.Fatalf("Fail to serve:%s", e.Error())
|
||||||
|
Reference in New Issue
Block a user