-
Notifications
You must be signed in to change notification settings - Fork 3
/
onlineconf.go
143 lines (133 loc) · 3.73 KB
/
onlineconf.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/url"
"strconv"
"time"
"github.com/rs/zerolog/log"
)
var ErrStatusNotOK = errors.New("response status code is not 200")
type NotificationsResponse struct {
Notifications []Notification `json:"notifications"`
LastID int `json:"lastID"`
}
func getNotifications(ctx context.Context, lastID, limit int) (*NotificationsResponse, error) {
uri, err := url.ParseRequestURI(config.GetString("/onlineconf/botapi/url", ""))
if err != nil {
return nil, err
}
uri.Path = "/botapi/notification/"
uri.RawQuery = url.Values{
"lastID": []string{strconv.Itoa(lastID)},
"limit": []string{strconv.Itoa(limit)},
"wait": []string{config.GetString("/onlineconf/botapi/wait", "60")},
}.Encode()
req, err := http.NewRequestWithContext(ctx, "GET", uri.String(), nil)
if err != nil {
return nil, err
}
req.SetBasicAuth(
config.GetString("/onlineconf/botapi/username", commandName),
config.GetString("/onlineconf/botapi/password", ""),
)
resp, err := http.DefaultClient.Do(req)
if err != nil {
if !errors.Is(err, context.Canceled) {
log.Ctx(ctx).Error().Str("method", req.Method).Str("url", req.URL.String()).Err(err).Msg("failed to get notifications")
}
return nil, err
}
if resp.StatusCode != 200 {
log.Ctx(ctx).Error().Str("method", req.Method).Str("url", req.URL.String()).Int("status", resp.StatusCode).Msg("failed to get notifications")
return nil, ErrStatusNotOK
}
var response NotificationsResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return nil, err
}
return &response, nil
}
func processNotifications(ctx context.Context, bot *myteamBot, limit int) (next bool, err error) {
waitCtx, cancel := context.WithCancel(log.Ctx(ctx).WithContext(context.Background()))
defer cancel()
go func() {
select {
case <-ctx.Done():
timer := time.NewTimer(time.Duration(config.GetInt("/stop-timeout", 15)) * time.Second)
select {
case <-timer.C:
log.Ctx(ctx).Warn().Msg("timeout, not all notifications was sent")
cancel()
case <-waitCtx.Done():
timer.Stop()
}
case <-waitCtx.Done():
}
}()
tx, err := db.BeginTx(waitCtx)
if err != nil {
return false, err
}
lastID, err := tx.GetLastID(ctx)
if err != nil {
return false, err
}
log.Ctx(ctx).Debug().Int("lastID", lastID).Msg("got lastID")
defer tx.Commit()
if lastID == 0 {
limit = 0
}
notifications, err := getNotifications(ctx, lastID, limit)
if err != nil {
return false, err
}
if notifications.LastID == lastID && len(notifications.Notifications) == 0 {
return false, nil
}
newLastID := 0
defer func() {
if newLastID != 0 {
if setErr := tx.SetLastID(waitCtx, newLastID); setErr == nil {
log.Ctx(ctx).Debug().Int("lastID", newLastID).Msg("new lastID")
} else if err == nil || errors.Is(err, context.Canceled) {
err = setErr
}
}
}()
for _, notification := range notifications.Notifications {
err := bot.notify(waitCtx, notification)
if err != nil {
return false, err
}
newLastID = notification.ID
select {
case <-ctx.Done():
return false, ctx.Err()
default:
}
}
newLastID = notifications.LastID
return len(notifications.Notifications) > 0, nil
}
func notificationsReceiver(ctx context.Context, bot *myteamBot) {
for {
timer := time.NewTimer(time.Duration(config.GetInt("/onlineconf/interval", 1)) * time.Second)
select {
case <-ctx.Done():
timer.Stop()
return
case <-timer.C:
for processNow := true; processNow; {
var err error
processNow, err = processNotifications(ctx, bot, config.GetInt("/onlineconf/batch-size", 100))
if err != nil && !errors.Is(err, context.Canceled) {
log.Ctx(ctx).Error().Err(err).Msg("failed to process notifications")
}
}
}
}
}