-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpurge.go
118 lines (94 loc) · 3.26 KB
/
purge.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
package courier
import (
"context"
"fmt"
"github.com/go-chi/chi"
"github.com/sirupsen/logrus"
"net/http"
"strings"
)
type PurgeHandler struct {
server Server
}
func NewPurgeHandler(s Server) *PurgeHandler {
p := new(PurgeHandler)
p.server = s
return p
}
func (p *PurgeHandler) PurgeChannel(w http.ResponseWriter, r *http.Request) {
uuid, err := NewChannelUUID(chi.URLParam(r, "uuid"))
if err != nil || len(uuid.String()) == 0 {
logrus.Error("Invalid channel ID provided")
WriteDataResponse(context.Background(), w, http.StatusBadRequest, "invalid channel ID", nil)
return
}
channelType := ChannelType(strings.ToUpper(chi.URLParam(r, "type")))
channel, err := p.server.Backend().GetChannel(r.Context(), channelType, uuid)
if channel == nil || err != nil {
logrus.Error("Could not find channel")
WriteDataResponse(context.Background(), w, http.StatusBadRequest, "could not find channel", nil)
return
}
logrus.WithField("channel_id", channel.UUID()).Info("Purging channel")
queues, err := p.server.Backend().GetCurrentQueuesForChannel(context.Background(), uuid)
if err != nil {
logrus.Error(err)
WriteDataResponse(context.Background(), w, http.StatusInternalServerError, "Error while fetching queues", nil)
return
} else if len(queues) == 0 {
logrus.Info("No queues found")
} else {
purgeQueues, err := p.server.Backend().PrepareQueuesForPurge(context.Background(), queues)
if err != nil {
logrus.Error(err)
WriteDataResponse(context.Background(), w, http.StatusInternalServerError,
"Error while preparing queues for purge", nil)
return
}
go p.PurgeRoutine(purgeQueues)
}
// Even if courier has no queues, always call the channel's purge handler if it is available.
ch := GetHandler(channelType)
ch.PurgeOutgoing(r.Context(), channel)
WriteDataResponse(context.Background(), w, http.StatusOK, "Ok", nil)
}
func (p PurgeHandler) ResumePurges() {
purgeQueues, err := p.server.Backend().GetActivePurges(context.Background())
if err == nil && len(purgeQueues) == 0 {
logrus.Debug("No purges to resume")
} else if err != nil {
logrus.WithError(err).Error("Could not resume purges")
} else {
logrus.WithField("queues", purgeQueues).Debug("Resuming purge")
go p.PurgeRoutine(purgeQueues)
}
}
func (p *PurgeHandler) PurgeRoutine(queueKeys []string) {
rc := p.server.Backend().RedisPool().Get()
defer rc.Close()
// Iterate throuhg each queue for the channel, then iterate messages
for _, v := range queueKeys {
logrus.WithField("queue", v).Info("Purging queue")
hasMsg := true
// Iterate through messages until we're out of them.
for hasMsg == true {
msgs, _ := p.server.Backend().PopMsgs(context.Background(), v, 10)
if len(msgs) == 0 {
logrus.Debug("out of messages")
hasMsg = false
break
}
for _, msg := range msgs {
status := p.server.Backend().NewMsgStatusForID(msg.Channel(), msg.ID(), MsgFailed)
status.AddLog(NewChannelLogFromError("Queue Purge", msg.Channel(), msg.ID(), 0,
fmt.Errorf("failing message due to purge")))
err := p.server.Backend().WriteMsgStatus(context.Background(), status)
if err != nil {
logrus.WithError(err).Info("error writing msg status")
} else {
logrus.WithField("msg", msg.ID()).Info("Failing message due to queue purge")
}
}
}
}
}