I have this function so that the methods inside, run on a ticker. My problem is that the second case, sigC, only works after the case ticker.C is complete, which is not ideal because the program runs with flags, so if I want to change the flags and alter the program's behaviour, I have to wait for the ticker methods to finish running, and that can take some time.
My objective is that when I press Ctrl+C, the program finishes running immediately.
func report() error {
ticker := time.NewTicker(timeConfig)
sigC := make(chan os.Signal, 1)
signal.Notify(sigC, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, os.Interrupt)
for range ticker.C {
select {
case <-ticker.C:
connection = connectionInit()
tagValues, err := fetchLatestTags()
if err != nil {
return err
}
if len(tagValues) >= threshold {
metrics, err := fetchMetrics(tagValues)
if err != nil {
return err
}
if stdout {
err = locally(metrics)
if err != nil {
return err
}
} else {
err = sendMail(metrics)
if err != nil {
return err
}
}
}
connection.Close()
case <-sigC:
return nil
}
}
return nil
}
I tried these solutions which are pretty similar to each other but to no avail: