Skip to content

Commit dbe65b8

Browse files
committed
Resend failed Slack notifications
1 parent 700fde8 commit dbe65b8

File tree

5 files changed

+51
-21
lines changed

5 files changed

+51
-21
lines changed

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
module github.com/CHTJonas/uptime-mon
22

3-
go 1.17
3+
go 1.23
4+
5+
toolchain go1.24.0
46

57
require (
8+
github.com/cenkalti/backoff/v5 v5.0.2
69
github.com/slack-go/slack v0.12.1
710
github.com/spf13/viper v1.9.0
811
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmV
5151
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
5252
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
5353
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
54+
github.com/cenkalti/backoff/v5 v5.0.2 h1:rIfFVxEf1QsI7E1ZHfp/B4DF/6QBAUhmgkxc0H7Zss8=
55+
github.com/cenkalti/backoff/v5 v5.0.2/go.mod h1:rkhZdG3JZukswDf7f0cwqPNk4K0sa+F97BxZthm/crw=
5456
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
5557
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
5658
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=

main.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,19 @@ func testLoop() {
5353
if strings.Contains(err.Error(), "Client.Timeout") {
5454
err = fmt.Errorf("response time was greater than %d milliseconds", t.MaxResponseTime)
5555
}
56-
errStr := fmt.Sprintf("Test failed: %s: %s", t.Name, err)
57-
debugPrintLn(errStr)
56+
debugPrintf("Test failed: %s: %s\n", t.Name, err)
5857
if t.ShouldNotify() && !t.notified {
59-
err := notify(errStr)
60-
if err != nil {
61-
fmt.Println("error sending test failure Slack notification:", err)
62-
} else {
63-
debugPrintLn("successfully sent test failure Slack notification")
64-
t.notified = true
65-
}
58+
msg := getMessage("failed", t.Name, err)
59+
go notifyRetry(msg)
60+
t.notified = true
6661
}
6762
} else {
6863
debugPrintLn("Test successful:", t.Name)
6964
if t.notified {
70-
err := notifyf("Test recovered: %s", t.Name)
71-
if err != nil {
72-
fmt.Println("error sending test recovery Slack notification:", err)
73-
} else {
74-
debugPrintLn("successfully sent test recovery Slack notification")
75-
}
65+
msg := getMessage("recovered", t.Name, nil)
66+
go notifyRetry(msg)
67+
t.notified = false
7668
}
77-
t.notified = false
7869
}
7970
}(c.tests[i])
8071
time.Sleep(duration)

slack.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,45 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
6+
"time"
57

8+
"github.com/cenkalti/backoff/v5"
69
"github.com/slack-go/slack"
710
)
811

12+
func getMessage(verb, name string, err error) string {
13+
timeStr := time.Now().UTC().Format("2006-01-02 15:04:05")
14+
if err == nil {
15+
return fmt.Sprintf("Test %s @ %s\n%s", verb, timeStr, name)
16+
}
17+
return fmt.Sprintf("Test %s @ %s\n%s: %s", verb, timeStr, name, err)
18+
}
19+
20+
func notifyRetry(msg string) error {
21+
operation := func() (string, error) {
22+
err := notify(msg)
23+
if err != nil {
24+
fmt.Println("error sending Slack notification:", err)
25+
return "", err
26+
}
27+
debugPrintLn("successfully sent Slack notification")
28+
return "", nil
29+
}
30+
strat := backoff.WithBackOff(&backoff.ExponentialBackOff{
31+
InitialInterval: 3 * time.Second,
32+
RandomizationFactor: 0.5,
33+
Multiplier: 1.5,
34+
MaxInterval: 48 * time.Hour,
35+
})
36+
_, err := backoff.Retry(context.TODO(), operation, strat)
37+
return err
38+
}
39+
940
func notify(msg string) error {
1041
hookURL := getConfig().slackWebhook
1142
return slack.PostWebhook(hookURL, &slack.WebhookMessage{
1243
Text: msg,
1344
})
1445
}
15-
16-
func notifyf(format string, a ...interface{}) error {
17-
return notify(fmt.Sprintf(format, a...))
18-
}

utils.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,10 @@ func debugPrintLn(a ...any) (n int, err error) {
1717
}
1818
return 0, nil
1919
}
20+
21+
func debugPrintf(format string, a ...any) (n int, err error) {
22+
if version == "dev" {
23+
fmt.Printf(format, a...)
24+
}
25+
return 0, nil
26+
}

0 commit comments

Comments
 (0)