diff --git a/irc.go b/irc.go index a13da13..48cdeae 100644 --- a/irc.go +++ b/irc.go @@ -238,39 +238,7 @@ func (notifier *IRCNotifier) MaybeSendAlertMsg(alertMsg *AlertMsg) { ircSentMsgs.WithLabelValues(alertMsg.Channel).Inc() } -func (notifier *IRCNotifier) Run() { - defer notifier.stopWg.Done() - - for notifier.ctx.Err() != context.Canceled { - if !notifier.Client.Connected() { - log.Printf("Connecting to IRC %s", notifier.Client.Config().Server) - if ok := notifier.BackoffCounter.DelayContext(notifier.ctx); !ok { - continue - } - if err := notifier.Client.Connect(); err != nil { - log.Printf("Could not connect to IRC: %s", err) - continue - } - log.Printf("Connected to IRC server, waiting to establish session") - } - - select { - case alertMsg := <-notifier.AlertMsgs: - notifier.MaybeSendAlertMsg(&alertMsg) - case <-notifier.sessionUpSignal: - notifier.sessionUp = true - notifier.MaybeIdentifyNick() - notifier.JoinChannels() - ircConnectedGauge.Set(1) - case <-notifier.sessionDownSignal: - notifier.sessionUp = false - notifier.CleanupChannels() - notifier.Client.Quit("see ya") - ircConnectedGauge.Set(0) - case <-notifier.ctx.Done(): - log.Printf("IRC routine asked to terminate") - } - } +func (notifier *IRCNotifier) ShutdownPhase() { if notifier.Client.Connected() { log.Printf("IRC client connected, quitting") notifier.Client.Quit("see ya") @@ -285,3 +253,53 @@ func (notifier *IRCNotifier) Run() { } } } + +func (notifier *IRCNotifier) ConnectedPhase() { + select { + case alertMsg := <-notifier.AlertMsgs: + notifier.MaybeSendAlertMsg(&alertMsg) + case <-notifier.sessionDownSignal: + notifier.sessionUp = false + notifier.CleanupChannels() + notifier.Client.Quit("see ya") + ircConnectedGauge.Set(0) + case <-notifier.ctx.Done(): + log.Printf("IRC routine asked to terminate") + } +} + +func (notifier *IRCNotifier) SetupPhase() { + if !notifier.Client.Connected() { + log.Printf("Connecting to IRC %s", notifier.Client.Config().Server) + if ok := notifier.BackoffCounter.DelayContext(notifier.ctx); !ok { + return + } + if err := notifier.Client.Connect(); err != nil { + log.Printf("Could not connect to IRC: %s", err) + return + } + log.Printf("Connected to IRC server, waiting to establish session") + } + select { + case <-notifier.sessionUpSignal: + notifier.sessionUp = true + notifier.MaybeIdentifyNick() + notifier.JoinChannels() + ircConnectedGauge.Set(1) + case <-notifier.ctx.Done(): + log.Printf("IRC routine asked to terminate") + } +} + +func (notifier *IRCNotifier) Run() { + defer notifier.stopWg.Done() + + for notifier.ctx.Err() != context.Canceled { + if !notifier.sessionUp { + notifier.SetupPhase() + } else { + notifier.ConnectedPhase() + } + } + notifier.ShutdownPhase() +} diff --git a/irc_test.go b/irc_test.go index d6cc9c2..106ca3e 100644 --- a/irc_test.go +++ b/irc_test.go @@ -513,7 +513,12 @@ func TestSendAlertDisconnected(t *testing.T) { go notifier.Run() - alertMsgs <- AlertMsg{Channel: testChannel, Alert: disconnectedTestMessage} + // Alert channels is not consumed while disconnected + select { + case alertMsgs <- AlertMsg{Channel: testChannel, Alert: disconnectedTestMessage}: + t.Error("Alert consumed while disconnected") + default: + } testStep.Done() holdUserStep.Wait()