Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update main.go to allow for docker stop, exit 0 (resolves #447) #449

Merged
merged 2 commits into from
Mar 27, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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")
Expand Down Expand Up @@ -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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need a defer here

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
}
}

}