-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintake_invoke.go
80 lines (65 loc) · 1.81 KB
/
intake_invoke.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
package quetaro
import (
"context"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/lambda"
"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 IntakeInvokeOpts struct {
QueueName string
ConnConfig *pgx.ConnConfig
NAgents int
Interval time.Duration
ErrInterval time.Duration
AWSRegion string
AWSEndpointUrl string
}
func (opts *IntakeInvokeOpts) 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 IntakeInvoke struct {
*IntakeInvokeOpts
lambda awsutil.LambdaAPI
}
func NewIntakeInvoke(opts *IntakeInvokeOpts) (*IntakeInvoke, error) {
cfg, err := awsutil.LoadDefaultConfig(opts.AWSRegion)
if err != nil {
return nil, errors.Wrap(err, "failed to load AWS config")
}
intakeInvoke := &IntakeInvoke{
IntakeInvokeOpts: opts,
lambda: lambda.NewFromConfig(cfg, func(o *lambda.Options) {
if opts.AWSEndpointUrl != "" {
o.BaseEndpoint = aws.String(opts.AWSEndpointUrl)
}
}),
}
return intakeInvoke, nil
}
func (intakeInvoke *IntakeInvoke) Start(ctx context.Context) error {
logger := log.Ctx(ctx).With().Str("queue_name", intakeInvoke.QueueName).Logger()
ctx = logger.WithContext(ctx)
logger.Info().Msg("start intake-invoke")
eg, ctx := errgroup.WithContext(ctx)
for i := 0; i < intakeInvoke.NAgents; i++ {
invokeAgent := newIntakeInvokeAgent(intakeInvoke)
eg.Go(func() error {
return invokeAgent.run(ctx)
})
}
err := eg.Wait()
logger.Info().Msg("shutdown intake-invoke")
if err != nil {
return errors.Wrap(err, "error in agent")
}
return nil
}