An Amazon SQS message.
 public void IsRequestHandled_Other()
 {
     var message = new Message();
     message.Body = @"{""ResponseQueueName"":""b1a84e89-e5b9-4720-9235-dcf9605a59c3"",""StoreIdentifier"":""Sixeyed-CloudServiceBus-ResponseData""}";
     var handler = new GetChangedEmployees();
     Assert.IsFalse(handler.IsRequestHandled(message));
 }
Esempio n. 2
0
        private string GetObjectKeyFromMessage(Amazon.SQS.Model.Message msg)
        {
            JObject notif = JObject.Parse(msg.Body);

            if (notif.ContainsKey("Message"))
            {
                JToken  jval    = notif.GetValue("Message");
                JObject message = JObject.Parse(jval.ToString());
                if (message.ContainsKey("receipt"))
                {
                    JToken  receipt  = message.GetValue("receipt");
                    JObject jreceipt = JObject.Parse(receipt.ToString());
                    if (jreceipt.ContainsKey("action"))
                    {
                        JToken  action  = jreceipt.GetValue("action");
                        JObject jaction = JObject.Parse(action.ToString());
                        if (jaction.ContainsKey("objectKey"))
                        {
                            JToken objkey       = jaction.GetValue("objectKey");
                            string s3object_key = objkey.ToString();
                            return(s3object_key);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Didnt find objkey");
                }
            }
            return("");
        }
 public static bool ProcessMessage(Message message)
 {
     Console.WriteLine("Message Recieved: {0}", message.Body);
     _completed = true;
     _completedSuccessfully = true;
     return true;
 }
Esempio n. 4
0
        public string WaitForNextEmail(string emailaddr, int waittimesec)
        {
            string email    = "";
            int    waittime = waittimesec > 20 ? 20 : waittimesec;

            if (Exists(emailaddr))
            {
                var monitor = monitors[emailaddr];
                do
                {
                    try
                    {
                        var sqsresp = sqsclient.ReceiveMessageAsync(new ReceiveMessageRequest
                        {
                            QueueUrl            = monitor.sqsqueue_url,
                            WaitTimeSeconds     = waittime,
                            MaxNumberOfMessages = 1
                        });
                        waittimesec -= waittime;
                        waittime     = waittimesec > 20 ? 20 : waittimesec;
                        var sqsresult = sqsresp.Result;
                        if (sqsresult.Messages.Count > 0)
                        {
                            if (sqsresult.Messages.Count != 1)
                            {
                                Console.WriteLine($"Message Count unexpectedly greater than 1 for {emailaddr}");
                            }

                            Amazon.SQS.Model.Message msg = sqsresult.Messages[0];
                            string objectkey             = GetObjectKeyFromMessage(msg);
                            create_s3client();
                            try
                            {
                                var s3response = s3client.GetObjectAsync(new GetObjectRequest
                                {
                                    BucketName = monitor.mbx.S3Bucket,
                                    Key        = objectkey
                                });
                                var s3result = s3response.Result;
                                using (var strm = s3result.ResponseStream)
                                {
                                    StreamReader reader = new StreamReader(strm);
                                    email = reader.ReadToEnd();
                                }
                            }
                            catch (AmazonS3Exception e)
                            {
                                Console.WriteLine("Exception when trying to download from bucket {0} for {1}: {2}", monitor.mbx.S3Bucket, emailaddr, e.Message);
                            }
                            break;
                        }
                    }
                    catch (AmazonSQSException e)
                    {
                        Console.WriteLine("Exception while waiting for next notification for {0}: {1}", emailaddr, e.Message);
                    }
                } while (waittimesec > 0);
            }
            return(email);
        }
 private ImTech.Notification.Messages.BaseMessage Map(Amazon.SQS.Model.Message message)
 {
     ImTech.Notification.Messages.BaseMessage objRet = null;
     objRet = JsonConvert.DeserializeObject <ImTech.Notification.Messages.BaseMessage>(message.Body);
     objRet = Map(message.Body, objRet.MessageType);
     return(objRet);
 }
        private async Task UpdateMessageVisibilityTimeout(SQSMessage message, string receiptHandle, Message typedMessage, Exception lastException)
        {
            if (TryGetApproxReceiveCount(message.Attributes, out int approxReceiveCount))
            {
                var visibilityTimeout        = _messageBackoffStrategy.GetBackoffDuration(typedMessage, approxReceiveCount, lastException);
                var visibilityTimeoutSeconds = (int)visibilityTimeout.TotalSeconds;

                var visibilityRequest = new ChangeMessageVisibilityRequest
                {
                    QueueUrl          = _queue.Uri.AbsoluteUri,
                    ReceiptHandle     = receiptHandle,
                    VisibilityTimeout = visibilityTimeoutSeconds
                };

                try
                {
                    await _queue.Client.ChangeMessageVisibilityAsync(visibilityRequest).ConfigureAwait(false);
                }
                catch (AmazonServiceException ex)
                {
                    _logger.LogError(0, ex, "Failed to update message visibility timeout by {VisibilityTimeout} seconds.", visibilityTimeoutSeconds);
                    _onError(ex, message);
                }
            }
        }
 public void IsRequestHandled()
 {
     var message = new Message();
     message.Body = fixedBody;
     var handler = new GetChangedEmployees();
     Assert.IsTrue(handler.IsRequestHandled(message));
 }
Esempio n. 8
0
        /// <summary>
        /// Delete a message from SQS queue
        /// </summary>
        /// <param name="message">The message to be deleted</param>
        /// <returns></returns>
        public async Task DeleteMessage(SQSMessage message)
        {
            try
            {
                if (message == null)
                {
                    throw new ArgumentNullException(nameof(message));
                }

                if (string.IsNullOrWhiteSpace(message.ReceiptHandle))
                {
                    throw new ArgumentNullException(nameof(message.ReceiptHandle));
                }

                if (string.IsNullOrWhiteSpace(queueUrl))
                {
                    throw new InvalidOperationException("Queue not initialized");
                }

                await sqsClient.DeleteMessageAsync(
                    new DeleteMessageRequest { QueueUrl = queueUrl, ReceiptHandle = message.ReceiptHandle });
            }
            catch (Exception exc)
            {
                ReportErrorAndRethrow(exc, "GetMessages", ErrorCode.StreamProviderManagerBase);
            }
        }
 protected override void OnReceivedMessage(Message message)
 {
     var action = new Action(() => ProcessMessage(message));
     var task = new Task(action);
     task.ContinueWith(MarkTaskAsComplete, TaskContinuationOptions.ExecuteSynchronously);
     task.Start();
 }
            public CheckForMessagesShould()
            {
                expectedQueueUrl = "QueueUrl";
                queueInfo        = new QueueInfo(typeof(TestMessage), expectedQueueUrl);
                var queues = new Dictionary <string, QueueInfo>();

                queues.Add("", queueInfo);

                var messageBusMock = new Mock <IMessageBus>();

                sqsServiceMock = new Mock <ISqsService>();
                var messageDeserializerMock = new Mock <IMessageDeserializer>();

                handlerMock = new Mock <IHandlerAsync <TestMessage> >();

                messageBusMock.Setup(x => x.Queues).Returns(queues);
                messageBusMock.Setup(x => x.GetHandler <TestMessage>()).Returns(handlerMock.Object);

                expectedMessage = new TestMessage();
                var response = new Amazon.SQS.Model.ReceiveMessageResponse();

                busMessage = new Amazon.SQS.Model.Message();
                busMessage.ReceiptHandle = "Recipt001";
                response.Messages.Add(busMessage);
                sqsServiceMock.Setup(x => x.ReceiveMessageAsync(expectedQueueUrl)).Returns(Task.FromResult(response));
                messageDeserializerMock.Setup(x => x.Deserialize <TestMessage>(busMessage)).Returns(expectedMessage);

                sut = new MessageBusListener(messageBusMock.Object, sqsServiceMock.Object, messageDeserializerMock.Object);
            }
 private HeaderResult <string> ReadContentType(Amazon.SQS.Model.Message sqsMessage)
 {
     if (sqsMessage.MessageAttributes.TryGetValue(HeaderNames.ContentType, out MessageAttributeValue value))
     {
         return(new HeaderResult <string>(value.StringValue, true));
     }
     return(new HeaderResult <string>(String.Empty, true));
 }
        private void Handle(SQSModelNameSpace.Message sqsMessage, Action <Message> onMessageReceived)
        {
            var message = Message.FromJson(sqsMessage.Body);

            onMessageReceived(message);

            DeleteMessage(sqsMessage);
        }
 private HeaderResult <string> ReadReceiptHandle(Amazon.SQS.Model.Message sqsMessage)
 {
     if (sqsMessage.ReceiptHandle != null)
     {
         return(new HeaderResult <string>(sqsMessage.ReceiptHandle, true));
     }
     return(new HeaderResult <string>(string.Empty, true));
 }
Esempio n. 14
0
 private async Task DeleteMessage(Amazon.SQS.Model.Message message)
 {
     var deleteMessageRequest = new DeleteMessageRequest {
         QueueUrl      = _config.Value.AwsSqsQueueUrl,
         ReceiptHandle = message.ReceiptHandle
     };
     await _sqsClient.DeleteMessageAsync(deleteMessageRequest);
 }
Esempio n. 15
0
        internal static SQSBatchContainer FromSQSMessage(SQSMessage msg, long sequenceId)
        {
            var json     = JObject.Parse(msg.Body);
            var sqsBatch = SerializationManager.DeserializeFromByteArray <SQSBatchContainer>(json["payload"].ToObject <byte[]>());

            sqsBatch.Message       = msg;
            sqsBatch.sequenceToken = new EventSequenceTokenV2(sequenceId);
            return(sqsBatch);
        }
 public void BodyAs()
 {
     var message = new Message();
     message.Body = fixedBody;
     var request = message.BodyAsRequest<GetChangedEmployeesRequest>();
     Assert.IsNotNull(request);
     Assert.AreEqual(fixedDate, request.LastChangeDate);
     Assert.AreEqual(fixedQueueName, request.ResponseQueueUrl);
 }
 private HeaderResult <int> ReadHandledCount(Amazon.SQS.Model.Message sqsMessage)
 {
     if (sqsMessage.MessageAttributes.TryGetValue(HeaderNames.HandledCount, out MessageAttributeValue value))
     {
         if (int.TryParse(value.StringValue, out int handledCount))
         {
             return(new HeaderResult <int>(handledCount, true));
         }
     }
     return(new HeaderResult <int>(0, true));
 }
Esempio n. 18
0
        private void DeleteMessge(Amazon.SQS.Model.Message message)
        {
            DeleteMessageResponse objDeleteMessageResponse = new DeleteMessageResponse();
            var deleteMessageRequest = new DeleteMessageRequest()
            {
                QueueUrl = sellerInfo.QueueUrl, ReceiptHandle = message.ReceiptHandle
            };

            objDeleteMessageResponse = sqsClient.DeleteMessage(deleteMessageRequest);
            nLogger.Log(LogLevel.Info, string.Format("Message deleted: {0}", message.MessageId));
        }
 private HeaderResult <Guid> ReadMessageId(Amazon.SQS.Model.Message sqsMessage)
 {
     if (sqsMessage.MessageAttributes.TryGetValue(HeaderNames.Id, out MessageAttributeValue value))
     {
         if (Guid.TryParse(value.StringValue, out Guid messageId))
         {
             return(new HeaderResult <Guid>(messageId, true));
         }
     }
     return(new HeaderResult <Guid>(Guid.Empty, false));
 }
 private HeaderResult <DateTime> ReadTimestamp(Amazon.SQS.Model.Message sqsMessage)
 {
     if (sqsMessage.MessageAttributes.TryGetValue(HeaderNames.Timestamp, out MessageAttributeValue value))
     {
         if (DateTime.TryParse(value.StringValue, out DateTime timestamp))
         {
             return(new HeaderResult <DateTime>(timestamp, true));
         }
     }
     return(new HeaderResult <DateTime>(DateTime.UtcNow, true));
 }
 private HeaderResult <string> ReadTopic(Amazon.SQS.Model.Message sqsMessage)
 {
     if (sqsMessage.MessageAttributes.TryGetValue(HeaderNames.Topic, out MessageAttributeValue value))
     {
         //we have an arn, and we want the topic
         var arnElements = value.StringValue.Split(':');
         var topic       = arnElements[(int)ARNAmazonSNS.TopicName];
         return(new HeaderResult <string>(topic, true));
     }
     return(new HeaderResult <string>(String.Empty, false));
 }
 private HeaderResult <MessageType> ReadMessageType(Amazon.SQS.Model.Message sqsMessage)
 {
     if (sqsMessage.MessageAttributes.TryGetValue(HeaderNames.MessageType, out MessageAttributeValue value))
     {
         if (Enum.TryParse <MessageType>(value.StringValue, out MessageType messageType))
         {
             return(new HeaderResult <MessageType>(messageType, true));
         }
     }
     return(new HeaderResult <MessageType>(MessageType.MT_UNACCEPTABLE, false));
 }
Esempio n. 23
0
        private IAsyncTask CreateRenewalTaskForMessage(Message message, string queueUrl, IAmazonSQS client)
        {
            return(m_amazonInternalSettings.AsyncTaskFactory.Create($"RenewPeekLock-{message.MessageId}", async() =>
            {
                m_log.Info("Renewing peek lock for message with ID {messageId}", message.MessageId);

                var request = new ChangeMessageVisibilityRequest(queueUrl, message.ReceiptHandle, (int)m_amazonInternalSettings.AmazonPeekLockDuration.PeekLockDuration.TotalSeconds);

                await client.ChangeMessageVisibilityAsync(request);
            }, intervalSeconds: (int)m_amazonInternalSettings.AmazonPeekLockDuration.PeekLockRenewalInterval.TotalSeconds, prettyInsignificant: true));
        }
        bool MessageIsExpiredUsingNativeSqsSentTimestamp(Message message, TimeSpan timeToBeReceived)
        {
            if (!message.Attributes.TryGetValue("SentTimestamp", out var sentTimeStampString))
            {
                return(false);
            }

            var sentTime = GetTimeFromUnixTimestamp(sentTimeStampString);

            return(_rebusTime.Now.UtcDateTime - sentTime > timeToBeReceived);
        }
 private HeaderResult <Guid> ReadCorrelationid(Amazon.SQS.Model.Message sqsMessage)
 {
     if (sqsMessage.MessageAttributes.TryGetValue(HeaderNames.CorrelationId, out MessageAttributeValue value))
     {
         if (Guid.TryParse(value.StringValue, out Guid correlationId))
         {
             return(new HeaderResult <Guid>(correlationId, true));
         }
     }
     return(new HeaderResult <Guid>(Guid.Empty, true));
 }
        TransportMessage ExtractTransportMessageFrom(Message message)
        {
            var sqsMessage = _serializer.Deserialize(message.Body);

            if (sqsMessage.Headers.ContainsKey("rebus-sqs-s3-fallback"))
            {
                var messageBody = ""; // Download from S3
                sqsMessage = new AmazonSQSTransportMessage(sqsMessage.Headers, messageBody);
            }

            return(new TransportMessage(sqsMessage.Headers, GetBodyBytes(sqsMessage.Body)));
        }
Esempio n. 27
0
        public async Task DispatchMessage(SQSMessage message)
        {
            Message typedMessage;

            try
            {
                typedMessage = _serialisationRegister.DeserializeMessage(message.Body);
            }
            catch (MessageFormatNotSupportedException ex)
            {
                Log.Trace(
                    "Didn't handle message [{0}]. No serialiser setup",
                    message.Body ?? string.Empty);
                DeleteMessageFromQueue(message.ReceiptHandle);
                _onError(ex, message);
                return;
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Error deserialising message");
                _onError(ex, message);
                return;
            }

            try
            {
                var handlingSucceeded = true;

                if (typedMessage != null)
                {
                    typedMessage.ReceiptHandle = message.ReceiptHandle;
                    typedMessage.QueueUrl      = _queue.Url;
                    handlingSucceeded          = await CallMessageHandlers(typedMessage).ConfigureAwait(false);
                }

                if (handlingSucceeded)
                {
                    DeleteMessageFromQueue(message.ReceiptHandle);
                }
            }
            catch (Exception ex)
            {
                var errorText = string.Format("Error handling message [{0}]", message.Body);
                Log.Error(ex, errorText);

                if (typedMessage != null)
                {
                    _messagingMonitor.HandleException(typedMessage.GetType().Name);
                }

                _onError(ex, message);
            }
        }
        private void Handle(sqs.Message sqsMessage, Action <Message> onMessageReceived)
        {
            var message = Message.FromJson(sqsMessage.Body);

            onMessageReceived(message);

            var deleteRequest = new DeleteMessageRequest();

            deleteRequest.QueueUrl      = Address;
            deleteRequest.ReceiptHandle = sqsMessage.ReceiptHandle;
            _sqsClient.DeleteMessage(deleteRequest);
        }
Esempio n. 29
0
        public async Task DispatchMessage(SQSMessage message)
        {
            Message typedMessage;
            try
            {
                typedMessage = _serialisationRegister.DeserializeMessage(message.Body);
            }
            catch (MessageFormatNotSupportedException ex)
            {
                Log.Trace(
                    "Didn't handle message [{0}]. No serialiser setup",
                    message.Body ?? string.Empty);
                DeleteMessageFromQueue(message.ReceiptHandle);
                _onError(ex, message);
                return;
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Error deserialising message");
                _onError(ex, message);
                return;
            }

            try
            {
                var handlingSucceeded = true;

                if (typedMessage != null)
                {
                    typedMessage.ReceiptHandle = message.ReceiptHandle;
                    typedMessage.QueueUrl = _queue.Url;
                    handlingSucceeded = await CallMessageHandlers(typedMessage).ConfigureAwait(false);
                }

                if (handlingSucceeded)
                {
                    DeleteMessageFromQueue(message.ReceiptHandle);
                }
            }
            catch (Exception ex)
            {
                var errorText = string.Format("Error handling message [{0}]", message.Body);
                Log.Error(ex, errorText);

                if (typedMessage != null)
                {
                    _messagingMonitor.HandleException(typedMessage.GetType().Name);
                }

                _onError(ex, message);
            }
        }
Esempio n. 30
0
        private Notification DeserializeNotification(Amazon.SQS.Model.Message message)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Notification));

            Notification notification;

            using (TextReader reader = new StringReader(message.Body))
            {
                notification = (Notification)serializer.Deserialize(reader);
            }

            return(notification);
        }
