From 31c937ec220968f64857c375008cc5376dcac701 Mon Sep 17 00:00:00 2001 From: Bernhard Tittelbach Date: Wed, 11 Sep 2013 13:34:06 +0000 Subject: [PATCH] some defers in case jabber server disappears --- r3-netstatus/main.go | 47 +++++++++++++++++++++-------------- r3-netstatus/r3xmppbot/r3xmppbot.go | 14 +++++++++++ 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/r3-netstatus/main.go b/r3-netstatus/main.go index cd0f601..ec65f06 100644 --- a/r3-netstatus/main.go +++ b/r3-netstatus/main.go @@ -52,6 +52,15 @@ func composeMessage(present, locked, shut bool, who string, ts int64) string { func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface{}) { events := ps.Sub("presence","door","buttons") + + defer func() { + if x := recover(); x != nil { + fmt.Printf("handleIncomingXMPPStanzas: run time panic: %v", x) + ps.Unsub(events, "presence","door","buttons") + close(xmpp_presence_events_chan_) + } + }() + var present, locked, shut bool = false, true, true var who string button_msg := "The button has been pressed ! Propably someone is bored and in need of company ! ;-)" @@ -79,14 +88,9 @@ func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface } func main() { - var err error + var xmpperr error var bot *r3xmppbot.XmppBot - bot, xmpp_presence_events_chan_, err = r3xmppbot.NewStartedBot(xmpp_login_.jid, xmpp_login_.pass, xmpp_bot_authstring_, xmpp_state_save_dir_, true) - if err != nil { - fmt.Println(err) - return - } - defer bot.StopBot() + bot, xmpp_presence_events_chan_, xmpperr = r3xmppbot.NewStartedBot(xmpp_login_.jid, xmpp_login_.pass, xmpp_bot_authstring_, xmpp_state_save_dir_, true) newlinequeue := make(chan string, 1) ps := pubsub.New(1) @@ -95,16 +99,23 @@ func main() { defer ps.Shutdown() //~ defer brn.Shutdown() - ticker := time.NewTicker(time.Duration(7) * time.Minute) go EventToWeb(ps) - go EventToXMPP(ps, xmpp_presence_events_chan_) - go ReadFromUSocket(presence_socket_path_, newlinequeue) - for { - select { - case e := <-newlinequeue: - ParseSocketInputLine(e, ps) //, brn) - case <-ticker.C: - ps.Pub(TimeTick{time.Now().Unix()}, "publishjson") - } - } + if xmpperr == nil { + defer bot.StopBot() + go EventToXMPP(ps, xmpp_presence_events_chan_) + } else { + fmt.Println(xmpperr) + fmt.Println("XMPP Bot disabled") + } + go ReadFromUSocket(presence_socket_path_, newlinequeue) + ticker := time.NewTicker(time.Duration(7) * time.Minute) + + for { + select { + case e := <-newlinequeue: + ParseSocketInputLine(e, ps) //, brn) + case <-ticker.C: + ps.Pub(TimeTick{time.Now().Unix()}, "publishjson") + } + } } diff --git a/r3-netstatus/r3xmppbot/r3xmppbot.go b/r3-netstatus/r3xmppbot/r3xmppbot.go index b1980da..f795afc 100644 --- a/r3-netstatus/r3xmppbot/r3xmppbot.go +++ b/r3-netstatus/r3xmppbot/r3xmppbot.go @@ -157,6 +157,12 @@ func init() { func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presence_events <- chan interface{}, jabber_events <- chan JabberEvent) { var last_status_msg *string + defer func() { + if x := recover(); x != nil { + log.Printf("handleEventsforXMPP: run time panic: %v", x) + } + }() + for { select { case pe := <-presence_events: @@ -278,6 +284,14 @@ func (botdata *XmppBot) handleIncomingMessageDialog(inmsg xmpp.Message, xmppout } func (botdata *XmppBot) handleIncomingXMPPStanzas(xmppin <- chan xmpp.Stanza, xmppout chan<- xmpp.Stanza, jabber_events chan JabberEvent) { + + defer func() { + if x := recover(); x != nil { + log.Printf("handleIncomingXMPPStanzas: run time panic: %v", x) + close(jabber_events) + } + }() + var incoming_stanza interface{} for incoming_stanza = range xmppin { switch stanza := incoming_stanza.(type) { -- 1.7.10.4