Beispiel #1
0
        /// <inheritdoc />
        public async Task <DateTime> RenewAsync(WorkItem item, CancellationToken cancellationToken)
        {
            var lockExpiration = (item.VisibleOn ?? DateTime.UtcNow).AddSeconds(_visibilityTimeout);
            await _client.ChangeMessageVisibilityAsync(_queueUrl, item.Token, _visibilityTimeout, cancellationToken);

            return(lockExpiration);
        }
        /// <summary>
        /// Rejects the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="requeue">if set to <c>true</c> [requeue].</param>
        public void Reject(Message message, bool requeue)
        {
            if (!message.Header.Bag.ContainsKey("ReceiptHandle"))
            {
                return;
            }

            var receiptHandle = message.Header.Bag["ReceiptHandle"].ToString();

            try
            {
                _logger.Value.InfoFormat("SqsMessageConsumer: Rejecting the message {0} with receipt handle {1} on the queue {2} with requeue paramter {3}", message.Id, receiptHandle, _queueUrl, requeue);

                using (var client = new AmazonSQSClient(_credentials))
                {
                    if (requeue)
                    {
                        client.ChangeMessageVisibilityAsync(new ChangeMessageVisibilityRequest(_queueUrl, receiptHandle, 0)).Wait();
                    }
                    else
                    {
                        client.DeleteMessageAsync(_queueUrl, receiptHandle).Wait();
                    }
                }

                _logger.Value.InfoFormat("SqsMessageConsumer: Message {0} with receipt handle {1} on the queue {2} with requeue paramter {3} has been rejected", message.Id, receiptHandle, _queueUrl, requeue);
            }
            catch (Exception exception)
            {
                _logger.Value.ErrorException("SqsMessageConsumer: Error during rejecting the message {0} with receipt handle {1} on the queue {2}", exception, message.Id, receiptHandle, _queueUrl);
                throw;
            }
        }
        /// <summary>
        /// Requeues the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="delayMilliseconds">Number of milliseconds to delay delivery of the message.</param>
        /// <returns>True if the message was requeued successfully</returns>
        public bool Requeue(Message message, int delayMilliseconds)
        {
            if (!message.Header.Bag.ContainsKey("ReceiptHandle"))
            {
                return(false);
            }

            var receiptHandle = message.Header.Bag["ReceiptHandle"].ToString();

            try
            {
                s_logger.LogInformation("SqsMessageConsumer: re-queueing the message {Id}", message.Id);

                using (var client = new AmazonSQSClient(_awsConnection.Credentials, _awsConnection.Region))
                {
                    var urlResponse = client.GetQueueUrlAsync(_queueName).Result;
                    client.ChangeMessageVisibilityAsync(new ChangeMessageVisibilityRequest(urlResponse.QueueUrl, receiptHandle, 0)).Wait();
                }

                s_logger.LogInformation("SqsMessageConsumer: re-queued the message {Id}", message.Id);

                return(true);
            }
            catch (Exception exception)
            {
                s_logger.LogError(exception, "SqsMessageConsumer: Error during re-queueing the message {Id} with receipt handle {ReceiptHandle} on the queue {ChannelName}", message.Id, receiptHandle, _queueName);
                return(false);
            }
        }
Beispiel #4
0
        public async Task <JobDetail> GetJobFromSQS(string sqsQueueUrl)
        {
            //Build message request, only get one message
            var receiveMessageRequest = new ReceiveMessageRequest()
            {
                QueueUrl            = sqsQueueUrl,
                MaxNumberOfMessages = 1
            };
            var receiveMessageResponse = await sqsClient.ReceiveMessageAsync(receiveMessageRequest);

            if (receiveMessageResponse.Messages.Count > 0)
            {
                System.Console.WriteLine($"Info: GetSQS, MessageID: {receiveMessageResponse.Messages.FirstOrDefault().MessageId}");
                //Change visibility of message so that nothing else picks up the same job
                await sqsClient.ChangeMessageVisibilityAsync(sqsQueueUrl, receiveMessageResponse.Messages.FirstOrDefault().ReceiptHandle, 40000);

                Message msg       = receiveMessageResponse.Messages.FirstOrDefault();
                var     jobDetail = JsonConvert.DeserializeObject <JobDetail>(msg.Body);

                jobDetail.MessageId     = msg.MessageId;
                jobDetail.ReceiptHandle = msg.ReceiptHandle;

                return(jobDetail);
            }
            else
            {
                return(null);
            }
        }
