gf icon indicating copy to clipboard operation
gf copied to clipboard

cmd/gf: run AddSigHandlerShutdown cannot work well

Open chaegumi opened this issue 1 year ago • 3 comments

Description

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/gogf/gf/v2/os/gproc"
)

func signalHandlerForMQ(sig os.Signal) {
	fmt.Println("MQ is shutting down due to signal:", sig.String())
	time.Sleep(time.Second)
	fmt.Println("MQ is shut down smoothly")
}

func signalHandlerForMain(sig os.Signal) {
	fmt.Println("MainProcess is shutting down due to signal:", sig.String())
}

func main() {
	fmt.Println("Process start, pid:", os.Getpid())
	gproc.AddSigHandlerShutdown(
		signalHandlerForMQ,
		signalHandlerForMain,
	)
	gproc.Listen()
}

go run能正确运行 gf run不能正确运行 image

Additional

No response

chaegumi avatar Sep 02 '24 01:09 chaegumi

@chaegumi gf run相比较go run有一些不一样的地方:它会监听源码变化实现自动的编译运行gf run会有一个主进程来管理这部分逻辑,并且主进程并不会把接收到的信号转发给子进程,而是强行Kill掉,源码在这里 https://github.com/gogf/gf/blob/183395f0a0f59c1680ce0e78638dd541e4e1c84e/cmd/gf/internal/cmd/cmd_run.go#L204 。我简单想了一下,这里改进可能比较简单,如果接收到的信号,将信号转发给子进程,并且执行Wait方法而不是直接Kill掉。如果没有接收到信号,走原有逻辑。

gqcn avatar Oct 05 '24 06:10 gqcn

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@chaegumi gf run has some differences compared to go run: It will monitor source code changes to implement automatic compilation and running. gf run will have a main process to manage this part of the logic, and the main process will not forward the received signal to the child process, but forcibly Kill ​​it. The source code is here https://github.com/ gogf/gf/blob/183395f0a0f59c1680ce0e78638dd541e4e1c84e/cmd/gf/internal/cmd/cmd_run.go#L204. I briefly thought about it, and the improvement here may be relatively simple. If a signal is received, forward the signal to the child process, and execute the Wait method instead of Kill ​​directly. If no signal is received, the original logic is followed.

Issues-translate-bot avatar Oct 05 '24 06:10 Issues-translate-bot

Hello @chaegumi. We like your proposal/feedback and would appreciate a contribution via a Pull Request by you or another community member. We thank you in advance for your contribution and are looking forward to reviewing it! 你好 @chaegumi。我们喜欢您的提案/反馈,并希望您或其他社区成员通过拉取请求做出贡献。我们提前感谢您的贡献,并期待对其进行审查。

github-actions[bot] avatar Oct 05 '24 06:10 github-actions[bot]