/// <summary>
/// Ensure sqs queue creation
/// </summary>
/// <param name="snsTopicArn">Subscribe to a sns topic</param>
/// <returns></returns>
public async Task EnsureQueueCreationAsync(string snsTopicArn = null)
{
// an integer representing seconds, from 60 (1 minute) to 1,209,600 (14 days). Default: 345,600 (4 days).
var messageRetentionPeriod = "1209600";
using (var client = new Amazon.SQS.AmazonSQSClient(accessKey, secretKey, region))
{
// dead letter queue
var req = new CreateQueueRequest("dead-" + queueName);
req.Attributes.Add(QueueAttributeName.MessageRetentionPeriod, messageRetentionPeriod);
if (isFIFO)
{
req.Attributes.Add(QueueAttributeName.FifoQueue, "true");
}
var r = await client.CreateQueueAsync(req);
string deadQueueArn = r.QueueUrl
.Replace("https://sqs.", "arn:aws:sqs:")
.Replace(".amazonaws.com", "")
.Replace("/", ":");
// queue
req = new CreateQueueRequest(queueName);
req.Attributes.Add(QueueAttributeName.MessageRetentionPeriod, messageRetentionPeriod);
req.Attributes.Add(QueueAttributeName.RedrivePolicy, "{\"maxReceiveCount\":3, \"deadLetterTargetArn\": \"" + deadQueueArn + "\"}");
if (isFIFO)
{
req.Attributes.Add(QueueAttributeName.FifoQueue, "true");
}
r = await client.CreateQueueAsync(req);
// subscribe to sns topic
if (!string.IsNullOrEmpty(snsTopicArn))
{
await notificationService.SubscribeSQS(snsTopicArn, client, r.QueueUrl);
}
}
}