Merge branch 'add-mechanism-for-graceful-shutdown' into 'main'

Add mechanism for graceful shutdown

See merge request hectorjsmith/fail2ban-prometheus-exporter!63
This commit is contained in:
Hector 2022-02-20 08:25:04 +00:00
commit cc68fe3f01
2 changed files with 20 additions and 4 deletions

View file

@ -20,4 +20,4 @@ WORKDIR /app
# Copy compiled binary to release image # Copy compiled binary to release image
COPY --from=build /build/src/exporter /app/fail2ban-prometheus-exporter COPY --from=build /build/src/exporter /app/fail2ban-prometheus-exporter
ENTRYPOINT /app/fail2ban-prometheus-exporter ENTRYPOINT ["/app/fail2ban-prometheus-exporter"]

View file

@ -6,11 +6,13 @@ import (
"fail2ban-prometheus-exporter/collector/f2b" "fail2ban-prometheus-exporter/collector/f2b"
"fail2ban-prometheus-exporter/collector/textfile" "fail2ban-prometheus-exporter/collector/textfile"
"fmt" "fmt"
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"net/http"
"os"
"os/signal"
"syscall"
) )
const ( const (
@ -54,6 +56,7 @@ func main() {
if appSettings.VersionMode { if appSettings.VersionMode {
printAppVersion() printAppVersion()
} else { } else {
handleGracefulShutdown()
log.Printf("fail2ban exporter version %s", version) log.Printf("fail2ban exporter version %s", version)
log.Printf("starting server at %s", appSettings.MetricsAddress) log.Printf("starting server at %s", appSettings.MetricsAddress)
@ -85,3 +88,16 @@ func main() {
log.Print(err) log.Print(err)
} }
} }
func handleGracefulShutdown() {
var signals = make(chan os.Signal)
signal.Notify(signals, syscall.SIGTERM)
signal.Notify(signals, syscall.SIGINT)
go func() {
sig := <-signals
log.Printf("caught signal: %+v", sig)
os.Exit(0)
}()
}