Properly close Satel connection on interrupt
This commit is contained in:
parent
9360d89fe1
commit
0e01644c3b
4
main.go
4
main.go
|
@ -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)
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue