-
Notifications
You must be signed in to change notification settings - Fork 0
/
outlet_failure.go
95 lines (77 loc) · 2.21 KB
/
outlet_failure.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
package quetaro
import (
"context"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/jackc/pgx/v5"
"github.com/pkg/errors"
"github.com/quetarohq/quetaro/awsutil"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"golang.org/x/sync/errgroup"
)
type OutletFailureOpts struct {
QueueName string
ConnConfig *pgx.ConnConfig
NAgents int
Interval time.Duration
ErrInterval time.Duration
MaxRecvNum int
AWSRegion string
AWSEndpointUrl string
}
func (opts *OutletFailureOpts) MarshalZerologObject(e *zerolog.Event) {
e.Str("queue", opts.QueueName).
Str("dsn", opts.ConnConfig.ConnString()).
Str("aws_region", opts.AWSRegion).
Str("aws_endpoint_url", opts.AWSEndpointUrl)
}
type OutletFailure struct {
*OutletFailureOpts
AwsCfg aws.Config
QueueUrl string
}
func NewOutletFailure(opts *OutletFailureOpts) (*OutletFailure, error) {
cfg, err := awsutil.LoadDefaultConfig(opts.AWSRegion)
if err != nil {
return nil, errors.Wrap(err, "failed to load AWS config")
}
// SQS client is created by each agent.
client := sqs.NewFromConfig(cfg, func(o *sqs.Options) {
if opts.AWSEndpointUrl != "" {
o.BaseEndpoint = aws.String(opts.AWSEndpointUrl)
}
})
// get the queue URL outside the agent.
output, err := client.GetQueueUrl(context.Background(), &sqs.GetQueueUrlInput{
QueueName: aws.String(opts.QueueName),
})
if err != nil {
return nil, errors.Wrap(err, "failed to get queue URL")
}
outletFailure := &OutletFailure{
OutletFailureOpts: opts,
AwsCfg: cfg,
QueueUrl: aws.ToString(output.QueueUrl),
}
return outletFailure, nil
}
func (outletFailure *OutletFailure) Start(ctx context.Context) error {
logger := log.Ctx(ctx).With().Str("queue_name", outletFailure.QueueName).Logger()
ctx = logger.WithContext(ctx)
logger.Info().Msg("start outlet-failure")
eg, ctx := errgroup.WithContext(ctx)
for i := 0; i < outletFailure.NAgents; i++ {
failureAgent := newOutletFailureAgent(outletFailure)
eg.Go(func() error {
return failureAgent.run(ctx)
})
}
err := eg.Wait()
logger.Info().Msg("shutdown outlet-failure")
if err != nil {
return errors.Wrap(err, "error in agent")
}
return nil
}