public virtual async Task UpdateQueueAttributeAsync(SqsBasicConfiguration queueConfig) { if (QueueNeedsUpdating(queueConfig)) { var request = new SetQueueAttributesRequest { QueueUrl = Url, Attributes = new Dictionary <string, string> { { JustSayingConstants.ATTRIBUTE_RETENTION_PERIOD, queueConfig.MessageRetentionSeconds.ToString() }, { JustSayingConstants.ATTRIBUTE_VISIBILITY_TIMEOUT, queueConfig.VisibilityTimeoutSeconds.ToString() }, { JustSayingConstants.ATTRIBUTE_DELIVERY_DELAY, queueConfig.DeliveryDelaySeconds.ToString() } } }; var response = await Client.SetQueueAttributesAsync(request).ConfigureAwait(false); if (response.HttpStatusCode == HttpStatusCode.OK) { MessageRetentionPeriod = queueConfig.MessageRetentionSeconds; VisibilityTimeout = queueConfig.VisibilityTimeoutSeconds; DeliveryDelay = queueConfig.DeliveryDelaySeconds; } } }
protected virtual bool QueueNeedsUpdating(SqsBasicConfiguration queueConfig) { return(MessageRetentionPeriod != queueConfig.MessageRetention || VisibilityTimeout != queueConfig.VisibilityTimeout || DeliveryDelay != queueConfig.DeliveryDelay || QueueNeedsUpdatingBecauseOfEncryption(queueConfig)); }
public override async Task <bool> CreateAsync(SqsBasicConfiguration queueConfig, int attempt = 0, CancellationToken cancellationToken = default) { if (NeedErrorQueue(queueConfig)) { var exists = await ErrorQueue.ExistsAsync(cancellationToken).ConfigureAwait(false); if (!exists) { using (Logger.Time("Creating error queue {QueueName}", ErrorQueue.QueueName)) { await ErrorQueue.CreateAsync(new SqsBasicConfiguration { ErrorQueueRetentionPeriod = queueConfig.ErrorQueueRetentionPeriod, ErrorQueueOptOut = true }, cancellationToken : cancellationToken).ConfigureAwait(false); } } else { Logger.LogInformation("Error queue {QueueName} already exists, skipping", ErrorQueue.QueueName); } } using (Logger.Time("Creating queue {QueueName} attempt number {AttemptNumber}", queueConfig.QueueName, attempt)) { return(await base.CreateAsync(queueConfig, attempt, cancellationToken).ConfigureAwait(false)); } }
public async Task Then_The_Message_Retention_Period_Is_Updated() { // Arrange ILoggerFactory loggerFactory = OutputHelper.ToLoggerFactory(); IAwsClientFactory clientFactory = CreateClientFactory(); var client = clientFactory.GetSqsClient(Region); var queue = new ErrorQueue( Region, UniqueName, client, loggerFactory); var queueConfig = new SqsBasicConfiguration() { ErrorQueueRetentionPeriod = JustSayingConstants.MaximumRetentionPeriod, ErrorQueueOptOut = true, }; // Act await queue.CreateAsync(queueConfig); queueConfig.ErrorQueueRetentionPeriod = TimeSpan.FromSeconds(100); await queue.UpdateQueueAttributeAsync(queueConfig, CancellationToken.None); // Assert queue.MessageRetentionPeriod.ShouldBe(TimeSpan.FromSeconds(100)); }
public override async Task UpdateQueueAttributeAsync(SqsBasicConfiguration queueConfig) { if (!QueueNeedsUpdating(queueConfig)) { return; } var request = new SetQueueAttributesRequest { QueueUrl = Uri.AbsoluteUri, Attributes = new Dictionary <string, string> { { JustSayingConstants.AttributeRetentionPeriod, queueConfig.ErrorQueueRetentionPeriodSeconds.ToString(CultureInfo.InvariantCulture) } } }; var response = await Client.SetQueueAttributesAsync(request).ConfigureAwait(false); if (response.HttpStatusCode == HttpStatusCode.OK) { MessageRetentionPeriod = queueConfig.ErrorQueueRetentionPeriodSeconds; } }
public void EnsureQueueAndErrorQueueExistAndAllAttributesAreUpdated(SqsBasicConfiguration queueConfig) { if (!Exists()) { Create(queueConfig); } else { UpdateQueueAttribute(queueConfig); } //Create an error queue for existing queues if they don't already have one if (ErrorQueue != null) { var errorQueueConfig = new SqsReadConfiguration(SubscriptionType.ToTopic) { ErrorQueueRetentionPeriodSeconds = queueConfig.ErrorQueueRetentionPeriodSeconds, ErrorQueueOptOut = true }; if (!ErrorQueue.Exists()) { ErrorQueue.Create(errorQueueConfig); } else { ErrorQueue.UpdateQueueAttribute(errorQueueConfig); } } UpdateRedrivePolicy(new RedrivePolicy(queueConfig.RetryCountBeforeSendingToErrorQueue, ErrorQueue.Arn)); }
public override async Task UpdateQueueAttributeAsync(SqsBasicConfiguration queueConfig) { if (!QueueNeedsUpdating(queueConfig)) { return; } var request = new SetQueueAttributesRequest { QueueUrl = Url, Attributes = new Dictionary <string, string> { { JustSayingConstants.ATTRIBUTE_RETENTION_PERIOD, queueConfig.ErrorQueueRetentionPeriodSeconds.ToString() } } }; var response = await Client.SetQueueAttributesAsync(request); if (response.HttpStatusCode == HttpStatusCode.OK) { MessageRetentionPeriod = queueConfig.ErrorQueueRetentionPeriodSeconds; } }
public override async Task <bool> CreateAsync(SqsBasicConfiguration queueConfig, int attempt = 0) { if (NeedErrorQueue(queueConfig)) { var exists = await ErrorQueue.ExistsAsync().ConfigureAwait(false); if (!exists) { using (Logger.Time("Creating error queue {QueueName}", ErrorQueue.QueueName)) { await ErrorQueue.CreateAsync(new SqsBasicConfiguration { ErrorQueueRetentionPeriod = queueConfig.ErrorQueueRetentionPeriod, ErrorQueueOptOut = true }).ConfigureAwait(false); } } } using (Logger.Time("Creating queue {QueueName} attempt number {AttemptNumber}", queueConfig.QueueName, attempt)) { return(await base.CreateAsync(queueConfig, attempt).ConfigureAwait(false)); } }
protected override Dictionary <string, string> GetCreateQueueAttributes(SqsBasicConfiguration queueConfig) { return(new Dictionary <string, string> { { SQSConstants.ATTRIBUTE_MESSAGE_RETENTION_PERIOD, queueConfig.ErrorQueueRetentionPeriodSeconds.ToString(CultureInfo.InvariantCulture) }, { SQSConstants.ATTRIBUTE_VISIBILITY_TIMEOUT, JustSayingConstants.DEFAULT_VISIBILITY_TIMEOUT.ToString(CultureInfo.InvariantCulture) }, }); }
protected override Dictionary <string, string> GetCreateQueueAttributes(SqsBasicConfiguration queueConfig) { return(new Dictionary <string, string> { { SQSConstants.ATTRIBUTE_MESSAGE_RETENTION_PERIOD, queueConfig.ErrorQueueRetentionPeriod.AsSecondsString() }, { SQSConstants.ATTRIBUTE_VISIBILITY_TIMEOUT, JustSayingConstants.DefaultVisibilityTimeout.AsSecondsString() }, }); }
public override bool Create(SqsBasicConfiguration queueConfig, int attempt = 0) { if (!queueConfig.ErrorQueueOptOut) { throw new InvalidOperationException("Cannot create a dead letter queue for a dead letter queue."); } return(base.Create(queueConfig, attempt: attempt)); }
public virtual async Task <bool> CreateAsync(SqsBasicConfiguration queueConfig, int attempt = 0) { // If we're on a delete timeout, throw after 3 attempts. const int maxAttempts = 3; try { var queueResponse = await Client.CreateQueueAsync(QueueName).ConfigureAwait(false); if (!string.IsNullOrWhiteSpace(queueResponse?.QueueUrl)) { Uri = new Uri(queueResponse.QueueUrl); await Client.SetQueueAttributesAsync(queueResponse.QueueUrl, GetCreateQueueAttributes(queueConfig)).ConfigureAwait(false); await SetQueuePropertiesAsync().ConfigureAwait(false); Logger.LogInformation("Created queue '{QueueName}' with ARN '{Arn}'.", QueueName, Arn); return(true); } } catch (AmazonSQSException ex) { if (ex.ErrorCode == "AWS.SimpleQueueService.QueueDeletedRecently") { if (attempt >= (maxAttempts - 1)) { Logger.LogError( ex, "Error trying to create queue '{QueueName}'. Maximum retries of {MaxAttempts} exceeded for delay {Delay}.", QueueName, maxAttempts, CreateRetryDelay); throw; } // Ensure we wait for queue delete timeout to expire. Logger.LogInformation( "Waiting to create queue '{QueueName}' for {Delay}, due to AWS time restriction. Attempt number {AttemptCount} of {MaxAttempts}.", QueueName, CreateRetryDelay, attempt + 1, maxAttempts); await Task.Delay(CreateRetryDelay).ConfigureAwait(false); await CreateAsync(queueConfig, attempt + 1).ConfigureAwait(false); } else { // Throw all errors which are not delete timeout related. Logger.LogError(ex, "Error trying to create queue '{QueueName}'.", QueueName); throw; } } Logger.LogWarning("Failed to create queue '{QueueName}'.", QueueName); return(false); }
protected override Dictionary <string, string> GetCreateQueueAttributes(SqsBasicConfiguration queueConfig) { return(new Dictionary <string, string> { { SQSConstants.ATTRIBUTE_MESSAGE_RETENTION_PERIOD, queueConfig.MessageRetentionSeconds.ToString(CultureInfo.InvariantCulture) }, { SQSConstants.ATTRIBUTE_VISIBILITY_TIMEOUT, queueConfig.VisibilityTimeoutSeconds.ToString(CultureInfo.InvariantCulture) }, { SQSConstants.ATTRIBUTE_DELAY_SECONDS, queueConfig.DeliveryDelaySeconds.ToString(CultureInfo.InvariantCulture) }, { JustSayingConstants.ATTRIBUTE_REDRIVE_POLICY, new RedrivePolicy(_retryCountBeforeSendingToErrorQueue, ErrorQueue.Arn).ToString() } }); }
public override bool Create(SqsBasicConfiguration queueConfig, int attempt = 0) { if (!ErrorQueue.Exists()) { ErrorQueue.Create(new SqsBasicConfiguration { ErrorQueueRetentionPeriodSeconds = queueConfig.ErrorQueueRetentionPeriodSeconds, ErrorQueueOptOut = true }); } return(base.Create(queueConfig, attempt)); }
protected override async Task When() { var queueConfig = new SqsBasicConfiguration { DeliveryDelay = _oldDeliveryDelay }; await SystemUnderTest.CreateAsync(queueConfig); queueConfig.DeliveryDelay = _newDeliveryDelay; await SystemUnderTest.UpdateQueueAttributeAsync(queueConfig); }
protected override async Task When() { var queueConfig = new SqsBasicConfiguration { ErrorQueueRetentionPeriodSeconds = JustSayingConstants.MaximumRetentionPeriod, ErrorQueueOptOut = true }; await SystemUnderTest.CreateAsync(queueConfig); queueConfig.ErrorQueueRetentionPeriodSeconds = 100; await SystemUnderTest.UpdateQueueAttributeAsync(queueConfig); }
public override async Task <bool> CreateAsync(SqsBasicConfiguration queueConfig, int attempt = 0) { if (NeedErrorQueue(queueConfig)) { var exisits = await ErrorQueue.ExistsAsync(); if (!exisits) { await ErrorQueue.CreateAsync( new SqsBasicConfiguration { ErrorQueueRetentionPeriodSeconds = queueConfig.ErrorQueueRetentionPeriodSeconds, ErrorQueueOptOut = true }); } } return(await base.CreateAsync(queueConfig, attempt)); }
private bool QueueNeedsUpdatingBecauseOfEncryption(SqsBasicConfiguration queueConfig) { if (ServerSideEncryption == queueConfig.ServerSideEncryption) { return(false); } if (ServerSideEncryption != null && queueConfig.ServerSideEncryption != null) { return(ServerSideEncryption.KmsMasterKeyId != queueConfig.ServerSideEncryption.KmsMasterKeyId || ServerSideEncryption.KmsDataKeyReusePeriodSeconds != queueConfig.ServerSideEncryption.KmsDataKeyReusePeriodSeconds); } return(true); }
public virtual async Task <bool> CreateAsync(SqsBasicConfiguration queueConfig, int attempt = 0) { try { var result = await Client.CreateQueueAsync(new CreateQueueRequest { QueueName = QueueName, Attributes = GetCreateQueueAttributes(queueConfig) }); if (!string.IsNullOrWhiteSpace(result?.QueueUrl)) { Url = result.QueueUrl; await SetQueuePropertiesAsync(); _log.LogInformation($"Created Queue: {QueueName} on Arn: {Arn}"); return(true); } } catch (AmazonSQSException ex) { if (ex.ErrorCode == "AWS.SimpleQueueService.QueueDeletedRecently") { // Ensure we wait for queue delete timeout to expire. _log.LogInformation($"Waiting to create Queue due to AWS time restriction - Queue: {QueueName}, AttemptCount: {attempt + 1}"); await Task.Delay(60000); await CreateAsync(queueConfig, attempt + 1); } else { // Throw all errors which are not delete timeout related. _log.LogError(0, (Exception)ex, $"Create Queue error: {QueueName}"); throw; } // If we're on a delete timeout, throw after 2 attempts. if (attempt >= 2) { _log.LogError(0, (Exception)ex, $"Create Queue error, max retries exceeded for delay - Queue: {QueueName}"); throw; } } _log.LogInformation($"Failed to create Queue: {QueueName}"); return(false); }
public virtual bool Create(SqsBasicConfiguration queueConfig, int attempt = 0) { try { var result = Client.CreateQueue(new CreateQueueRequest { QueueName = QueueName, Attributes = GetCreateQueueAttributes(queueConfig) }); if (!string.IsNullOrWhiteSpace(result.QueueUrl)) { Url = result.QueueUrl; SetQueueProperties(); Log.Info(string.Format("Created Queue: {0} on Arn: {1}", QueueName, Arn)); return(true); } } catch (AmazonSQSException ex) { if (ex.ErrorCode == "AWS.SimpleQueueService.QueueDeletedRecently") { // Ensure we wait for queue delete timeout to expire. Log.Info(string.Format("Waiting to create Queue due to AWS time restriction - Queue: {0}, AttemptCount: {1}", QueueName, attempt + 1)); Thread.Sleep(60000); Create(queueConfig, attempt: attempt++); } else { // Throw all errors which are not delete timeout related. Log.Error(ex, string.Format("Create Queue error: {0}", QueueName)); throw; } // If we're on a delete timeout, throw after 2 attempts. if (attempt >= 2) { Log.Error(ex, string.Format("Create Queue error, max retries exceeded for delay - Queue: {0}", QueueName)); throw; } } Log.Info(string.Format("Failed to create Queue: {0}", QueueName)); return(false); }
protected internal override void UpdateQueueAttribute(SqsBasicConfiguration queueConfig) { if (QueueNeedsUpdating(queueConfig)) { var response = Client.SetQueueAttributes( new SetQueueAttributesRequest { QueueUrl = Url, Attributes = new Dictionary <string, string> { { JustSayingConstants.ATTRIBUTE_RETENTION_PERIOD, queueConfig.ErrorQueueRetentionPeriodSeconds.ToString() }, } }); if (response.HttpStatusCode == HttpStatusCode.OK) { MessageRetentionPeriod = queueConfig.ErrorQueueRetentionPeriodSeconds; } } }
protected internal void UpdateQueueAttribute(SqsBasicConfiguration queueConfig) { if (QueueNeedsUpdating(queueConfig)) { var response = Client.SetQueueAttributes( new SetQueueAttributesRequest { QueueUrl = Url, Attributes = new Dictionary <string, string> { { JustSayingConstants.ATTRIBUTE_RETENTION_PERIOD, queueConfig.MessageRetentionSeconds.ToString() }, { JustSayingConstants.ATTRIBUTE_VISIBILITY_TIMEOUT, queueConfig.VisibilityTimeoutSeconds.ToString() }, } }); if (response.HttpStatusCode == HttpStatusCode.OK) { MessageRetentionPeriod = queueConfig.MessageRetentionSeconds; VisibilityTimeout = queueConfig.VisibilityTimeoutSeconds; } } }
public virtual async Task UpdateQueueAttributeAsync(SqsBasicConfiguration queueConfig) { if (QueueNeedsUpdating(queueConfig)) { var attributes = new Dictionary <string, string> { { JustSayingConstants.AttributeRetentionPeriod, queueConfig.MessageRetention.AsSecondsString() }, { JustSayingConstants.AttributeVisibilityTimeout, queueConfig.VisibilityTimeout.AsSecondsString() }, { JustSayingConstants.AttributeDeliveryDelay, queueConfig.DeliveryDelay.AsSecondsString() } }; if (queueConfig.ServerSideEncryption != null) { attributes.Add(JustSayingConstants.AttributeEncryptionKeyId, queueConfig.ServerSideEncryption.KmsMasterKeyId); attributes.Add(JustSayingConstants.AttributeEncryptionKeyReusePeriodSecondId, queueConfig.ServerSideEncryption.KmsDataKeyReusePeriodSeconds); } if (queueConfig.ServerSideEncryption == null) { attributes.Add(JustSayingConstants.AttributeEncryptionKeyId, string.Empty); } var request = new SetQueueAttributesRequest { QueueUrl = Uri.AbsoluteUri, Attributes = attributes }; var response = await Client.SetQueueAttributesAsync(request).ConfigureAwait(false); if (response.HttpStatusCode == HttpStatusCode.OK) { MessageRetentionPeriod = queueConfig.MessageRetention; VisibilityTimeout = queueConfig.VisibilityTimeout; DeliveryDelay = queueConfig.DeliveryDelay; ServerSideEncryption = queueConfig.ServerSideEncryption; } } }
protected override Dictionary <string, string> GetCreateQueueAttributes(SqsBasicConfiguration queueConfig) { var policy = new Dictionary <string, string> { { SQSConstants.ATTRIBUTE_MESSAGE_RETENTION_PERIOD, queueConfig.MessageRetention.AsSecondsString() }, { SQSConstants.ATTRIBUTE_VISIBILITY_TIMEOUT, queueConfig.VisibilityTimeout.AsSecondsString() }, { SQSConstants.ATTRIBUTE_DELAY_SECONDS, queueConfig.DeliveryDelay.AsSecondsString() }, }; if (NeedErrorQueue(queueConfig)) { policy.Add(JustSayingConstants.AttributeRedrivePolicy, new RedrivePolicy(_retryCountBeforeSendingToErrorQueue, ErrorQueue.Arn).ToString()); } if (queueConfig.ServerSideEncryption != null) { policy.Add(JustSayingConstants.AttributeEncryptionKeyId, queueConfig.ServerSideEncryption.KmsMasterKeyId); policy.Add(JustSayingConstants.AttributeEncryptionKeyReusePeriodSecondId, queueConfig.ServerSideEncryption.KmsDataKeyReusePeriodSeconds); } return(policy); }
public virtual async Task UpdateQueueAttributeAsync(SqsBasicConfiguration queueConfig) { if (QueueNeedsUpdating(queueConfig)) { var attributes = new Dictionary <string, string> { { JustSayingConstants.ATTRIBUTE_RETENTION_PERIOD, queueConfig.MessageRetentionSeconds.ToString() }, { JustSayingConstants.ATTRIBUTE_VISIBILITY_TIMEOUT, queueConfig.VisibilityTimeoutSeconds.ToString() }, { JustSayingConstants.ATTRIBUTE_DELIVERY_DELAY, queueConfig.DeliveryDelaySeconds.ToString() } }; if (queueConfig.ServerSideEncryption != null) { attributes.Add(JustSayingConstants.ATTRIBUTE_ENCRYPTION_KEY_ID, queueConfig.ServerSideEncryption.KmsMasterKeyId); attributes.Add(JustSayingConstants.ATTRIBUTE_ENCRYPTION_KEY_REUSE_PERIOD_SECOND_ID, queueConfig.ServerSideEncryption.KmsDataKeyReusePeriodSeconds); } if (queueConfig.ServerSideEncryption == null) { attributes.Add(JustSayingConstants.ATTRIBUTE_ENCRYPTION_KEY_ID, string.Empty); } var request = new SetQueueAttributesRequest { QueueUrl = Url, Attributes = attributes }; var response = await Client.SetQueueAttributesAsync(request).ConfigureAwait(false); if (response.HttpStatusCode == HttpStatusCode.OK) { MessageRetentionPeriod = queueConfig.MessageRetentionSeconds; VisibilityTimeout = queueConfig.VisibilityTimeoutSeconds; DeliveryDelay = queueConfig.DeliveryDelaySeconds; ServerSideEncryption = queueConfig.ServerSideEncryption; } } }
public async Task EnsureQueueAndErrorQueueExistAndAllAttributesAreUpdatedAsync(SqsBasicConfiguration queueConfig) { var exists = await ExistsAsync(); if (!exists) { await CreateAsync(queueConfig); } else { await UpdateQueueAttributeAsync(queueConfig); } //Create an error queue for existing queues if they don't already have one if (ErrorQueue != null && NeedErrorQueue(queueConfig)) { var errorQueueConfig = new SqsReadConfiguration(SubscriptionType.ToTopic) { ErrorQueueRetentionPeriodSeconds = queueConfig.ErrorQueueRetentionPeriodSeconds, ErrorQueueOptOut = true }; var errorQueueExists = await ErrorQueue.ExistsAsync(); if (!errorQueueExists) { await ErrorQueue.CreateAsync(errorQueueConfig); } else { await ErrorQueue.UpdateQueueAttributeAsync(errorQueueConfig); } await UpdateRedrivePolicyAsync( new RedrivePolicy(queueConfig.RetryCountBeforeSendingToErrorQueue, ErrorQueue.Arn)); } }
private bool QueueNeedsUpdating(SqsBasicConfiguration queueConfig) { return(MessageRetentionPeriod != queueConfig.MessageRetentionSeconds || VisibilityTimeout != queueConfig.VisibilityTimeoutSeconds); }
protected override bool QueueNeedsUpdating(SqsBasicConfiguration queueConfig) { return(MessageRetentionPeriod != queueConfig.ErrorQueueRetentionPeriodSeconds); }
public bool Create(SqsBasicConfiguration queueConfig, int attempt = 0) { return(CreateAsync(queueConfig, attempt) .GetAwaiter().GetResult()); }
protected abstract Dictionary <string, string> GetCreateQueueAttributes(SqsBasicConfiguration queueConfig);