Falcore热重启不会重新加载主要代码。

huangapple go评论134阅读模式
英文:

falcore hot restart does not reload main code

问题

我正在尝试使用falcore(Go语言框架)进行实验,他们有一个很好的示例,允许您向进程发送SIGHUP信号,之后它会重新启动客户端,转移连接并退出父进程。

所以,在我的示例中,我有一个单独的server.go文件(在末尾发布),默认情况下我提供一个文件。

我运行服务器,然后编辑.go文件,使用kill -1命令杀死进程的pid,应用程序按预期重新启动,但是新添加的代码不会被加载。

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

感谢任何帮助。

  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "github.com/fitstar/falcore"
  6. "github.com/fitstar/falcore/filter"
  7. "net/http"
  8. "os"
  9. "os/signal"
  10. "syscall"
  11. )
  12. // 命令行选项
  13. var (
  14. port = flag.Int("port", 8000, "要监听的端口")
  15. path = flag.String("base", "./www", "要提供文件的路径")
  16. )
  17. // 非常简单的请求过滤器
  18. func Filter(req *falcore.Request) *http.Response {
  19. pid := syscall.Getpid()
  20. fmt.Println(pid, "GET", req.HttpRequest.URL.Path)
  21. // return falcore.StringResponse(request.HttpRequest, 200, nil, "OK\n")
  22. if req.HttpRequest.URL.Path == "/" {
  23. req.HttpRequest.URL.Path = "AppNexus-Interesting.txt" //"/summary.xml"
  24. }
  25. return nil
  26. }
  27. // 接受套接字文件描述符的标志
  28. var socketFd = flag.Int("socket", -1, "套接字文件描述符")
  29. func main() {
  30. pid := syscall.Getpid()
  31. flag.Parse()
  32. fmt.Println("Falcore热重启正在运行,pid为:", pid, "要进行热重启,请发出kill -1", pid, "命令")
  33. // 创建流水线
  34. pipeline := falcore.NewPipeline()
  35. // 上游过滤器
  36. pipeline.Upstream.PushBack(falcore.NewRequestFilter(Filter))
  37. // 提供文件
  38. pipeline.Upstream.PushBack(&filter.FileFilter{
  39. BasePath: *path,
  40. })
  41. // 下游过滤器
  42. pipeline.Downstream.PushBack(filter.NewCompressionFilter(nil))
  43. // 使用流水线创建服务器
  44. srv := falcore.NewServer(*port, pipeline)
  45. // 如果传递了套接字文件描述符,则使用该方式设置监听器
  46. // 如果没有传递,则默认使用 falcore.NewServer 上面传递的数据创建套接字监听器(在 ListenAndServer() 中发生)
  47. if *socketFd != -1 {
  48. // 我知道如果到达这里,我是一个子进程,所以我可以在准备接管时向父进程发送信号
  49. go childReady(srv)
  50. fmt.Printf("%v 获取到套接字FD:%v\n", pid, *socketFd)
  51. srv.FdListen(*socketFd)
  52. }
  53. // 使用信号管理重启生命周期
  54. go handleSignals(srv)
  55. // 启动服务器
  56. // 除非发送生命周期命令,否则通常会一直阻塞
  57. if err := srv.ListenAndServe(); err != nil {
  58. fmt.Printf("%v 无法启动服务器:%v", pid, err)
  59. }
  60. fmt.Printf("%v 现在退出\n", pid)
  61. }
  62. // 阻塞在服务器准备就绪,当准备就绪时,向父进程发送信号,以便它知道现在可以退出
  63. func childReady(srv *falcore.Server) {
  64. pid := syscall.Getpid()
  65. // 等待就绪信号
  66. <-srv.AcceptReady
  67. // 获取父进程并发送子进程就绪的信号
  68. parent := syscall.Getppid()
  69. fmt.Printf("%v 使用SIGUSR1信号杀死父进程 %v\n", pid, parent)
  70. syscall.Kill(parent, syscall.SIGUSR1)
  71. }
  72. // 设置并fork/exec自己。确保保持打开的重要文件描述符不会被子进程重新创建
  73. // 具体来说,是std*和监听套接字
  74. func forker(srv *falcore.Server) (pid int, err error) {
  75. fmt.Printf("现在使用套接字进行fork:%v\n", srv.SocketFd())
  76. mypath := os.Args[0]
  77. args := []string{mypath, "-socket", fmt.Sprintf("%v", srv.SocketFd())}
  78. attr := new(syscall.ProcAttr)
  79. attr.Files = append([]uintptr(nil), 0, 1, 2, uintptr(srv.SocketFd()))
  80. pid, err = syscall.ForkExec(mypath, args, attr)
  81. return
  82. }
  83. // 处理生命周期事件
  84. func handleSignals(srv *falcore.Server) {
  85. var sig os.Signal
  86. var sigChan = make(chan os.Signal)
  87. signal.Notify(sigChan, syscall.SIGHUP, syscall.SIGUSR1, syscall.SIGINT, syscall.SIGTERM, syscall.SIGTSTP)
  88. pid := syscall.Getpid()
  89. for {
  90. sig = <-sigChan
  91. switch sig {
  92. case syscall.SIGHUP:
  93. // 发送给父进程以启动重启
  94. fmt.Println(pid, "收到SIGHUP信号。正在fork。")
  95. cpid, err := forker(srv)
  96. fmt.Println(pid, "已fork,pid为:", cpid, "错误码为:", err)
  97. case syscall.SIGUSR1:
  98. // 子进程准备好接受时发送给父进程
  99. fmt.Println(pid, "收到SIGUSR1信号。停止接受。")
  100. srv.StopAccepting()
  101. case syscall.SIGINT:
  102. fmt.Println(pid, "收到SIGINT信号。正在关闭。")
  103. os.Exit(0)
  104. case syscall.SIGTERM:
  105. fmt.Println(pid, "收到SIGTERM信号。正在终止。")
  106. os.Exit(0)
  107. case syscall.SIGTSTP:
  108. fmt.Println(pid, "收到SIGTSTP信号。停止。")
  109. syscall.Kill(pid, syscall.SIGSTOP)
  110. default:
  111. fmt.Println(pid, "收到", sig, ":忽略")
  112. }
  113. }
  114. }
英文:

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.

  1. package main
  2. import (
  3. &quot;flag&quot;
  4. &quot;fmt&quot;
  5. &quot;github.com/fitstar/falcore&quot;
  6. &quot;github.com/fitstar/falcore/filter&quot;
  7. &quot;net/http&quot;
  8. &quot;os&quot;
  9. &quot;os/signal&quot;
  10. &quot;syscall&quot;
  11. )
  12. // Command line options
  13. var (
  14. port = flag.Int(&quot;port&quot;, 8000, &quot;the port to listen on&quot;)
  15. path = flag.String(&quot;base&quot;, &quot;./www&quot;, &quot;the path to serve files from&quot;)
  16. )
  17. // very simple request filter
  18. func Filter(req *falcore.Request) *http.Response {
  19. pid := syscall.Getpid()
  20. fmt.Println(pid, &quot;GET&quot;, req.HttpRequest.URL.Path)
  21. // return falcore.StringResponse(request.HttpRequest, 200, nil, &quot;OK\n&quot;)
  22. if req.HttpRequest.URL.Path == &quot;/&quot; {
  23. req.HttpRequest.URL.Path = &quot;AppNexus-Interesting.txt&quot; //&quot;/summary.xml&quot;
  24. }
  25. return nil
  26. }
  27. // flag to accept a socket file descriptor
  28. var socketFd = flag.Int(&quot;socket&quot;, -1, &quot;Socket file descriptor&quot;)
  29. func main() {
  30. pid := syscall.Getpid()
  31. flag.Parse()
  32. fmt.Println(&quot;Falcore hot restart running with pid:&quot;, pid, &quot;to hot restart, issue the kill -1&quot;, pid, &quot;command&quot;)
  33. // create the pipeline
  34. pipeline := falcore.NewPipeline()
  35. // upstream filters
  36. pipeline.Upstream.PushBack(falcore.NewRequestFilter(Filter))
  37. // Serve files
  38. pipeline.Upstream.PushBack(&amp;filter.FileFilter{
  39. BasePath: *path,
  40. })
  41. // downstream filters
  42. pipeline.Downstream.PushBack(filter.NewCompressionFilter(nil))
  43. // create the server with the pipeline
  44. srv := falcore.NewServer(*port, pipeline)
  45. // if passed the socket file descriptor, setup the listener that way
  46. // if you don&#39;t have it, the default is to create the socket listener
  47. // with the data passed to falcore.NewServer above (happens in ListenAndServer())
  48. if *socketFd != -1 {
  49. // I know I&#39;m a child process if I get here so I can signal the parent when I&#39;m ready to take over
  50. go childReady(srv)
  51. fmt.Printf(&quot;%v Got socket FD: %v\n&quot;, pid, *socketFd)
  52. srv.FdListen(*socketFd)
  53. }
  54. // using signals to manage the restart lifecycle
  55. go handleSignals(srv)
  56. // start the server
  57. // this is normally blocking forever unless you send lifecycle commands
  58. if err := srv.ListenAndServe(); err != nil {
  59. fmt.Printf(&quot;%v Could not start server: %v&quot;, pid, err)
  60. }
  61. fmt.Printf(&quot;%v Exiting now\n&quot;, pid)
  62. }
  63. // blocks on the server ready and when ready, it sends
  64. // a signal to the parent so that it knows it cna now exit
  65. func childReady(srv *falcore.Server) {
  66. pid := syscall.Getpid()
  67. // wait for the ready signal
  68. &lt;-srv.AcceptReady
  69. // grab the parent and send a signal that the child is ready
  70. parent := syscall.Getppid()
  71. fmt.Printf(&quot;%v Kill parent %v with SIGUSR1\n&quot;, pid, parent)
  72. syscall.Kill(parent, syscall.SIGUSR1)
  73. }
  74. // setup and fork/exec myself. Make sure to keep open important FD&#39;s that won&#39;t get re-created by the child
  75. // specifically, std* and your listen socket
  76. func forker(srv *falcore.Server) (pid int, err error) {
  77. fmt.Printf(&quot;Forking now with socket: %v\n&quot;, srv.SocketFd())
  78. mypath := os.Args[0]
  79. args := []string{mypath, &quot;-socket&quot;, fmt.Sprintf(&quot;%v&quot;, srv.SocketFd())}
  80. attr := new(syscall.ProcAttr)
  81. attr.Files = append([]uintptr(nil), 0, 1, 2, uintptr(srv.SocketFd()))
  82. pid, err = syscall.ForkExec(mypath, args, attr)
  83. return
  84. }
  85. // Handle lifecycle events
  86. func handleSignals(srv *falcore.Server) {
  87. var sig os.Signal
  88. var sigChan = make(chan os.Signal)
  89. signal.Notify(sigChan, syscall.SIGHUP, syscall.SIGUSR1, syscall.SIGINT, syscall.SIGTERM, syscall.SIGTSTP)
  90. pid := syscall.Getpid()
  91. for {
  92. sig = &lt;-sigChan
  93. switch sig {
  94. case syscall.SIGHUP:
  95. // send this to the paraent process to initiate the restart
  96. fmt.Println(pid, &quot;Received SIGHUP. forking.&quot;)
  97. cpid, err := forker(srv)
  98. fmt.Println(pid, &quot;Forked pid:&quot;, cpid, &quot;errno:&quot;, err)
  99. case syscall.SIGUSR1:
  100. // child sends this back to the parent when it&#39;s ready to Accept
  101. fmt.Println(pid, &quot;Received SIGUSR1. Stopping accept.&quot;)
  102. srv.StopAccepting()
  103. case syscall.SIGINT:
  104. fmt.Println(pid, &quot;Received SIGINT. Shutting down.&quot;)
  105. os.Exit(0)
  106. case syscall.SIGTERM:
  107. fmt.Println(pid, &quot;Received SIGTERM. Terminating.&quot;)
  108. os.Exit(0)
  109. case syscall.SIGTSTP:
  110. fmt.Println(pid, &quot;Received SIGTSTP. Stopping.&quot;)
  111. syscall.Kill(pid, syscall.SIGSTOP)
  112. default:
  113. fmt.Println(pid, &quot;Received&quot;, sig, &quot;: ignoring&quot;)
  114. }
  115. }
  116. }

答案1

得分: 1

由于Go不是一种脚本语言,你必须先将源代码编译成二进制文件(使用go buildgo install),然后再进行重启操作。

英文:

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

huangapple
  • 本文由 发表于 2014年2月12日 22:30:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/21730796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定