From a1f5ce5d2bf3cde26a2e063c20cd867e9fc475c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Rudowicz?= Date: Sat, 30 Mar 2024 07:21:37 +0100 Subject: [PATCH] Remove reconnection attempts, return error unless ctrl+c was sent --- main.go | 27 +++++++++++++-------------- satel_utils.go | 4 ++-- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index 4cdee58..07724d4 100644 --- a/main.go +++ b/main.go @@ -52,10 +52,10 @@ func main() { tgEvents = make(chan GenericMessage, 5) logger = log.New(os.Stderr, "Main", log.Lmicroseconds) sleeper = RealSleeper{time.Second * 60} - stopRequested = atomic.Bool{} + cleanShutdown = atomic.Bool{} ) - stopRequested.Store(false) + cleanShutdown.Store(false) config := MakeConfig(logger) s := makeSatel(config.SatelAddr, config.PoolInterval.GetDuration()) @@ -79,21 +79,20 @@ func main() { tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds), tpl), ) - go CloseSatelOnCtrlC(s, &stopRequested) + go CloseSatelOnCtrlC(s, &cleanShutdown) - for stopRequested.Load() == false { - for e := range FilterByTypeOrIndex( - FilterByLastSeen(s.Events, &wg, &dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)), - &wg, config.AllowedTypes, config.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) - } + for e := range FilterByTypeOrIndex( + FilterByLastSeen(s.Events, &wg, &dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)), + &wg, config.AllowedTypes, config.AllowedIndexes) { + logger.Print("Received change from SATEL: ", e) + tgEvents <- GenericMessage{e.BasicEvents} } + logger.Print("Closing...") close(tgEvents) wg.Wait() + if cleanShutdown.Load() { + os.Exit(0) + } + os.Exit(1) } diff --git a/satel_utils.go b/satel_utils.go index 68b1fd3..7c9df4c 100644 --- a/satel_utils.go +++ b/satel_utils.go @@ -94,11 +94,11 @@ var SATEL_STRING_TO_CHANGE_TYPE = map[string]satel.ChangeType{ "zone-isolate": satel.ZoneIsolate, } -func CloseSatelOnCtrlC(s *satel.Satel, stopRequested *atomic.Bool) { +func CloseSatelOnCtrlC(s *satel.Satel, cleanShutdown *atomic.Bool) { exitSignal := make(chan os.Signal, 1) signal.Notify(exitSignal, os.Interrupt) <-exitSignal - stopRequested.Store(true) + cleanShutdown.Store(true) s.Close() }