falcore hot restart不会重新加载主代码

时间:2014-02-12 14:30:39

标签: go continuous-integration

我正在试验falcore(go(lang)框架),他们有一个很好的例子,它允许你向进程发送一个SIGHUP,之后它重新启动一个客户端,移动连接并退出父进程。 / p>

所以,在我的例子中,我有一个server.go(最后发布),我默认提供一个文件。

我运行服务器,然后编辑.go文件,kill -1进程的pid,应用程序按预期重新启动,但是没有加载.go文件中新添加的代码。

例如,我将从summary.xml提供的默认文件更改为AppNexus-Interesting.txt,但它将继续为所有新请求提供summary.xml文件。

感谢任何帮助。

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\n")
    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\n", 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\n", 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\n", 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\n", 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")
        }
    }
}

1 个答案:

答案 0 :(得分:1)

由于go不是脚本语言,因此必须首先将源代码编译为二进制文件(使用go buildgo install),然后执行重新启动。