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)),
|
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)
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
Loading…
Reference in New Issue