restart server when crashed

Related #65
This commit is contained in:
Gokcehan 2018-06-05 22:11:20 +03:00
parent 397d5e0fac
commit 448f2a3cb2

21
main.go
View File

@ -4,6 +4,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"log" "log"
"net"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -42,6 +43,21 @@ func startServer() {
} }
} }
func checkServer() {
if gSocketProt == "unix" {
if _, err := os.Stat(gSocketPath); os.IsNotExist(err) {
startServer()
} else if _, err := net.Dial(gSocketProt, gSocketPath); err != nil {
os.Remove(gSocketPath)
startServer()
}
} else {
if _, err := net.Dial(gSocketProt, gSocketPath); err != nil {
startServer()
}
}
}
func main() { func main() {
showDoc := flag.Bool( showDoc := flag.Bool(
"doc", "doc",
@ -125,10 +141,7 @@ func main() {
gServerLogPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.server.log", gUser.Username)) gServerLogPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.server.log", gUser.Username))
serve() serve()
default: default:
// TODO: check if the socket is working checkServer()
if _, err := os.Stat(gSocketPath); os.IsNotExist(err) {
startServer()
}
gClientID = 1000 gClientID = 1000
gLogPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.%d.log", gUser.Username, gClientID)) gLogPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.%d.log", gUser.Username, gClientID))