Try again to connect after Satel disconnects
This commit is contained in:
parent
df2fdc970b
commit
c026863342
12
main.go
12
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 {
|
||||
|
@ -104,8 +106,10 @@ func main() {
|
|||
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,14 +133,20 @@ func main() {
|
|||
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(
|
||||
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)
|
||||
wg.Wait()
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue