-
Notifications
You must be signed in to change notification settings - Fork 0
/
intake_pull_agent.go
193 lines (152 loc) · 4.72 KB
/
intake_pull_agent.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package quetaro
import (
"context"
"encoding/json"
"strings"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
"github.com/jackc/pgx/v5"
"github.com/pkg/errors"
"github.com/quetarohq/quetaro/awsutil"
"github.com/quetarohq/quetaro/ctxutil"
"github.com/rs/zerolog/log"
)
const (
FuncAttreName = "FunctionName"
)
type IntakePullAgent struct {
*IntakePull
sqs awsutil.SQSAPI
}
func newIntakePullAgent(intakePull *IntakePull) *IntakePullAgent {
sqsClient := sqs.NewFromConfig(intakePull.AwsCfg, func(o *sqs.Options) {
if intakePull.AWSEndpointUrl != "" {
o.BaseEndpoint = aws.String(intakePull.AWSEndpointUrl)
}
})
agent := &IntakePullAgent{
IntakePull: intakePull,
sqs: sqsClient,
}
return agent
}
func (agent *IntakePullAgent) run(ctx context.Context) error {
logger := log.Ctx(ctx)
logger.Info().Msg("start agent")
err := loopForAgent(ctx, agent.ConnConfig, agent.Interval, agent.ErrInterval, func(ctx context.Context, conn *pgx.Conn) error {
err := agent.pull(ctx, conn)
if err != nil {
return errors.Wrap(err, "failed to pull messages")
}
return nil
})
if err != nil {
return errors.Wrap(err, "cannot continue to run agent")
}
return nil
}
func (agent *IntakePullAgent) pull(ctx context.Context, conn *pgx.Conn) error {
msgs, err := awsutil.ReceiveMessages(ctx, agent.sqs, agent.QueueUrl, agent.MaxRecvNum, FuncAttreName)
if err != nil {
return errors.Wrap(err, "failed to receive messages")
}
if len(msgs) == 0 {
return nil
}
// do not cancel the following processes
ctx = ctxutil.WithoutCancel(ctx)
err = pgx.BeginFunc(ctx, conn, func(tx pgx.Tx) error {
logger := log.Ctx(ctx)
msgsToDel := []types.Message{}
for _, m := range msgs {
msgId := aws.ToString(m.MessageId)
logger := logger.With().Str("id", msgId).Logger()
var alreadyExists bool
sql, args := sq.Select("*").Prefix("select exists (").From("jobs").
Where(sq.Eq{"id": msgId}).Limit(1).Suffix(")").MustSql()
err := tx.QueryRow(ctx, sql, args...).Scan(&alreadyExists)
if err != nil {
ewj := &ErrWithJob{cause: err, Id: msgId}
return errors.Wrap(ewj, "failed to fetch a job from DB")
}
if alreadyExists {
logger.Warn().Msg("message already queued")
msgsToDel = append(msgsToDel, m)
continue
}
var funcName string
var invalidCause []string
if funcAttr, ok := m.MessageAttributes[FuncAttreName]; ok {
funcName = aws.ToString(funcAttr.StringValue)
} else {
invalidCause = append(invalidCause, "no function name")
}
var body string
rawBody := aws.ToString(m.Body)
if json.Valid([]byte(rawBody)) {
body = rawBody
logger = logger.With().Int("body_size", len(body)).Logger()
} else {
invalidCause = append(invalidCause, "invalid JSON")
}
isValid := funcName != "" && body != ""
now := time.Now()
valByCol := sq.Eq{
"id": msgId,
"queue_name": agent.QueueName,
"function_name": funcName,
"payload": body,
"status": JobStatusPending,
"invoke_after": now,
"error_count": 0,
"created_at": now,
"updated_at": now,
}
if body == "" {
valByCol["payload"] = "{}"
}
if !isValid {
valByCol["status"] = JobStatusInvalid
valByCol["last_error"] = strings.Join(invalidCause, ",")
}
sql, args = sq.Insert("jobs").SetMap(valByCol).MustSql()
_, err = tx.Exec(ctx, sql, args...)
if err != nil {
ewj := &ErrWithJob{cause: err, Id: msgId, Name: funcName}
return errors.Wrap(ewj, "failed to insert the message to DB")
}
if isValid {
logger = logger.With().Str("function_name", funcName).Logger()
logger.Info().Msg("message queued")
} else {
logger.Error().Strs("invalid_cause", invalidCause).Msg("invalid message received")
}
msgsToDel = append(msgsToDel, m)
}
if len(msgsToDel) > 0 {
err, failed := awsutil.DeleteMessages(ctx, agent.sqs, agent.QueueUrl, msgsToDel)
if err != nil {
for _, e := range failed {
logger.Error().EmbedObject(FromBatchResultErrorEntry(e)).Msg("failed to delete the message from SQS")
errMsgId := aws.ToString(e.Id)
sql, args := sq.Delete("jobs").Where(sq.Eq{"id": errMsgId}).MustSql()
_, err = tx.Exec(ctx, sql, args...)
if err != nil {
ewj := &ErrWithJob{cause: err, Id: errMsgId}
return errors.Wrap(ewj, "failed to delete the job on DeleteMessageBatch failure")
}
}
// continue if DeleteMessageBatch fails
logger.Error().Err(err).Msgf("failed to delete messages from SQS")
}
}
return nil
}) // end of pgx.BeginFunc()
if err != nil {
return errors.Wrap(err, "error in agant transactions")
}
return nil
}