diff --git a/main.go b/main.go index a58ed8d..80a6213 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "strconv" "strings" "sync" + "sync/atomic" "time" "git.sr.ht/~michalr/go-satel" @@ -19,6 +20,7 @@ import ( const ( PersistenceFilename = "hs_wro_last_seen.bin" + retryDelaySec = 25 ) type RealSleeper struct { @@ -100,12 +102,14 @@ func getPersistenceFilePath() string { func main() { var ( - wg sync.WaitGroup - tgEvents = make(chan GenericMessage, 5) - logger = log.New(os.Stderr, "Main", log.Lmicroseconds) - sleeper = RealSleeper{time.Second * 60} + wg sync.WaitGroup + tgEvents = make(chan GenericMessage, 5) + logger = log.New(os.Stderr, "Main", log.Lmicroseconds) + sleeper = RealSleeper{time.Second * 60} + stopRequested = atomic.Bool{} ) + stopRequested.Store(false) satelAddr, chatIds, allowedTypes, allowedIndexes, poolInterval := getCmdLineParams(logger) s := makeSatel(satelAddr, poolInterval) @@ -129,13 +133,19 @@ func main() { tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds), tpl), ) - go CloseSatelOnCtrlC(s) + go CloseSatelOnCtrlC(s, &stopRequested) - for e := range FilterByTypeOrIndex( - FilterByLastSeen(s.Events, &wg, &dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)), - &wg, allowedTypes, allowedIndexes) { - logger.Print("Received change from SATEL: ", e) - tgEvents <- GenericMessage{e.BasicEvents} + for stopRequested.Load() == false { + for e := range FilterByTypeOrIndex( + FilterByLastSeen(s.Events, &wg, &dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)), + &wg, allowedTypes, allowedIndexes) { + logger.Print("Received change from SATEL: ", e) + tgEvents <- GenericMessage{e.BasicEvents} + } + if stopRequested.Load() == false { + logger.Printf("Waiting %d seconds before trying again", retryDelaySec) + time.Sleep(retryDelaySec * time.Second) + } } close(tgEvents) diff --git a/satel_utils.go b/satel_utils.go index cb12836..68b1fd3 100644 --- a/satel_utils.go +++ b/satel_utils.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "os/signal" + "sync/atomic" "git.sr.ht/~michalr/go-satel" ) @@ -93,10 +94,11 @@ var SATEL_STRING_TO_CHANGE_TYPE = map[string]satel.ChangeType{ "zone-isolate": satel.ZoneIsolate, } -func CloseSatelOnCtrlC(s *satel.Satel) { +func CloseSatelOnCtrlC(s *satel.Satel, stopRequested *atomic.Bool) { exitSignal := make(chan os.Signal, 1) signal.Notify(exitSignal, os.Interrupt) <-exitSignal + stopRequested.Store(true) s.Close() }