00e7dca3c7
Move queue to config * movequeue * passtests
55 lines
1.5 KiB
Go
55 lines
1.5 KiB
Go
package queue
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"queryorchestration/internal/serviceconfig/aws"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/config"
|
|
"github.com/aws/aws-sdk-go-v2/service/sqs"
|
|
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
|
|
)
|
|
|
|
type QueueConfig struct {
|
|
aws.AWSConfig
|
|
EnableOtel bool `env:"ENABLE_OTEL" envDefault:"false"`
|
|
AWSEndpointUrlSQS string `env:"AWS_ENDPOINT_URL_SQS"`
|
|
QueueClient SQSClient
|
|
}
|
|
|
|
type ConfigProvider interface {
|
|
aws.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
|
|
}
|
|
|
|
func (c *QueueConfig) GetQueueClient() SQSClient {
|
|
return c.QueueClient
|
|
}
|
|
|
|
func (c *QueueConfig) PingQueueByURL(ctx context.Context, url string) error {
|
|
_, err := c.QueueClient.GetQueueAttributes(ctx, &sqs.GetQueueAttributesInput{
|
|
QueueUrl: &url,
|
|
AttributeNames: []types.QueueAttributeName{types.QueueAttributeNameApproximateNumberOfMessages},
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("unable to load ping queue by url (%s): %v", url, err)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (c *QueueConfig) SetQueueClient(ctx context.Context) error {
|
|
qcfg, err := config.LoadDefaultConfig(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to load SDK config: %v", err)
|
|
}
|
|
|
|
c.QueueClient = sqs.NewFromConfig(qcfg)
|
|
|
|
return nil
|
|
}
|