Files
query-orchestration/internal/serviceconfig/queue/config.go
T
Michael McGuinness 81e7223560 Merged in feature/textextraction (pull request #110)
Text Extraction

* bases

* go

* splitting

* structure

* movetoasync

* movetoasync

* settinguptrigger

* reorder

* storevent

* standardisepollingvalidation

* unittests

* fixlint

* fixlint

* awscfg

* generatesample

* followthrough

* tests

* clena

* store

* externalidcleanup

* clientid

* local

* baseunittests

* putobjecttests

* tests
2025-04-02 18:50:03 +00:00

82 lines
2.0 KiB
Go

package queue
import (
"context"
"fmt"
"log/slog"
"time"
awsc "queryorchestration/internal/serviceconfig/aws"
"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"
)
type QueueConfig struct {
AWSEndpointUrlSQS string `env:"AWS_ENDPOINT_URL_SQS"`
QueueClient SQSClient
}
type ConfigProvider interface {
awsc.ConfigProvider
SetQueueClient(context.Context) error
GetQueueClient() SQSClient
PingQueueByURL(context.Context, string) error
SendToQueue(context.Context, *SendParams) error
ReceiveFromQueue(context.Context, *ReceiveParams) (*sqs.ReceiveMessageOutput, error)
DeleteFromQueue(context.Context, *DeleteParams) error
SetSQSEndpoint(string)
GetSQSEndpoint() string
}
func (c *QueueConfig) GetQueueClient() SQSClient {
return c.QueueClient
}
func (c *QueueConfig) GetSQSEndpoint() string {
return c.AWSEndpointUrlSQS
}
func (c *QueueConfig) PingQueueByURL(ctx context.Context, url string) error {
timeout := time.After(30 * time.Second)
tick := time.NewTicker(2 * time.Second)
defer tick.Stop()
input := &sqs.GetQueueAttributesInput{
QueueUrl: aws.String(url),
AttributeNames: []types.QueueAttributeName{types.QueueAttributeNameApproximateNumberOfMessages},
}
for {
select {
case <-timeout:
return fmt.Errorf("unable to ping queue by url: %s", url)
case <-tick.C:
r, err := c.QueueClient.GetQueueAttributes(ctx, input)
if err == nil {
slog.Info("Successful queue ping", "url", url, "approx_msgs", r.Attributes[string(types.QueueAttributeNameApproximateNumberOfMessages)])
return nil
}
slog.Debug("Attempted queue ping", "url", url, "address", c.GetSQSEndpoint())
case <-ctx.Done():
return ctx.Err()
}
}
}
func (c *QueueConfig) SetQueueClient(ctx context.Context) error {
cfg, err := awsc.GetAWSConfig(ctx)
if err != nil {
return err
}
c.QueueClient = sqs.NewFromConfig(cfg)
return nil
}
func (c *QueueConfig) SetSQSEndpoint(e string) {
c.AWSEndpointUrlSQS = e
}