1
0
Fork 0

Try again to connect after Satel disconnects

This commit is contained in:
Michał Rudowicz 2024-03-23 10:26:36 +01:00
parent df2fdc970b
commit c026863342
2 changed files with 23 additions and 11 deletions

12
main.go
View File

@ -11,6 +11,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"sync/atomic"
"time" "time"
"git.sr.ht/~michalr/go-satel" "git.sr.ht/~michalr/go-satel"
@ -19,6 +20,7 @@ import (
const ( const (
PersistenceFilename = "hs_wro_last_seen.bin" PersistenceFilename = "hs_wro_last_seen.bin"
retryDelaySec = 25
) )
type RealSleeper struct { type RealSleeper struct {
@ -104,8 +106,10 @@ func main() {
tgEvents = make(chan GenericMessage, 5) tgEvents = make(chan GenericMessage, 5)
logger = log.New(os.Stderr, "Main", log.Lmicroseconds) logger = log.New(os.Stderr, "Main", log.Lmicroseconds)
sleeper = RealSleeper{time.Second * 60} sleeper = RealSleeper{time.Second * 60}
stopRequested = atomic.Bool{}
) )
stopRequested.Store(false)
satelAddr, chatIds, allowedTypes, allowedIndexes, poolInterval := getCmdLineParams(logger) satelAddr, chatIds, allowedTypes, allowedIndexes, poolInterval := getCmdLineParams(logger)
s := makeSatel(satelAddr, poolInterval) s := makeSatel(satelAddr, poolInterval)
@ -129,14 +133,20 @@ func main() {
tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds), tpl), tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds), tpl),
) )
go CloseSatelOnCtrlC(s) go CloseSatelOnCtrlC(s, &stopRequested)
for stopRequested.Load() == false {
for e := range FilterByTypeOrIndex( for e := range FilterByTypeOrIndex(
FilterByLastSeen(s.Events, &wg, &dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)), FilterByLastSeen(s.Events, &wg, &dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)),
&wg, allowedTypes, allowedIndexes) { &wg, allowedTypes, allowedIndexes) {
logger.Print("Received change from SATEL: ", e) logger.Print("Received change from SATEL: ", e)
tgEvents <- GenericMessage{e.BasicEvents} tgEvents <- GenericMessage{e.BasicEvents}
} }
if stopRequested.Load() == false {
logger.Printf("Waiting %d seconds before trying again", retryDelaySec)
time.Sleep(retryDelaySec * time.Second)
}
}
close(tgEvents) close(tgEvents)
wg.Wait() wg.Wait()

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
"sync/atomic"
"git.sr.ht/~michalr/go-satel" "git.sr.ht/~michalr/go-satel"
) )
@ -93,10 +94,11 @@ var SATEL_STRING_TO_CHANGE_TYPE = map[string]satel.ChangeType{
"zone-isolate": satel.ZoneIsolate, "zone-isolate": satel.ZoneIsolate,
} }
func CloseSatelOnCtrlC(s *satel.Satel) { func CloseSatelOnCtrlC(s *satel.Satel, stopRequested *atomic.Bool) {
exitSignal := make(chan os.Signal, 1) exitSignal := make(chan os.Signal, 1)
signal.Notify(exitSignal, os.Interrupt) signal.Notify(exitSignal, os.Interrupt)
<-exitSignal <-exitSignal
stopRequested.Store(true)
s.Close() s.Close()
} }