mirror of
https://github.com/google/alertmanager-irc-relay.git
synced 2024-12-02 16:09:25 +01:00
Stop sending messages while disconnected
Make sure the session is up before consuming alert messages. Also, split main run loop for readability. Signed-off-by: Luca Bigliardi <shammash@google.com>
This commit is contained in:
parent
82af7c1f69
commit
2471b866f1
84
irc.go
84
irc.go
@ -238,39 +238,7 @@ func (notifier *IRCNotifier) MaybeSendAlertMsg(alertMsg *AlertMsg) {
|
|||||||
ircSentMsgs.WithLabelValues(alertMsg.Channel).Inc()
|
ircSentMsgs.WithLabelValues(alertMsg.Channel).Inc()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (notifier *IRCNotifier) Run() {
|
func (notifier *IRCNotifier) ShutdownPhase() {
|
||||||
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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if notifier.Client.Connected() {
|
if notifier.Client.Connected() {
|
||||||
log.Printf("IRC client connected, quitting")
|
log.Printf("IRC client connected, quitting")
|
||||||
notifier.Client.Quit("see ya")
|
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()
|
||||||
|
}
|
||||||
|
@ -513,7 +513,12 @@ func TestSendAlertDisconnected(t *testing.T) {
|
|||||||
|
|
||||||
go notifier.Run()
|
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()
|
testStep.Done()
|
||||||
holdUserStep.Wait()
|
holdUserStep.Wait()
|
||||||
|
Loading…
Reference in New Issue
Block a user