I'm experimenting with falcore (go(lang) framework), and they have a nice example that allows you to send a SIGHUP to the process, after which it restarts a client, moves connections over and exits the parent.
So, in my example, I have a single server.go (posted at the end) where I serve one file by default.
I run the server, then edit the .go file, kill -1 the pid of the process, and the app restarts as expected, but the newly added code to the .go file is not loaded.
For example, I change the default file served from summary.xml to AppNexus-Interesting.txt, but it will keep serving the summary.xml file for all new requests.
Any help is appreciated.
package main
import (
"flag"
"fmt"
"github.com/fitstar/falcore"
"github.com/fitstar/falcore/filter"
"net/http"
"os"
"os/signal"
"syscall"
)
// Command line options
var (
port = flag.Int("port", 8000, "the port to listen on")
path = flag.String("base", "./www", "the path to serve files from")
)
// very simple request filter
func Filter(req *falcore.Request) *http.Response {
pid := syscall.Getpid()
fmt.Println(pid, "GET", req.HttpRequest.URL.Path)
// return falcore.StringResponse(request.HttpRequest, 200, nil, "OK
")
if req.HttpRequest.URL.Path == "/" {
req.HttpRequest.URL.Path = "AppNexus-Interesting.txt" //"/summary.xml"
}
return nil
}
// flag to accept a socket file descriptor
var socketFd = flag.Int("socket", -1, "Socket file descriptor")
func main() {
pid := syscall.Getpid()
flag.Parse()
fmt.Println("Falcore hot restart running with pid:", pid, "to hot restart, issue the kill -1", pid, "command")
// create the pipeline
pipeline := falcore.NewPipeline()
// upstream filters
pipeline.Upstream.PushBack(falcore.NewRequestFilter(Filter))
// Serve files
pipeline.Upstream.PushBack(&filter.FileFilter{
BasePath: *path,
})
// downstream filters
pipeline.Downstream.PushBack(filter.NewCompressionFilter(nil))
// create the server with the pipeline
srv := falcore.NewServer(*port, pipeline)
// if passed the socket file descriptor, setup the listener that way
// if you don't have it, the default is to create the socket listener
// with the data passed to falcore.NewServer above (happens in ListenAndServer())
if *socketFd != -1 {
// I know I'm a child process if I get here so I can signal the parent when I'm ready to take over
go childReady(srv)
fmt.Printf("%v Got socket FD: %v
", pid, *socketFd)
srv.FdListen(*socketFd)
}
// using signals to manage the restart lifecycle
go handleSignals(srv)
// start the server
// this is normally blocking forever unless you send lifecycle commands
if err := srv.ListenAndServe(); err != nil {
fmt.Printf("%v Could not start server: %v", pid, err)
}
fmt.Printf("%v Exiting now
", pid)
}
// blocks on the server ready and when ready, it sends
// a signal to the parent so that it knows it cna now exit
func childReady(srv *falcore.Server) {
pid := syscall.Getpid()
// wait for the ready signal
<-srv.AcceptReady
// grab the parent and send a signal that the child is ready
parent := syscall.Getppid()
fmt.Printf("%v Kill parent %v with SIGUSR1
", pid, parent)
syscall.Kill(parent, syscall.SIGUSR1)
}
// setup and fork/exec myself. Make sure to keep open important FD's that won't get re-created by the child
// specifically, std* and your listen socket
func forker(srv *falcore.Server) (pid int, err error) {
fmt.Printf("Forking now with socket: %v
", srv.SocketFd())
mypath := os.Args[0]
args := []string{mypath, "-socket", fmt.Sprintf("%v", srv.SocketFd())}
attr := new(syscall.ProcAttr)
attr.Files = append([]uintptr(nil), 0, 1, 2, uintptr(srv.SocketFd()))
pid, err = syscall.ForkExec(mypath, args, attr)
return
}
// Handle lifecycle events
func handleSignals(srv *falcore.Server) {
var sig os.Signal
var sigChan = make(chan os.Signal)
signal.Notify(sigChan, syscall.SIGHUP, syscall.SIGUSR1, syscall.SIGINT, syscall.SIGTERM, syscall.SIGTSTP)
pid := syscall.Getpid()
for {
sig = <-sigChan
switch sig {
case syscall.SIGHUP:
// send this to the paraent process to initiate the restart
fmt.Println(pid, "Received SIGHUP. forking.")
cpid, err := forker(srv)
fmt.Println(pid, "Forked pid:", cpid, "errno:", err)
case syscall.SIGUSR1:
// child sends this back to the parent when it's ready to Accept
fmt.Println(pid, "Received SIGUSR1. Stopping accept.")
srv.StopAccepting()
case syscall.SIGINT:
fmt.Println(pid, "Received SIGINT. Shutting down.")
os.Exit(0)
case syscall.SIGTERM:
fmt.Println(pid, "Received SIGTERM. Terminating.")
os.Exit(0)
case syscall.SIGTSTP:
fmt.Println(pid, "Received SIGTSTP. Stopping.")
syscall.Kill(pid, syscall.SIGSTOP)
default:
fmt.Println(pid, "Received", sig, ": ignoring")
}
}
}