我想撰寫一個 golang 程式,在 kubernetes 的容器中作為服務運行。程式應該一直運行并且不會自行終止——除非發生錯誤。如果一個 SIGTERM 來自 kubelet / kubernetes,因為 pod 應該被洗掉,程式應該退出。
在我的第一種方法中,我實作了一個應該每分鐘運行一次的 for 回圈。在這個回圈中,程式從另一個服務中查詢資源并對其進行處理。然后,該服務應“休眠”一分鐘,然后再次執行這些步驟。在“睡眠”階段,如果有 SIGTERM,程式應該立即回應。
func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
//restart loop every minute
ticker := time.NewTicker(60 * time.Second)
defer ticker.Stop()
// while true loop
for {
select {
case <-c:
fmt.Println("Break the loop")
return
case <-ticker.C:
fmt.Println("Hello in a loop")
}
}
}
我目前的方法有兩個問題。首先,我第一次運行 for 回圈時必須等待一分鐘。我可以以某種方式對其進行配置,以便計時器僅在第一次運行后運行嗎?
第二個問題是程式在運行之間不應該做任何事情——除了對 SIGTERM 做出反應。
我不確定我解決問題的方法是否正確。我剛開始使用 GO 語言。也許有更好的方法來解決我的問題。
uj5u.com熱心網友回復:
你可以在select宣告之外做你的作業,然后在票務通道案例中繼續。這將立即執行您的作業一次,然后每次代碼滴答作響。
您的函式看起來不錯,但最好在可能的情況下使用背景關系。
許多包(資料庫、IO 等)都可以選擇指定背景關系,這通常用于定義超時。在您的情況下,將相同(或子)背景關系傳遞給這些包意味著它們也尊重 sigterm。
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
ctx := cancelCtxOnSigterm(context.Background())
startWork(ctx)
}
// cancelCtxOnSigterm returns a Context that will be cancelled when the program receives a sigterm.
func cancelCtxOnSigterm(ctx context.Context) context.Context {
exitCh := make(chan os.Signal, 1)
signal.Notify(exitCh, os.Interrupt, syscall.SIGTERM)
ctx, cancel := context.WithCancel(ctx)
go func() {
<-exitCh
cancel()
}()
return ctx
}
// startWork performs a task every 60 seconds until the context is done.
func startWork(ctx context.Context) {
ticker := time.NewTicker(60 * time.Second)
defer ticker.Stop()
for {
// Do work here so we don't need duplicate calls. It will run immediately, and again every minute as the loop continues.
if err := work(ctx); err != nil {
fmt.Printf("failed to do work: %s", err)
}
select {
case <-ticker.C:
continue
case <-ctx.Done():
return
}
}
}
func work(ctx context.Context) error {
fmt.Println("doing work")
return nil
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/485168.html
標籤:去
下一篇:將變數限制為自身和其他特定包
