Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/gf: run AddSigHandlerShutdown cannot work well #3752

Open
chaegumi opened this issue Sep 2, 2024 · 3 comments
Open

cmd/gf: run AddSigHandlerShutdown cannot work well #3752

chaegumi opened this issue Sep 2, 2024 · 3 comments
Labels
enhancement help wanted planned This issue/proposal is planned into our next steps.

Comments

@chaegumi
Copy link

chaegumi commented Sep 2, 2024

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

@gqcn gqcn added the planned This issue/proposal is planned into our next steps. label Oct 5, 2024
@gqcn
Copy link
Member

gqcn commented Oct 5, 2024

@chaegumi gf run相比较go run有一些不一样的地方:它会监听源码变化实现自动的编译运行gf run会有一个主进程来管理这部分逻辑,并且主进程并不会把接收到的信号转发给子进程,而是强行Kill掉,源码在这里

if err := process.Kill(); err != nil {
。我简单想了一下,这里改进可能比较简单,如果接收到的信号,将信号转发给子进程,并且执行Wait方法而不是直接Kill掉。如果没有接收到信号,走原有逻辑。

@Issues-translate-bot
Copy link

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.

@github-actions github-actions bot removed the inactive label Oct 5, 2024
Copy link

github-actions bot commented Oct 5, 2024

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。我们喜欢您的提案/反馈,并希望您或其他社区成员通过拉取请求做出贡献。我们提前感谢您的贡献,并期待对其进行审查。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement help wanted planned This issue/proposal is planned into our next steps.
Projects
None yet
Development

No branches or pull requests

3 participants