From 266d206b0742938ded670003c8cf3f50540597e7 Mon Sep 17 00:00:00 2001 From: silentpete Date: Tue, 26 Mar 2019 18:03:18 -0400 Subject: [PATCH 1/2] update main.go to allow for docker stop, exit 0 (#447) Signed-off-by: silentpete --- main.go | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 9103682a..2e6d0d34 100644 --- a/main.go +++ b/main.go @@ -202,6 +202,10 @@ func init() { } func main() { + os.Exit(run()) +} + +func run() int { allowedLevel := promlog.AllowedLevel{} flag.AddFlags(kingpin.CommandLine, &allowedLevel) kingpin.Version(version.Print("blackbox_exporter")) @@ -215,12 +219,12 @@ func main() { if err := sc.ReloadConfig(*configFile); err != nil { level.Error(logger).Log("msg", "Error loading config", "err", err) - os.Exit(1) + return 1 } if *configCheck { level.Info(logger).Log("msg", "Config file is ok exiting...") - os.Exit(0) + return 0 } level.Info(logger).Log("msg", "Loaded config file") @@ -327,9 +331,32 @@ func main() { w.Write(c) }) - level.Info(logger).Log("msg", "Listening on address", "address", *listenAddress) - if err := http.ListenAndServe(*listenAddress, nil); err != nil { - level.Error(logger).Log("msg", "Error starting HTTP server", "err", err) - os.Exit(1) + srv := http.Server{Addr: *listenAddress} + srvc := make(chan struct{}) + term := make(chan os.Signal, 1) + signal.Notify(term, os.Interrupt, syscall.SIGTERM) + + go func() { + level.Info(logger).Log("msg", "Listening on address", "address", *listenAddress) + if err := srv.ListenAndServe(); err != http.ErrServerClosed { + level.Error(logger).Log("msg", "Error starting HTTP server", "err", err) + close(srvc) + } + defer func() { + if err := srv.Close(); err != nil { + level.Error(logger).Log("msg", "Error on closing the server", "err", err) + } + }() + }() + + for { + select { + case <-term: + level.Info(logger).Log("msg", "Received SIGTERM, exiting gracefully...") + return 0 + case <-srvc: + return 1 + } } + } From ac1fc76672c55e70c127660f2fc19c9f3116ba92 Mon Sep 17 00:00:00 2001 From: silentpete Date: Wed, 27 Mar 2019 08:59:02 -0600 Subject: [PATCH 2/2] remove unneeded defer (resolves #447) Signed-off-by: silentpete --- main.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/main.go b/main.go index 2e6d0d34..c9023db8 100644 --- a/main.go +++ b/main.go @@ -342,11 +342,6 @@ func run() int { level.Error(logger).Log("msg", "Error starting HTTP server", "err", err) close(srvc) } - defer func() { - if err := srv.Close(); err != nil { - level.Error(logger).Log("msg", "Error on closing the server", "err", err) - } - }() }() for {