-
Notifications
You must be signed in to change notification settings - Fork 40
/
webhook.go
77 lines (71 loc) · 1.8 KB
/
webhook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package job
import (
"bytes"
"context"
"encoding/json"
"time"
"net/http"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/runabol/tork"
)
const (
webhookDefaultMaxAttempts = 5
webhookDefaultTimeout = time.Second * 5
)
func Webhook(next HandlerFunc) HandlerFunc {
return func(ctx context.Context, et EventType, j *tork.Job) error {
if err := next(ctx, et, j); err != nil {
return err
}
if et != StateChange {
return nil
}
if len(j.Webhooks) == 0 {
return nil
}
summary, err := json.Marshal(tork.NewJobSummary(j))
if err != nil {
log.Err(err).Msgf(" error serializing")
return errors.Wrapf(err, "error serializing job summary")
}
for _, webhook := range j.Webhooks {
go func(w *tork.Webhook) {
callWebhook(w, summary)
}(webhook)
}
return nil
}
}
func callWebhook(webhook *tork.Webhook, body []byte) {
attempts := 1
client := http.Client{
Timeout: webhookDefaultTimeout,
}
for attempts <= webhookDefaultMaxAttempts {
log.Debug().Msgf("Calling webhook %s (attempt: %d)", webhook.URL, attempts)
req, err := http.NewRequest("POST", webhook.URL, bytes.NewReader(body))
if err != nil {
log.Error().Err(err).Msg("error creating webhook request")
return
}
if webhook.Headers != nil {
for name, val := range webhook.Headers {
req.Header.Set(name, val)
}
}
resp, err := client.Do(req)
if err != nil {
log.Error().Err(err).Msg("error submitting webhook")
return
}
if resp.StatusCode == http.StatusOK {
return
}
log.Warn().Msgf("webhook to %s failed with %d", webhook.URL, resp.StatusCode)
// sleep a little before retrying
time.Sleep(time.Second * time.Duration(attempts*2))
attempts = attempts + 1
}
log.Error().Msgf("failed to call webhook %s. max attempts: %d)", webhook.URL, webhookDefaultMaxAttempts)
}