cron
cron copied to clipboard
is there any way to work with K8s liveness probe?
Hello, I would like to use this library to create a worker that runs in K8s, is there any way to work with K8s liveness probe?
package main
import (
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
// Simulated worker logic
var workerRunning = true
// Liveness Probe Handler
func livenessHandler(w http.ResponseWriter, r *http.Request) {
if workerRunning {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Worker is alive"))
} else {
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte("Worker is not alive"))
}
}
func main() {
// Register the liveness probe HTTP handler
http.HandleFunc("/liveness", livenessHandler)
// Start HTTP server for the liveness probe
go func() {
fmt.Println("Starting HTTP server for liveness probe on port 8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
fmt.Printf("Error starting HTTP server: %v\n", err)
}
}()
// Simulate some worker logic
fmt.Println("Worker started...")
for {
// Simulate worker failure after some time
time.Sleep(10 * time.Second)
workerRunning = false
fmt.Println("Simulating worker failure...")
// Handle shutdown gracefully
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
<-sigs
fmt.Println("Worker shutting down...")
return
}
}