douzhuan1169 2014-02-12 14:30
浏览 32
已采纳

falcore热重启不会重新加载主代码

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")
        }
    }
}
  • 写回答

1条回答 默认 最新

  • dongyihang3575 2014-02-13 17:28
    关注

    Since go is not a scripting language, you must compile sources into binary first (using go build or go install) and then perform restart.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据