Esempio n. 31
0
        private async Task DeleteMessage(AwsModel.Message message)
        {
            var response = await Client.AwsClient.DeleteMessageAsync(new AwsModel.DeleteMessageRequest
            {
                QueueUrl      = Url,
                ReceiptHandle = message.ReceiptHandle
            });

            if (!HttpStatusCode.OK.Equals(response.HttpStatusCode))
            {
                ErrorEvent?.Invoke(false, $"Message Delete Error => Message id : {message.MessageId} - Status Code: {response.HttpStatusCode}");
            }
        }
Esempio n. 32
0
 /// <summary>
 /// This class encapsulates a messages context as it passes through a middleware pipeline.
 /// </summary>
 /// <param name="message">The JustSaying message that was deserialized from SQS.</param>
 /// <param name="messageType">The type of the JustSaying message contained in <see cref="Message"/>.</param>
 /// <param name="queueName">The queue from which this message was received.</param>
 /// <param name="visibilityUpdater">The <see cref="IMessageVisibilityUpdater"/> to use to update message visibilities on failure.</param>
 /// <param name="messageDeleter">The <see cref="IMessageDeleter"/> to use to remove a message from the queue on success.</param>
 public HandleMessageContext(string queueName, Amazon.SQS.Model.Message rawMessage, Message message,
                             Type messageType, IMessageVisibilityUpdater visibilityUpdater, IMessageDeleter messageDeleter,
                             Uri queueUri, MessageAttributes messageAttributes)
 {
     Message           = message;
     MessageType       = messageType;
     QueueName         = queueName;
     VisibilityUpdater = visibilityUpdater;
     MessageDeleter    = messageDeleter;
     QueueUri          = queueUri;
     MessageAttributes = messageAttributes;
     RawMessage        = rawMessage;
 }
Esempio n. 33
0
        public Task Send(Message message)
        {
            var messageString = JsonConvert.SerializeObject(message);
            _logger.DebugFormat("SQSMessageProducer: Publishing message with topic {0} and id {1} and message: {2}", message.Header.Topic, message.Id, messageString);
            
            using (var client = new AmazonSimpleNotificationServiceClient())
            {
                var topicArn = EnsureTopic(message.Header.Topic, client);

                var publishRequest = new PublishRequest(topicArn, messageString);
                return client.PublishAsync(publishRequest);
            }
        }
Esempio n. 34
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));
        }
        protected virtual void Given()
        {
            _typedMessage = new OrderAccepted();

            _sqsMessage = new SQSMessage
            {
                Body          = JsonConvert.SerializeObject(_typedMessage),
                ReceiptHandle = "i_am_receipt_handle"
            };

            _loggerFactory.CreateLogger(Arg.Any <string>()).Returns(_logger);
            _queue = new DummySqsQueue(new Uri(ExpectedQueueUrl), _amazonSqsClient);
            _serializationRegister.DeserializeMessage(Arg.Any <string>()).Returns(_typedMessage);
        }
        static bool MessageIsExpiredUsingNativeSqsSentTimestamp(Message message, TimeSpan timeToBeReceived)
        {
            string sentTimeStampString;

            if (message.Attributes.TryGetValue("SentTimestamp", out sentTimeStampString))
            {
                var sentTime = GetTimeFromUnixTimestamp(sentTimeStampString);
                if (RebusTime.Now.UtcDateTime - sentTime > timeToBeReceived)
                {
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 37
0
        static bool MessageIsExpiredUsingRebusSentTime(Message message, TimeSpan timeToBeReceived)
        {
            MessageAttributeValue rebusUtcTimeSentAttributeValue;

            if (message.MessageAttributes.TryGetValue(Headers.SentTime, out rebusUtcTimeSentAttributeValue))
            {
                var rebusUtcTimeSent = DateTimeOffset.ParseExact(rebusUtcTimeSentAttributeValue.StringValue, "O", null);

                if (RebusTime.Now.UtcDateTime - rebusUtcTimeSent > timeToBeReceived)
                {
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 38
0
        public async Task ProcessMessage(Message message, CancellationToken ctx)
        {
            var type = typeof(QueueMessageProcessor).GetTypeInfo().Assembly.GetTypes()
                .Where(t => typeof(ITask).IsAssignableFrom(t) && t.GetTypeInfo().IsClass)
                .Where(t => t.Name == message.Body)
                .SingleOrDefault();

            if (type == null)
            {
                return;
            }
            else
            {
                var sw = new Stopwatch();
                var task = (ITask)ActivatorUtilities.CreateInstance(provider, type);
                logger.LogInformation($"Received event from SQS, running task {type.Name}");
                sw.Start();
                await task.DoWork(ctx);
                logger.LogInformation($"Triggered task {type.Name} completed in {sw.Elapsed.TotalSeconds} seconds");
            }
        }
Esempio n. 39
0
        public void Acknowledge(Message message)
        {
            if(!message.Header.Bag.ContainsKey("ReceiptHandle"))
                return;

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

            try
            {
                using (var client = new AmazonSQSClient())
                {
                    client.DeleteMessageAsync(new DeleteMessageRequest(_queueUrl, receiptHandle));

                    _logger.InfoFormat("SqsMessageConsumer: Deleted the message {0} with receipt handle {1} on the queue {2}", message.Id, receiptHandle, _queueUrl);
                }
            }
            catch (Exception exception)
            {
                _logger.ErrorException("SqsMessageConsumer: Error during deleting the message {0} with receipt handle {1} on the queue {2}", exception, message.Id, receiptHandle, _queueUrl);
                throw;
            }
        }
Esempio n. 40
0
 /// <summary>
 /// Requeues the specified message.
 /// </summary>
 /// <param name="message">The message.</param>
 public void Requeue(Message message)
 {
     Requeue(message, 0);
 }
 protected override bool OnMessageRecieved(Message message)
 {
     _messagesReceived.Add(message);
     return true;
 }
Esempio n. 42
0
        private TransportMessage GetTransportMessage(Message message)
        {
            var headers = message.MessageAttributes.ToDictionary((kv) => kv.Key, (kv) => kv.Value.StringValue);

            return new TransportMessage(headers, GetBodyBytes(message.Body));

        }
Esempio n. 43
0
 public static void ValidateMD5(Message message)
 {
     ValidateMD5(message.Body, message.MessageId, message.MD5OfBody);
 }
Esempio n. 44
0
        private static bool MessageIsExpiredUsingRebusSentTime(Message message, TimeSpan timeToBeReceived)
        {
            MessageAttributeValue rebusUtcTimeSentAttributeValue;
            if (message.MessageAttributes.TryGetValue(Headers.SentTime, out rebusUtcTimeSentAttributeValue))
            {
                var rebusUtcTimeSent = DateTimeOffset.ParseExact(rebusUtcTimeSentAttributeValue.StringValue, "O", null);

                if (Time.RebusTime.Now.UtcDateTime - rebusUtcTimeSent > timeToBeReceived)
                {
                    return true;
                }

            }

            return false;

        }
        protected void ProcessMessage(Message message)
        {
            var handledSuccessfully = false;
            try
            {
                handledSuccessfully = _onRecieved.Invoke(message);
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Exception occured whilst processing message.");

                try
                {
                    if (_onError != null)
                    {
                        _onError(ex, message);
                    }
                }
                catch (Exception onErrorException)
                {
                    Logger.Error(onErrorException, "Exception during invocation of onError action.");
                }
            }
            finally
            {
                if (handledSuccessfully)
                {
                    _amazonSQSClient.DeleteMessage(new DeleteMessageRequest
                    {
                        QueueUrl = QueueUrl,
                        ReceiptHandle = message.ReceiptHandle,
                    });
                }
            }
        }
Esempio n. 46
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);
        }
Esempio n. 47
0
        private void RemoveMessage(Message message)
        {
            try
            {
                Log.Info("Deleting SQS message {0} on queue: {1}", message.MessageId, QueueUrl);
                var request = new DeleteMessageRequest {QueueUrl = QueueUrl, ReceiptHandle = message.ReceiptHandle};

                _client.DeleteMessage(request);
            }
            catch (Exception ex)
            {
                Log.Error("Error removing message:{0}", message.ReceiptHandle);
                Log.Error(ex);
            }
        }
Esempio n. 48
0
        private void single_cycle()
        {
            DateTime beforeTime = DateTime.Now;
            last_Response = THREAD_RESPONSE.NO_RESPONSE;
            last_request_id = String.Empty;
            // Get a single MSG from Queue_Requests
            bool msgFound;
            Amazon.SQS.Model.Message msg;
            if (!SQS.Get_Msg_From_Req_Q(out msg, out msgFound))
            {
                MyLog("Get_Msg_From_Req_Q() failed (# " + get_msg_failures + ") !!!");
                if (get_msg_failures < get_msg_failures_allowed)
                {
                    get_msg_failures++;
                    Thread.Sleep(1000);
                    last_Response = THREAD_RESPONSE.FAIL;
                    return;
                }
                MyLog("Get_Msg_From_Req_Q() failed and (" + get_msg_failures + "=get_msg_failures >= get_msg_failures_allowed=" + get_msg_failures_allowed + ") !!!");
                last_Response = THREAD_RESPONSE.FAIL;
                return;
            }
            if (get_msg_failures > 0)
            {
                MyLog("Get_Msg_From_Req_Q() succeeded after " + get_msg_failures + " failures");
            }

            DateTime beforeProcessingTime = DateTime.Now;
            get_msg_failures = 0;

            // if there is No Msg - Sleep & continue;
            if (!msgFound)
            {
                Thread.Sleep(250);
                last_Response = THREAD_RESPONSE.NO_REQUEST;
                return;
            }

            Utils.lastMsg_Time = DateTime.Now;

            // Extract the ImageData
            ImageDataRequest imageData = null;
            if (!deciferImageDataFromBody(msg.Body, out imageData))
            {
                MyLog("GHR_Dispatcher.deciferImagesDataFromJSON(msg.Body=" + msg.Body + ", out imagesDatas) failed!!!");
                last_Response = THREAD_RESPONSE.FAIL;
                return;
            }

            last_request_id = imageData.item_id;
            last_msg = msg;

            // Add Msg to Queue_Readies
            if (!SQS.Send_Msg_To_Readies_Q(RenderStatus.STARTED, imageData.item_id, beforeProcessingTime))
            {
                MyLog("Form1.Send_Msg_To_Readies_Q(status=STARTED,imageData.item_id=" + imageData.item_id + ") failed");
                last_Response = THREAD_RESPONSE.FAIL;
                return;
            }

            if (imageData.operation == "render_model")
            {
                DateTime beforeRhino = DateTime.Now;
                // Process Msg to picture
                String resultingLocalImageFilePath;
                TimeSpan renderTime, buildTime, waitTime;
                if (!Process_Into_Image_File(imageData, out resultingLocalImageFilePath, out renderTime, out buildTime, out waitTime))
                {
                    String logLine = "Process_Msg_Into_Image_File(msg) failed!!! (). ImageData=" + imageData.ToString();
                    MyLog(logLine);
                    SQS.Send_Msg_To_ERROR_Q(id, logLine);
                    last_Response = THREAD_RESPONSE.FAIL;
                    return;
                }

                DateTime afterRhino_Before_S3 = DateTime.Now;

                String fileName_on_S3 = imageData.item_id.ToString() + ".jpg";
                if (!S3.Write_File_To_S3(resultingLocalImageFilePath, fileName_on_S3))
                {
                    String logLine = "Write_File_To_S3(resultingImagePath=" + resultingLocalImageFilePath + ", fileName_on_S3=" + fileName_on_S3 + ") failed !!!";
                    SQS.Send_Msg_To_ERROR_Q(id, logLine);
                    last_Response = THREAD_RESPONSE.FAIL;
                    return;
                }

                DateTime afterS3_Before_SQS = DateTime.Now;

                // Delete Msg From Queue_Requests
                if (!SQS.Delete_Msg_From_Req_Q(msg))
                {
                    MyLog("Delete_Msg_From_Req_Q(msg) failed!!!");
                    // Add Msg to Queue_Readies
                    if (!SQS.Send_Msg_To_Readies_Q(RenderStatus.ERROR, imageData.item_id, beforeProcessingTime))
                    {
                        MyLog("Form1.Send_Msg_To_Readies_Q(status=ERROR,imageData.item_id=" + imageData.item_id + ") failed");
                    }
                    last_Response = THREAD_RESPONSE.FAIL;
                    return;
                }
                // Add Msg to Queue_Readies
                if (!SQS.Send_Msg_To_Readies_Q(RenderStatus.FINISHED, imageData.item_id, beforeProcessingTime))
                {
                    MyLog("Form1.Send_Msg_To_Readies_Q(imageData.item_id=" + imageData.item_id + ") failed");
                    last_Response = THREAD_RESPONSE.FAIL;
                    return;
                }

                DateTime afterSQS = DateTime.Now;

                MyLog("Reading msg time =" + (beforeRhino - beforeProcessingTime).TotalMilliseconds.ToString() + " millis");
                MyLog("Wait time=" + waitTime.TotalMilliseconds.ToString() + " millis");
                MyLog("Rhino build time=" + buildTime.TotalMilliseconds.ToString() + " millis");
                MyLog("Render time=" + renderTime.TotalMilliseconds.ToString() + " millis");
                MyLog("S3 Time=" + (afterS3_Before_SQS - afterRhino_Before_S3).TotalMilliseconds.ToString() + " millis");
                MyLog("SQS Time=" + (afterSQS - afterS3_Before_SQS).TotalMilliseconds.ToString() + " millis");
                MyLog("Total single_cycle(id=" + imageData.item_id + ")=" + (afterSQS - beforeTime).TotalMilliseconds.ToString() + " millis");

                last_Response = THREAD_RESPONSE.SUCCESS;
                return;
            }
            else
            {
                MyLog("ERROR !!! - (" + imageData.operation + "=imageData.operation != \"render_model\")");
            }
            MyLog("imageData=" + imageData.ToString());
            last_Response = THREAD_RESPONSE.FAIL;
            return;
        }
        private ActivityItems getActivityItems(Message message)
        {
            XmlSerializer serializer = new XmlSerializer (typeof(ActivityItems));
            ActivityItems items = null;
            // deserialize the body of the SQS message as an ActivityItems instance
            using (XmlTextReader sr = new XmlTextReader (new StringReader (message.Body))) {
                log.Debug("Received SQS message: " + message.MessageId + ": " + message.Body);
                items = (ActivityItems)serializer.Deserialize (sr);
            }

            // Print out the message metadata
            foreach (Amazon.SQS.Model.Attribute attr in message.Attribute) {
                if(attr.Name.Equals("ApproximateReceiveCount")) {
                    if(Int16.Parse(attr.Value) == 1) {
                        log.Info(attr.Name + ": " + attr.Value);
                    } else {
                        log.Warn(attr.Name + ": " + attr.Value);
                    }
                }
            }

            return items;
        }
Esempio n. 50
0
 public static void ProcessError(Exception ex, Message message)
 {
     Console.WriteLine("Error: {0}, Message Body: {0}", ex, message.Body);
     _completed = true;
 }
Esempio n. 51
0
 public SQSMessageEventArgs(Message message)
 {
     SQSMessage = message;
 }
Esempio n. 52
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)
        {
            try
            {
                Reject(message, false);

                using (var client = new AmazonSQSClient(_credentials))
                {
                    _logger.Value.InfoFormat("SqsMessageConsumer: requeueing the message {0}", message.Id);

                    message.Header.Bag.Remove("ReceiptHandle");
                    var request = new SendMessageRequest(_queueUrl, JsonConvert.SerializeObject(message))
                                  {
                                      DelaySeconds = (int)TimeSpan.FromMilliseconds(delayMilliseconds).TotalSeconds
                                  };

                    client.SendMessageAsync(request).Wait();
                }

                _logger.Value.InfoFormat("SqsMessageConsumer: requeued the message {0}", message.Id);
            }
            catch (Exception exception)
            {
                _logger.Value.ErrorException("SqsMessageConsumer: Error purging queue {0}", exception, _queueUrl);
                throw;
            }
        }
Esempio n. 53
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;
        }
Esempio n. 54
0
        /// <summary>
        /// Deletes a single message from the queue
        /// </summary>
        public bool DeleteMessage(Message message)
        {
            ClearErrorInfo();

            bool result = false;
            try
            {
                delMessageRequest.ReceiptHandle = message.ReceiptHandle;
                queue.DeleteMessage(delMessageRequest);
                result = true;
            }
            catch (Exception ex)
            {
                ErrorCode = e_Exception;
                ErrorMessage = ex.Message;
            }

            return result;
        }
Esempio n. 55
0
        private bool MessageIsExpired(Message message)
        {
            MessageAttributeValue value;
            TimeSpan timeToBeReceived;
            if (message.MessageAttributes.TryGetValue(Headers.TimeToBeReceived, out value))
            {
                timeToBeReceived = TimeSpan.Parse(value.StringValue);

                if (MessageIsExpiredUsingRebusSentTime(message, timeToBeReceived)) return true;
                if (MessageIsExpiredUsingNativeSqsSentTimestamp(message, timeToBeReceived)) return true;
            }

            return false;
        }
        void processMessage(Message message, string jobId)
        {
            var messageJobId = getJobIdFromMessage(message);
            if (messageJobId == null)
                return;

            var command = new DownloadJobCommand(this.manager, this.vaultName, jobId, this.filePath, this.options);
            command.Execute();
        }
Esempio n. 57
0
 private static bool MessageIsExpiredUsingNativeSqsSentTimestamp(Message message, TimeSpan timeToBeReceived)
 {
     string sentTimeStampString;
     if (message.Attributes.TryGetValue("SentTimestamp", out sentTimeStampString))
     {
         var sentTime = GetTimeFromUnixTimestamp(sentTimeStampString);
         if (Time.RebusTime.Now.UtcDateTime - sentTime > timeToBeReceived)
         {
             return true;
         }
     }
     return false;
 }
 protected virtual void OnReceivedMessage(Message message)
 {
     ProcessMessage(message);
 }
		private bool TryDeleteRecordSQS(Message InMessage)
		{
#if DEBUG
			// Debug doesn't empty the queue - it's just reads records
			return true;
#else
			try
			{
				var DeleteRequest = new DeleteMessageRequest
				{
					QueueUrl = Config.Default.AWSSQSQueueUrl,
					ReceiptHandle = InMessage.ReceiptHandle
				};

				var DeleteResponse = SqsClient.DeleteMessage(DeleteRequest);
				return DeleteResponse.HttpStatusCode == HttpStatusCode.OK;
			}
			catch (Exception Ex)
			{
				CrashReporterProcessServicer.WriteException("TryDeleteRecordSQS: " + Ex.ToString());
			}
			return false;
#endif
        }
Esempio n. 60
-2
        public void Reject(Message message, bool requeue)
        {
            if (!message.Header.Bag.ContainsKey("ReceiptHandle"))
                return;

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

            try
            {
                _logger.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())
                {
                    if (requeue)
                    {
                        client.ChangeMessageVisibility(new ChangeMessageVisibilityRequest(_queueUrl, receiptHandle, 0));
                    }
                    else
                    {
                        client.DeleteMessage(_queueUrl, receiptHandle);
                    }
                }

                _logger.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.ErrorException("SqsMessageConsumer: Error during rejecting the message {0} with receipt handle {1} on the queue {2}", exception, message.Id, receiptHandle, _queueUrl);
                throw;
            }
        }