Skip to content

Commit 409238d

Browse files
taktv6Oliver Geiselhardt-Herms
and
Oliver Geiselhardt-Herms
authored
Allow stopping junos_exporter with sigint/ctrl+c (#270)
Co-authored-by: Oliver Geiselhardt-Herms <ogh@deepl.com>
1 parent eb6dac4 commit 409238d

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

main.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ import (
1515
"syscall"
1616
"time"
1717

18-
"go.opentelemetry.io/otel/codes"
19-
18+
"github.com/czerwonk/junos_exporter/internal/config"
2019
"github.com/czerwonk/junos_exporter/pkg/connector"
2120

2221
"github.com/prometheus/client_golang/prometheus"
2322
"github.com/prometheus/client_golang/prometheus/promhttp"
24-
log "github.com/sirupsen/logrus"
23+
"go.opentelemetry.io/otel/codes"
2524

26-
"github.com/czerwonk/junos_exporter/internal/config"
25+
log "github.com/sirupsen/logrus"
2726
)
2827

2928
const version string = "0.12.7"
@@ -126,12 +125,12 @@ func main() {
126125
}
127126
defer shutdownTracing()
128127

129-
initChannels()
128+
initChannels(ctx)
130129

131130
startServer()
132131
}
133132

134-
func initChannels() {
133+
func initChannels(ctx context.Context) {
135134
hup := make(chan os.Signal, 1)
136135
signal.Notify(hup, syscall.SIGHUP)
137136

@@ -155,15 +154,21 @@ func initChannels() {
155154
} else {
156155
rc <- nil
157156
}
157+
case <-ctx.Done():
158+
shutdown()
158159
case <-term:
159-
log.Infoln("Closing connections to devices")
160-
connManager.Close()
161-
os.Exit(0)
160+
shutdown()
162161
}
163162
}
164163
}()
165164
}
166165

166+
func shutdown() {
167+
log.Infoln("Closing connections to devices")
168+
connManager.Close()
169+
os.Exit(0)
170+
}
171+
167172
func printVersion() {
168173
fmt.Println("junos_exporter")
169174
fmt.Printf("Version: %s\n", version)

0 commit comments

Comments
 (0)