1
0
Fork 0

Properly close Satel connection on interrupt

This commit is contained in:
Michał Rudowicz 2024-02-18 19:36:54 +01:00
parent 9360d89fe1
commit 0e01644c3b
2 changed files with 6 additions and 19 deletions

View File

@ -151,8 +151,10 @@ func main() {
tgSenderWorker(tgEvents, &wg, sleeper, log.New(os.Stderr, "TgSender", log.Lmicroseconds)),
tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds)))
go CloseSatelOnCtrlC(s)
for e := range FilterByIndex(FilterByType(
FilterByLastSeen(CloseChanOnCtrlC(s.Events), "hs_wro_last_seen.bin", log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)),
FilterByLastSeen(s.Events, "hs_wro_last_seen.bin", log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)),
allowedTypes),
allowedIndexes) {
logger.Print("Received change from SATEL: ", e)

View File

@ -93,26 +93,11 @@ var SATEL_STRING_TO_CHANGE_TYPE = map[string]satel.ChangeType{
"zone-isolate": satel.ZoneIsolate,
}
func CloseChanOnCtrlC(ev <-chan satel.Event) <-chan satel.Event {
returnChan := make(chan satel.Event)
func CloseSatelOnCtrlC(s *satel.Satel) {
exitSignal := make(chan os.Signal, 1)
signal.Notify(exitSignal, os.Interrupt)
go func() {
defer close(returnChan)
loop:
for {
select {
case <-exitSignal:
return
case e, ok := <-ev:
if !ok {
break loop
}
returnChan <- e
}
}
}()
return returnChan
<-exitSignal
s.Close()
}
func StringToSatelChangeType(s string) (satel.ChangeType, error) {