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

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

falcore hot restart does not reload main code

问题

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

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

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

例如,我将默认提供的文件从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"
)

// 命令行选项
var (
	port = flag.Int("port", 8000, "要监听的端口")
	path = flag.String("base", "./www", "要提供文件的路径")
)

// 非常简单的请求过滤器
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
}

// 接受套接字文件描述符的标志
var socketFd = flag.Int("socket", -1, "套接字文件描述符")

func main() {
	pid := syscall.Getpid()
	flag.Parse()
	fmt.Println("Falcore热重启正在运行,pid为:", pid, "要进行热重启,请发出kill -1", pid, "命令")

	// 创建流水线
	pipeline := falcore.NewPipeline()

	// 上游过滤器
	pipeline.Upstream.PushBack(falcore.NewRequestFilter(Filter))

	// 提供文件
	pipeline.Upstream.PushBack(&filter.FileFilter{
		BasePath: *path,
	})

	// 下游过滤器
	pipeline.Downstream.PushBack(filter.NewCompressionFilter(nil))

	// 使用流水线创建服务器
	srv := falcore.NewServer(*port, pipeline)

	// 如果传递了套接字文件描述符,则使用该方式设置监听器
	// 如果没有传递,则默认使用 falcore.NewServer 上面传递的数据创建套接字监听器(在 ListenAndServer() 中发生)
	if *socketFd != -1 {
		// 我知道如果到达这里,我是一个子进程,所以我可以在准备接管时向父进程发送信号
		go childReady(srv)
		fmt.Printf("%v 获取到套接字FD:%v\n", pid, *socketFd)
		srv.FdListen(*socketFd)
	}

	// 使用信号管理重启生命周期
	go handleSignals(srv)

	// 启动服务器
	// 除非发送生命周期命令,否则通常会一直阻塞
	if err := srv.ListenAndServe(); err != nil {
		fmt.Printf("%v 无法启动服务器:%v", pid, err)
	}
	fmt.Printf("%v 现在退出\n", pid)
}

// 阻塞在服务器准备就绪,当准备就绪时,向父进程发送信号,以便它知道现在可以退出
func childReady(srv *falcore.Server) {
	pid := syscall.Getpid()
	// 等待就绪信号
	<-srv.AcceptReady
	// 获取父进程并发送子进程就绪的信号
	parent := syscall.Getppid()
	fmt.Printf("%v 使用SIGUSR1信号杀死父进程 %v\n", pid, parent)
	syscall.Kill(parent, syscall.SIGUSR1)
}

// 设置并fork/exec自己。确保保持打开的重要文件描述符不会被子进程重新创建
// 具体来说,是std*和监听套接字
func forker(srv *falcore.Server) (pid int, err error) {
	fmt.Printf("现在使用套接字进行fork:%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
}

// 处理生命周期事件
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:
			// 发送给父进程以启动重启
			fmt.Println(pid, "收到SIGHUP信号。正在fork。")
			cpid, err := forker(srv)
			fmt.Println(pid, "已fork,pid为:", cpid, "错误码为:", err)
		case syscall.SIGUSR1:
			// 子进程准备好接受时发送给父进程
			fmt.Println(pid, "收到SIGUSR1信号。停止接受。")
			srv.StopAccepting()
		case syscall.SIGINT:
			fmt.Println(pid, "收到SIGINT信号。正在关闭。")
			os.Exit(0)
		case syscall.SIGTERM:
			fmt.Println(pid, "收到SIGTERM信号。正在终止。")
			os.Exit(0)
		case syscall.SIGTSTP:
			fmt.Println(pid, "收到SIGTSTP信号。停止。")
			syscall.Kill(pid, syscall.SIGSTOP)
		default:
			fmt.Println(pid, "收到", sig, ":忽略")
		}
	}
}
英文:

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 (
&quot;flag&quot;
&quot;fmt&quot;
&quot;github.com/fitstar/falcore&quot;
&quot;github.com/fitstar/falcore/filter&quot;
&quot;net/http&quot;
&quot;os&quot;
&quot;os/signal&quot;
&quot;syscall&quot;
)
// Command line options
var (
port = flag.Int(&quot;port&quot;, 8000, &quot;the port to listen on&quot;)
path = flag.String(&quot;base&quot;, &quot;./www&quot;, &quot;the path to serve files from&quot;)
)
// very simple request filter
func Filter(req *falcore.Request) *http.Response {
pid := syscall.Getpid()
fmt.Println(pid, &quot;GET&quot;, req.HttpRequest.URL.Path)
// return falcore.StringResponse(request.HttpRequest, 200, nil, &quot;OK\n&quot;)
if req.HttpRequest.URL.Path == &quot;/&quot; {
req.HttpRequest.URL.Path = &quot;AppNexus-Interesting.txt&quot; //&quot;/summary.xml&quot;
}
return nil
}
// flag to accept a socket file descriptor
var socketFd = flag.Int(&quot;socket&quot;, -1, &quot;Socket file descriptor&quot;)
func main() {
pid := syscall.Getpid()
flag.Parse()
fmt.Println(&quot;Falcore hot restart running with pid:&quot;, pid, &quot;to hot restart, issue the kill -1&quot;, pid, &quot;command&quot;)
// create the pipeline
pipeline := falcore.NewPipeline()
// upstream filters
pipeline.Upstream.PushBack(falcore.NewRequestFilter(Filter))
// Serve files
pipeline.Upstream.PushBack(&amp;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&#39;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&#39;m a child process if I get here so I can signal the parent when I&#39;m ready to take over
go childReady(srv)
fmt.Printf(&quot;%v Got socket FD: %v\n&quot;, 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(&quot;%v Could not start server: %v&quot;, pid, err)
}
fmt.Printf(&quot;%v Exiting now\n&quot;, 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
&lt;-srv.AcceptReady
// grab the parent and send a signal that the child is ready
parent := syscall.Getppid()
fmt.Printf(&quot;%v Kill parent %v with SIGUSR1\n&quot;, pid, parent)
syscall.Kill(parent, syscall.SIGUSR1)
}
// setup and fork/exec myself. Make sure to keep open important FD&#39;s that won&#39;t get re-created by the child
// specifically, std* and your listen socket
func forker(srv *falcore.Server) (pid int, err error) {
fmt.Printf(&quot;Forking now with socket: %v\n&quot;, srv.SocketFd())
mypath := os.Args[0]
args := []string{mypath, &quot;-socket&quot;, fmt.Sprintf(&quot;%v&quot;, 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 = &lt;-sigChan
switch sig {
case syscall.SIGHUP:
// send this to the paraent process to initiate the restart
fmt.Println(pid, &quot;Received SIGHUP.  forking.&quot;)
cpid, err := forker(srv)
fmt.Println(pid, &quot;Forked pid:&quot;, cpid, &quot;errno:&quot;, err)
case syscall.SIGUSR1:
// child sends this back to the parent when it&#39;s ready to Accept
fmt.Println(pid, &quot;Received SIGUSR1.  Stopping accept.&quot;)
srv.StopAccepting()
case syscall.SIGINT:
fmt.Println(pid, &quot;Received SIGINT.  Shutting down.&quot;)
os.Exit(0)
case syscall.SIGTERM:
fmt.Println(pid, &quot;Received SIGTERM.  Terminating.&quot;)
os.Exit(0)
case syscall.SIGTSTP:
fmt.Println(pid, &quot;Received SIGTSTP.  Stopping.&quot;)
syscall.Kill(pid, syscall.SIGSTOP)
default:
fmt.Println(pid, &quot;Received&quot;, sig, &quot;: ignoring&quot;)
}
}
}

答案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:

确定