Beispiel #5
0
        public async Task ChangeMessageVisibility()
        {
            // Create service client using the SDK's default logic for determining AWS credentials and region to use.
            // For information configuring service clients checkout the .NET developer guide: https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config.html
            AmazonSQSClient client   = new AmazonSQSClient();
            string          queueUrl = "SQS_QUEUE_URL";

            var receiveMessageRequest = new ReceiveMessageRequest
            {
                AttributeNames        = { "SentTimestamp" },
                MaxNumberOfMessages   = 1,
                MessageAttributeNames = { "All" },
                QueueUrl = queueUrl
            };

            var response = await client.ReceiveMessageAsync(receiveMessageRequest);

            var changeMessageVisibilityRequest = new ChangeMessageVisibilityRequest
            {
                QueueUrl          = queueUrl,
                ReceiptHandle     = response.Messages[0].ReceiptHandle,
                VisibilityTimeout = 36000, // 10 hour timeout
            };

            await client.ChangeMessageVisibilityAsync(changeMessageVisibilityRequest);
        }
Beispiel #6
0
        /// <summary>
        /// Requeues the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="delayMilliseconds">Number of milliseconds to delay delivery of the message.</param>
        public void Requeue(Message message, int delayMilliseconds)
        {
            if (!message.Header.Bag.ContainsKey("ReceiptHandle"))
            {
                return;
            }

            var receiptHandle = message.Header.Bag["ReceiptHandle"].ToString();

            try
            {
                _logger.Value.InfoFormat("SqsMessageConsumer: requeueing the message {0}", message.Id);

                using (var client = new AmazonSQSClient(_awsConnection.Credentials, _awsConnection.Region))
                {
                    var urlResponse = client.GetQueueUrlAsync(_queueName).Result;
                    client.ChangeMessageVisibilityAsync(new ChangeMessageVisibilityRequest(urlResponse.QueueUrl, receiptHandle, 0)).Wait();
                }

                _logger.Value.InfoFormat("SqsMessageConsumer: requeued the message {0}", message.Id);
            }
            catch (Exception exception)
            {
                _logger.Value.ErrorException("SqsMessageConsumer: Error during requeing the message {0} with receipt handle {1} on the queue {2}", exception,
                                             message.Id, receiptHandle, _queueName);
                throw;
            }
        }
 public async Task ReturnMessageToQueue(string queueName, string messageId)
 {
     var changeMessageVisibilityRequest = new ChangeMessageVisibilityRequest()
     {
         QueueUrl          = QueueUrlFactory(queueName),
         ReceiptHandle     = messageId,
         VisibilityTimeout = 0
     };
     await _sqsClient.ChangeMessageVisibilityAsync(changeMessageVisibilityRequest);
 }
Beispiel #8
0
        public override async Task RenewLockAsync(MessageEnvelope message, long lockTimeout)
        {
            CheckOpened(message.CorrelationId);

            // Extend the message visibility
            var envelope = (Message)message.Reference;

            if (envelope != null)
            {
                var request = new ChangeMessageVisibilityRequest()
                {
                    QueueUrl          = _queue,
                    ReceiptHandle     = envelope.ReceiptHandle,
                    VisibilityTimeout = (int)(lockTimeout / 1000)
                };
                await _client.ChangeMessageVisibilityAsync(request, _cancel.Token);

                _logger.Trace(message.CorrelationId, "Renewed lock for message {0} at {1}", message, this);
            }
        }
Beispiel #9
0
        /// <summary>
        /// add some time so that we can continue to work on this message
        /// </summary>
        /// <param name="recieptHandle"></param>
        /// <param name="extensionInSeconds"></param>
        /// <param name="highPriority"></param>
        /// <returns></returns>
        public bool ExtendMessageOwnerShip(string recieptHandle, int extensionInSeconds)
        {
            ChangeMessageVisibilityRequest changeMessageVisibilityRequest = new ChangeMessageVisibilityRequest
            {
                ReceiptHandle     = recieptHandle,
                VisibilityTimeout = extensionInSeconds,
                QueueUrl          = _QueueUrl
            };
            ChangeMessageVisibilityResponse ignoreThis = _AmazonSQSClient.ChangeMessageVisibilityAsync(changeMessageVisibilityRequest).Result;

            return(ignoreThis.HttpStatusCode == System.Net.HttpStatusCode.OK);
        }
        public void ReleaseWorkItem(WorkItemInfo workItem)
        {
            string          queueSuffix = workItem.WorkerType;
            string          finalQueueName;
            AmazonSQSClient sqsClient = constructSqsClient(ConfigurationManager.AppSettings[AWSSQSConfigOptions.AWSQueueURL], queueSuffix, out finalQueueName);
            var             resTask   = sqsClient.ChangeMessageVisibilityAsync(finalQueueName, workItem.GetParamOrDefault("ReceiptHandle"), 1);

            resTask.Wait();
            if (resTask.Result.HttpStatusCode != System.Net.HttpStatusCode.OK)
            {
                throw new InvalidOperationException("The item was not correctly deleted, error code=" + resTask.Result.HttpStatusCode, resTask.Exception);
            }
        }
