2 回答

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超5個(gè)贊
文檔不是特別清楚,但是有一個(gè)接口可用于任何Fx模塊,該接口具有請(qǐng)求正常關(guān)閉應(yīng)用程序的方法。ShutdownerShutdown
下面是示例包的修改部分,它將在收到請(qǐng)求時(shí)將其簡(jiǎn)單地關(guān)閉:
func NewHandler(logger *log.Logger, shutdowner fx.Shutdowner) (http.Handler, error) {
logger.Print("Executing NewHandler.")
return http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
logger.Print("Got a request. Requesting shutdown now that I've gotten one request.")
shutdowner.Shutdown()
}), nil
}
編輯:以下是修改解決方案的方法:
func register(shutdowner fx.Shutdowner) {
time.Sleep(5*time.Seconds)
shutdowner.Shutdown()
}

TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以將其包裝在 go 例程中,并使用上下文優(yōu)雅地退出。
import (
"context"
"log"
" go.uber.org/fx"
)
func main() {
f := fx.New(fx.Invoke(register))
go func() {
f.Run()
}()
stopCh := make(chan os.Signal)
signal.Notify(stopCh, syscall.SIGINT, syscall.SIGTERM)
<-stopCh
if err := f.Stop(context.Background()); err != nil {
log.Printf("error stopping gracefully")
}
}
func register() {
time.Sleep(5*time.Seconds)
// shutdown somehow
}
- 2 回答
- 0 關(guān)注
- 137 瀏覽
添加回答
舉報(bào)