Select Git revision
-
Daniel Naude authored
This is to fix sqs messages with a delay not working on local development environments.
Daniel Naude authoredThis is to fix sqs messages with a delay not working on local development environments.
sqs.go 5.29 KiB
package sqs
/*Package sqs provides a simple interface to send messages to AWS SQS*/
import (
"context"
"encoding/json"
"fmt"
"io"
"sync"
"time"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/utils"
"github.com/google/uuid"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/s3"
"github.com/aws/aws-sdk-go-v2/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"
"github.com/go-resty/resty/v2"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs"
)
var sqsClient *sqs.Client
const SQSMessageOnS3Key = "message-on-s3"
// Messenger sends an arbitrary message via SQS
type Messenger struct {
QueueName string
QueueURL string
Region string
S3Client *s3.ClientWithHelpers
S3BucketName string
MessageGroupID *string
DelaySeconds *int64
RequestIDHeaderKey string
}
// NewSQSClient constructs a Messenger which sends messages to an SQS queue
// awsRegion - region that the queue was created
// awsQueue - name of the queue
// Note: Calling code needs SQS IAM permissions
func NewSQSClient(awsRegion string) (*sqs.Client, error) {
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(awsRegion))
if err != nil {
return nil, err
}
sqsClient := sqs.NewFromConfig(cfg)
return sqsClient, err
}
// SendSQSMessage sends a message to the queue associated with the messenger
// headers - string message attributes of the SQS message (see AWS SQS documentation)
// body - body of the SQS message (see AWS SQS documentation)
func (m *Messenger) SendSQSMessage(headers map[string]string, body string, currentRequestID *string, sqsType string) (string, error) {
msgAttrs := make(map[string]types.MessageAttributeValue)
for key, val := range headers {
msgAttrs[key] = types.MessageAttributeValue{
DataType: aws.String("String"),
StringValue: aws.String(val),
}
}
// Add request ID