1

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:

[1]Force priority of go select statement

[2]Priority in Go select statement workaround

0

1 Answer 1

2

If I understood correctly what you want to do is to replace the range-over-channel loop with an infinite loop. i.e. for range ticker.C { ... } -> for { ... }.

And if you want the program, rather than the report function, to finish immediately without waiting on the code in the case <-ticker.C: block to finish, you should execute that block of code in a separate goroutine and update the case <-sigC: block from return nil to os.Exit(1).

func report() {
    ticker := time.NewTicker(timeConfig)
    defer ticker.Stop()

    sigC := make(chan os.Signal, 1)
    signal.Notify(sigC, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, os.Interrupt)

    for {
        select {
        case <-ticker.C:
            go func() {
                connection = connectionInit()

                tagValues, err := fetchLatestTags()
                if err != nil {
                    return
                }
                if len(tagValues) >= threshold {
                    metrics, err := fetchMetrics(tagValues)
                    if err != nil {
                        return
                    }
                    if stdout {
                        err = locally(metrics)
                        if err != nil {
                            return
                        }
                    } else {
                        err = sendMail(metrics)
                        if err != nil {
                            return
                        }
                    }
                }
                connection.Close()
            }()
        case <-sigC:
            os.Exit(1)
        }
    }
}
2
  • 1
    The errors inside the IFs had to be removed and so did the last return nil as the code doesn’t reach it but it works. Thank you :)
    – Lique
    Mar 17, 2022 at 13:36
  • @Lique ah yes, sorry about that. Fixed it now.
    – mkopriva
    Mar 17, 2022 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.