Beispiel #11
0
        IAsyncTask CreateRenewalTaskForMessage(Message message, AmazonSQSClient client)
        {
            return(_asyncTaskFactory.Create($"RenewPeekLock-{message.MessageId}",
                                            async() =>
            {
                _log.Info("Renewing peek lock for message with ID {0}", message.MessageId);

                await
                client.ChangeMessageVisibilityAsync(new ChangeMessageVisibilityRequest(_queueUrl,
                                                                                       message.ReceiptHandle, (int)_peekLockDuration.TotalSeconds));
            },
                                            intervalSeconds: (int)_peekLockRenewalInterval.TotalSeconds,
                                            prettyInsignificant: true));
        }
Beispiel #12
0
        private AsyncTask CreateRenewalTaskForMessage(Message message, AmazonSQSClient client)
        {
            var renewalTask = new AsyncTask(string.Format("RenewPeekLock-{0}", message.MessageId),
                                            async() =>
            {
                _log.Info("Renewing peek lock for message with ID {0}", message.MessageId);

                await client.ChangeMessageVisibilityAsync(new ChangeMessageVisibilityRequest(_queueUrl, message.ReceiptHandle, (int)_peekLockDuration.TotalSeconds));
            },
                                            prettyInsignificant: true)
            {
                Interval = _peekLockRenewalInterval
            };

            return(renewalTask);
        }
Beispiel #13
0
        static void Main(string[] args)
        {
            var batch_size = int.Parse(args[0]);
            var queue_url  = args[1];

            System.Console.WriteLine($"Reading {batch_size} messages at a time from SQS queue {queue_url}");
            var sqs = new AmazonSQSClient(); // need keys and region

            try
            {
                while (true)
                {
                    var receive_message_request = new ReceiveMessageRequest
                    {
                        AttributeNames = new List <string>()
                        {
                            "All"
                        },
                        MaxNumberOfMessages = batch_size,
                        QueueUrl            = queue_url,
                        WaitTimeSeconds     = 20
                    };

                    var result = sqs.ReceiveMessageAsync(receive_message_request).Result;

                    foreach (var message in result.Messages)
                    {
                        if (process_message(message))
                        {
                            sqs.DeleteMessageAsync(new DeleteMessageRequest
                            {
                                QueueUrl      = queue_url,
                                ReceiptHandle = message.ReceiptHandle
                            });
                        }
                        else
                        {
                            sqs.ChangeMessageVisibilityAsync(queue_url, message.ReceiptHandle, 10);
                        }
                    }
                }
            }
            catch (System.Exception)
            {
                throw;
            }
        }
Beispiel #14
0
        /// <summary>
        /// Rejects the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Reject(Message message)
        {
            if (!message.Header.Bag.ContainsKey("ReceiptHandle"))
            {
                return;
            }

            var receiptHandle = message.Header.Bag["ReceiptHandle"].ToString();

            try
            {
                s_logger.LogInformation(
                    "SqsMessageConsumer: Rejecting the message {Id} with receipt handle {ReceiptHandle} on the queue {ChannelName}",
                    message.Id, receiptHandle, _queueName
                    );

                using (var client = new AmazonSQSClient(_awsConnection.Credentials, _awsConnection.Region))
                {
                    var urlResponse = client.GetQueueUrlAsync(_queueName).Result;
                    if (_hasDlq)
                    {
                        client.ChangeMessageVisibilityAsync(new ChangeMessageVisibilityRequest(urlResponse.QueueUrl, receiptHandle, 0)).Wait();
                    }
                    else
                    {
                        client.DeleteMessageAsync(urlResponse.QueueUrl, receiptHandle).Wait();
                    }
                }

                s_logger.LogInformation(
                    "SqsMessageConsumer: Message {Id} with receipt handle {ReceiptHandle} on the queue {ChannelName} with requeue parameter {3} has been rejected",
                    message.Id, receiptHandle, _queueName
                    );
            }
            catch (Exception exception)
            {
                s_logger.LogError(exception, "SqsMessageConsumer: Error during rejecting the message {Id} with receipt handle {ReceiptHandle} on the queue {ChannelName}", message.Id, receiptHandle, _queueName);
                throw;
            }
        }