handle SIGHUP, restart camli from wizard

Change-Id: I9856e49b9c4d76dc3bed1827594451349d4f5810
This commit is contained in:
mpl 2012-05-15 10:44:03 +02:00
parent ec70a6e2c8
commit b2f079f081
5 changed files with 80 additions and 8 deletions

View File

@ -0,0 +1,17 @@
// +build linux darwin
// TODO(mpl): Copyright in next CL.
package osutil
import (
"os"
"syscall"
)
// restartProcess returns an error if things couldn't be
// restarted. On success, this function never returns
// because the process becomes the new process.
func RestartProcess() error {
return syscall.Exec(os.Args[0], os.Args, os.Environ())
}

View File

@ -0,0 +1,17 @@
// +build windows
// TODO(mpl): Copyright in next CL.
package osutil
import (
"log"
)
// restartProcess returns an error if things couldn't be
// restarted. On success, this function never returns
// because the process becomes the new process.
func RestartProcess() error {
log.Print("RestartProcess not implemented on windows")
return nil
}

View File

@ -30,6 +30,10 @@ const topWizard =`
const bottomWizard =`
<p> <a href="/">Back</a> </p>
<p>
<form id="Restart" name="Restart" action="restartCamli" method="post">
<input type="submit" form="Restart" value="RestartCamli"></form>
</p>
</body>
</html>
`

View File

@ -20,6 +20,7 @@ import (
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"os"
"reflect"
@ -167,11 +168,6 @@ func rewriteConfig(config *jsonconfig.Obj, configfile string) error {
}
func handleSetupChange(req *http.Request, rw http.ResponseWriter) {
err := req.ParseMultipartForm(10e6)
if err != nil {
httputil.ServerError(rw, err)
return
}
hilevelConf, err := jsonconfig.ReadFile(osutil.UserServerConfigPath())
if err != nil {
httputil.ServerError(rw, err)
@ -257,8 +253,21 @@ func (sh *SetupHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
return
}
if req.Method == "POST" {
handleSetupChange(req, rw)
return
err := req.ParseMultipartForm(10e6)
if err != nil {
httputil.ServerError(rw, err)
return
}
if len(req.Form) > 0 {
handleSetupChange(req, rw)
return
}
if strings.Contains(req.URL.Path, "restartCamli") {
err = osutil.RestartProcess()
if err != nil {
log.Fatal("Failed to restart: " + err.Error())
}
}
}
sendWizard(req, rw, false)

View File

@ -29,9 +29,11 @@ import (
"math/big"
"net"
"os"
"os/signal"
"path/filepath"
"runtime"
"strings"
"syscall"
"time"
"camlistore.org/pkg/jsonsign"
@ -266,6 +268,28 @@ func setupTLS(ws *webserver.Server, config *serverconfig.Config, listen string)
ws.SetTLS(cert, key)
}
func handleSignals() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)
for {
sig := <-c
sysSig, ok := sig.(syscall.Signal)
if !ok {
log.Fatal("Not a unix signal")
}
switch sysSig {
case syscall.SIGHUP:
log.Print("SIGHUP: restarting camli")
err := osutil.RestartProcess()
if err != nil {
log.Fatal("Failed to restart: " + err.Error())
}
default:
log.Fatal("Received another signal, should not happen.")
}
}
}
func main() {
flag.Parse()
@ -320,5 +344,6 @@ func main() {
}
}()
}
ws.Serve()
go ws.Serve()
handleSignals()
}