1
0
mirror of https://github.com/cmur2/miflorad.git synced 2024-11-01 00:56:17 +01:00

miflorad: shorten process termination timeout by listening for quit on every retry

This commit is contained in:
cn 2019-04-23 16:15:38 +02:00
parent 4531a05af3
commit d241c8c6be

View File

@ -214,16 +214,24 @@ func connectPeripheral(peripheral *peripheral, send chan mifloraMetric) error {
return nil return nil
} }
func readPeripheral(peripheral *peripheral, send chan mifloraMetric) error { func readPeripheral(quit chan struct{}, peripheral *peripheral, send chan mifloraMetric) error {
var err error var err error
fmt.Fprintf(os.Stderr, "Scanning for %s...", peripheral.id) fmt.Fprintf(os.Stderr, "Scanning for %s...", peripheral.id)
L:
for retry := 0; retry < *readRetries; retry++ { for retry := 0; retry < *readRetries; retry++ {
// check for quit signal (non-blocking) and terminate
select {
case <-quit:
break L
default:
}
fmt.Fprintf(os.Stderr, " %d", retry+1) fmt.Fprintf(os.Stderr, " %d", retry+1)
err = connectPeripheral(peripheral, send) err = connectPeripheral(peripheral, send)
// stop retrying once we have a success, last err will be returned (or nil) // stop retrying once we have a success, last err will be returned (or nil)
if err == nil { if err == nil {
fmt.Fprintf(os.Stderr, ".") fmt.Fprintf(os.Stderr, ".")
break break L
} }
} }
fmt.Fprintf(os.Stderr, "\n") fmt.Fprintf(os.Stderr, "\n")
@ -232,14 +240,7 @@ func readPeripheral(peripheral *peripheral, send chan mifloraMetric) error {
func readAllPeripherals(quit chan struct{}, send chan mifloraMetric) { func readAllPeripherals(quit chan struct{}, send chan mifloraMetric) {
for _, peripheral := range allPeripherals { for _, peripheral := range allPeripherals {
// check for quit signal (non-blocking) and terminate err := readPeripheral(quit, peripheral, send)
select {
case <-quit:
return
default:
}
err := readPeripheral(peripheral, send)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read peripheral %s, err: %s\n", peripheral.id, err) fmt.Fprintf(os.Stderr, "Failed to read peripheral %s, err: %s\n", peripheral.id, err)
// id := common.MifloraGetAlphaNumericID(peripheral.id) // id := common.MifloraGetAlphaNumericID(peripheral.id)
@ -350,8 +351,8 @@ func main() {
fmt.Fprintf(os.Stderr, "Received %s! Stopping...\n", signal) fmt.Fprintf(os.Stderr, "Received %s! Stopping...\n", signal)
intervalTicker.Stop() intervalTicker.Stop()
close(quit) close(quit)
// wait for last readPeripheral to finish (worst case) // wait for last connectPeripheral to finish (worst case)
time.Sleep(*scanTimeout * time.Duration(*readRetries)) time.Sleep(*scanTimeout)
mqttClient.Disconnect(1000) mqttClient.Disconnect(1000)