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)), tgSenderWorker(tgEvents, &wg, sleeper, log.New(os.Stderr, "TgSender", log.Lmicroseconds)),
tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds))) tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds)))
go CloseSatelOnCtrlC(s)
for e := range FilterByIndex(FilterByType( 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), allowedTypes),
allowedIndexes) { allowedIndexes) {
logger.Print("Received change from SATEL: ", e) 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, "zone-isolate": satel.ZoneIsolate,
} }
func CloseChanOnCtrlC(ev <-chan satel.Event) <-chan satel.Event { func CloseSatelOnCtrlC(s *satel.Satel) {
returnChan := make(chan satel.Event)
exitSignal := make(chan os.Signal, 1) exitSignal := make(chan os.Signal, 1)
signal.Notify(exitSignal, os.Interrupt) signal.Notify(exitSignal, os.Interrupt)
go func() { <-exitSignal
defer close(returnChan) s.Close()
loop:
for {
select {
case <-exitSignal:
return
case e, ok := <-ev:
if !ok {
break loop
}
returnChan <- e
}
}
}()
return returnChan
} }
func StringToSatelChangeType(s string) (satel.ChangeType, error) { func StringToSatelChangeType(s string) (satel.ChangeType, error) {