/// <summary>
        /// Process Queue message
        /// </summary>
        /// <param name="msg"></param>
        private void ProcessQueueMessage(CloudQueueMessage msg)
        {
            if (msg.DequeueCount > 5)
            {
                Trace.TraceError("Deleting poison message:    message {0} Role Instance {1}.",
                                 msg.ToString(), GetRoleInstance());
                sendEmailQueue.DeleteMessage(msg);
                return;
            }
            var messageParts = msg.AsString.Split(new char[] { ',' });
            var partitionKey = messageParts[0];
            var rowKey       = messageParts[1];

            //var restartFlag = messageParts[2];
            Trace.TraceInformation("ProcessQueueMessage start:  partitionKey {0} rowKey {1} Role Instance {2}.",
                                   partitionKey, rowKey, GetRoleInstance());
            // Get the row in the Message table that has data we need to send the email.
            var retrieveOperation      = TableOperation.Retrieve <SendEmail>(partitionKey, rowKey);
            var retrievedResult        = messageTable.Execute(retrieveOperation);
            var emailRowInMessageTable = retrievedResult.Result as SendEmail;

            if (emailRowInMessageTable == null)
            {
                Trace.TraceError("SendEmail row not found:  partitionKey {0} rowKey {1} Role Instance {2}.",
                                 partitionKey, rowKey, GetRoleInstance());
                return;
            }

            var htmlMessageBodyRef = emailRowInMessageTable.MessageBody;
            var textMessageBodyRef = emailRowInMessageTable.MessageBody;

            if (emailRowInMessageTable.EmailSent != true)
            {
                SendEmail(emailRowInMessageTable, htmlMessageBodyRef, textMessageBodyRef);

                var emailRowToDelete = new SendEmail {
                    PartitionKey = partitionKey, RowKey = rowKey, ETag = "*"
                };
                emailRowInMessageTable.EmailSent = true;

                var upsertOperation = TableOperation.InsertOrReplace(emailRowInMessageTable);
                messagearchiveTable.Execute(upsertOperation);
                var deleteOperation = TableOperation.Delete(emailRowToDelete);
                messageTable.Execute(deleteOperation);
                Trace.TraceError("Row deleted from message Deleted found:  partitionKey {0} rowKey {1} Role Instance {2}.",
                                 partitionKey, rowKey, GetRoleInstance());
            }

            // Delete the queue message.
            sendEmailQueue.DeleteMessage(msg);

            Trace.TraceInformation("ProcessQueueMessage complete:  partitionKey {0} rowKey {1} Role Instance {2}.",
                                   partitionKey, rowKey, GetRoleInstance());
        }
Exemplo n.º 2
0
 public string GetErrors()
 {
     ErrorQueue = CloudConfiguration.GetErrorQueue();
     if (ErrorQueue.PeekMessage() == null)
     {
         return("");
     }
     else
     {
         CloudQueueMessage error        = ErrorQueue.GetMessage();
         string            errorMessage = error.ToString();
         ErrorQueue.DeleteMessage(error);
         return(new JavaScriptSerializer().Serialize(errorMessage));
     }
 }
Exemplo n.º 3
0
        private Message RehydrateMessage(CloudQueueMessage cqm)
        {
            Message message = JsonConvert.DeserializeObject <Message>(cqm.ToString());

            if (!string.IsNullOrEmpty(message.Reference))
            {
                CloudBlob blob = null;

                lock (containerLock)
                    blob = _container.GetBlobReference(message.Reference);

                var text = blob.DownloadText();
                _dblog.InfoFormat("Retrieved pocketed message from {0} with length {1}", blob.Uri, text.Length);


                message.Content = text;
            }

            return(message);
        }
Exemplo n.º 4
0
        private void ProcessQueueMessage(CloudQueueMessage msg)
        {
            Stopwatch requestTimer = Stopwatch.StartNew();
            var       request      = RequestTelemetryHelper.StartNewRequest("ProcessEmailQueueMessage", DateTimeOffset.UtcNow);

            CallContext.LogicalSetData(CORRELATION_SLOT, request.Id);
            //Thread.SetData(Thread.GetNamedDataSlot(CORRELATION_SLOT), request.Id);
            try
            {
                // Log and delete if this is a "poison" queue message (repeatedly processed
                // and always causes an error that prevents processing from completing).
                // Production applications should move the "poison" message to a "dead message"
                // queue for analysis rather than deleting the message.
                if (msg.DequeueCount > 5)
                {
                    Trace.TraceError("Deleting poison message:    message {0} Role Instance {1}.",
                                     msg.ToString(), GetRoleInstance());
                    sendEmailQueue.DeleteMessage(msg);
                    request.Properties.Add(new KeyValuePair <string, string>("FailureCode", "PoisonMessage"));
                    request.Properties.Add(new KeyValuePair <string, string>("DequeueCount", msg.DequeueCount.ToString()));
                    if (msg.InsertionTime != null)
                    {
                        request.Metrics.Add(new KeyValuePair <string, double>("EmailProcessingTimeMs", ((TimeSpan)(DateTimeOffset.Now - msg.InsertionTime)).Milliseconds));
                    }
                    RequestTelemetryHelper.DispatchRequest(request, requestTimer.Elapsed, false);
                    return;
                }
                // Parse message retrieved from queue.
                // Example:  2012-01-01,[email protected],0
                var messageParts = msg.AsString.Split(new char[] { ',' });
                var partitionKey = messageParts[0];
                var rowKey       = messageParts[1];
                var restartFlag  = messageParts[2];
                Trace.TraceInformation("ProcessQueueMessage start:  partitionKey {0} rowKey {1} Role Instance {2}.", partitionKey, rowKey, GetRoleInstance());
                // If this is a restart, verify that the email hasn't already been sent.
                if (restartFlag == "1")
                {
                    var retrieveOperationForRestart = TableOperation.Retrieve <SendEmail>(partitionKey, rowKey);
                    var retrievedResultForRestart   = messagearchiveTable.Execute(retrieveOperationForRestart);
                    var messagearchiveRow           = retrievedResultForRestart.Result as SendEmail;
                    if (messagearchiveRow != null)
                    {
                        // SendEmail row is in archive, so email is already sent.
                        // If there's a SendEmail Row in message table, delete it,
                        // and delete the queue message.
                        Trace.TraceInformation("Email already sent: partitionKey=" + partitionKey + " rowKey= " + rowKey);
                        var deleteOperation = TableOperation.Delete(new SendEmail {
                            PartitionKey = partitionKey, RowKey = rowKey, ETag = "*"
                        });
                        try
                        {
                            messageTable.Execute(deleteOperation);
                        }
                        catch (Exception ex)
                        {
                            aiClient.TrackException(ex);
                        }
                        sendEmailQueue.DeleteMessage(msg);
                        request.Properties.Add(new KeyValuePair <string, string>("SuccessCode", "NoOp-MessageAlreadySent"));
                        if (msg.InsertionTime != null)
                        {
                            request.Metrics.Add(new KeyValuePair <string, double>("EmailProcessingTimeMs", ((TimeSpan)(DateTimeOffset.Now - msg.InsertionTime)).Milliseconds));
                        }
                        RequestTelemetryHelper.DispatchRequest(request, requestTimer.Elapsed, true);
                        return;
                    }
                }
                // Get the row in the Message table that has data we need to send the email.
                var retrieveOperation      = TableOperation.Retrieve <SendEmail>(partitionKey, rowKey);
                var retrievedResult        = messageTable.Execute(retrieveOperation);
                var emailRowInMessageTable = retrievedResult.Result as SendEmail;
                if (emailRowInMessageTable == null)
                {
                    Trace.TraceError("SendEmail row not found:  partitionKey {0} rowKey {1} Role Instance {2}.", partitionKey, rowKey, GetRoleInstance());
                    request.Properties.Add(new KeyValuePair <string, string>("FailureCode", "SendEmailRowNotFound"));
                    if (msg.InsertionTime != null)
                    {
                        request.Metrics.Add(new KeyValuePair <string, double>("EmailProcessingTimeMs", ((TimeSpan)(DateTimeOffset.Now - msg.InsertionTime)).Milliseconds));
                    }
                    RequestTelemetryHelper.DispatchRequest(request, requestTimer.Elapsed, false);
                    return;
                }
                // Derive blob names from the MessageRef.
                var htmlMessageBodyRef = emailRowInMessageTable.MessageRef + ".htm";
                var textMessageBodyRef = emailRowInMessageTable.MessageRef + ".txt";
                // If the email hasn't already been sent, send email and archive the table row.
                if (emailRowInMessageTable.EmailSent != true)
                {
                    SendEmailToList(emailRowInMessageTable, htmlMessageBodyRef, textMessageBodyRef);

                    var emailRowToDelete = new SendEmail {
                        PartitionKey = partitionKey, RowKey = rowKey, ETag = "*"
                    };
                    emailRowInMessageTable.EmailSent = true;

                    var upsertOperation = TableOperation.InsertOrReplace(emailRowInMessageTable);
                    messagearchiveTable.Execute(upsertOperation);
                    var deleteOperation = TableOperation.Delete(emailRowToDelete);
                    messageTable.Execute(deleteOperation);
                }

                // Delete the queue message.
                sendEmailQueue.DeleteMessage(msg);
                Trace.TraceInformation("ProcessQueueMessage complete:  partitionKey {0} rowKey {1} Role Instance {2}.", partitionKey, rowKey, GetRoleInstance());
                request.Properties.Add(new KeyValuePair <string, string>("SuccessCode", "EmailSent"));
                if (msg.InsertionTime != null)
                {
                    request.Metrics.Add(new KeyValuePair <string, double>("EmailProcessingTimeMs", ((TimeSpan)(DateTimeOffset.Now - msg.InsertionTime)).Milliseconds));
                }
                RequestTelemetryHelper.DispatchRequest(request, requestTimer.Elapsed, true);
            }
            catch (Exception ex)
            {
                request.Properties.Add(new KeyValuePair <string, string>("FailureCode", "Exception"));
                if (msg.InsertionTime != null)
                {
                    request.Metrics.Add(new KeyValuePair <string, double>("FailedEmailProcessingTimeMs", ((TimeSpan)(DateTimeOffset.Now - msg.InsertionTime)).Milliseconds));
                }
                RequestTelemetryHelper.DispatchRequest(request, requestTimer.Elapsed, false);
                throw ex;
            }
        }
        private void ProcessQueueMessage(CloudQueueMessage msg)
        {
            // Log and delete if this is a "poison" queue message (repeatedly processed
            // and always causes an error that prevents processing from completing).
            // Production applications should move the "poison" message to a "dead message"
            // queue for analysis rather than deleting the message.
            if (msg.DequeueCount > 5)
            {
                Trace.TraceError("Deleting poison message:    message {0} Role Instance {1}.",
                                 msg.ToString(), GetRoleInstance());
                sendEmailQueue.DeleteMessage(msg);
                return;
            }
            // Parse message retrieved from queue.
            // Example:  2012-01-01,[email protected],0
            var messageParts = msg.AsString.Split(new char[] { ',' });
            var partitionKey = messageParts[0];
            var rowKey       = messageParts[1];
            var restartFlag  = messageParts[2];

            Trace.TraceInformation("ProcessQueueMessage start:  partitionKey {0} rowKey {1} Role Instance {2}.",
                                   partitionKey, rowKey, GetRoleInstance());
            // If this is a restart, verify that the email hasn't already been sent.
            if (restartFlag == "1")
            {
                var retrieveOperationForRestart = TableOperation.Retrieve <SendEmail>(partitionKey, rowKey);
                var retrievedResultForRestart   = messagearchiveTable.Execute(retrieveOperationForRestart);
                var messagearchiveRow           = retrievedResultForRestart.Result as SendEmail;
                if (messagearchiveRow != null)
                {
                    // SendEmail row is in archive, so email is already sent.
                    // If there's a SendEmail Row in message table, delete it,
                    // and delete the queue message.
                    Trace.TraceInformation("Email already sent: partitionKey=" + partitionKey + " rowKey= " + rowKey);
                    var deleteOperation = TableOperation.Delete(new SendEmail {
                        PartitionKey = partitionKey, RowKey = rowKey, ETag = "*"
                    });
                    try
                    {
                        messageTable.Execute(deleteOperation);
                    }
                    catch
                    {
                    }
                    sendEmailQueue.DeleteMessage(msg);
                    return;
                }
            }
            // Get the row in the Message table that has data we need to send the email.
            var retrieveOperation      = TableOperation.Retrieve <SendEmail>(partitionKey, rowKey);
            var retrievedResult        = messageTable.Execute(retrieveOperation);
            var emailRowInMessageTable = retrievedResult.Result as SendEmail;

            if (emailRowInMessageTable == null)
            {
                Trace.TraceError("SendEmail row not found:  partitionKey {0} rowKey {1} Role Instance {2}.",
                                 partitionKey, rowKey, GetRoleInstance());
                return;
            }
            // Derive blob names from the MessageRef.
            var htmlMessageBodyRef = emailRowInMessageTable.MessageRef + ".htm";
            var textMessageBodyRef = emailRowInMessageTable.MessageRef + ".txt";

            // If the email hasn't already been sent, send email and archive the table row.
            if (emailRowInMessageTable.EmailSent != true)
            {
                SendEmailToList(emailRowInMessageTable, htmlMessageBodyRef, textMessageBodyRef);

                var emailRowToDelete = new SendEmail {
                    PartitionKey = partitionKey, RowKey = rowKey, ETag = "*"
                };
                emailRowInMessageTable.EmailSent = true;

                var upsertOperation = TableOperation.InsertOrReplace(emailRowInMessageTable);
                messagearchiveTable.Execute(upsertOperation);
                var deleteOperation = TableOperation.Delete(emailRowToDelete);
                messageTable.Execute(deleteOperation);
            }

            // Delete the queue message.
            sendEmailQueue.DeleteMessage(msg);

            Trace.TraceInformation("ProcessQueueMessage complete:  partitionKey {0} rowKey {1} Role Instance {2}.",
                                   partitionKey, rowKey, GetRoleInstance());
        }
Exemplo n.º 6
0
        private void ProcessQueueMessage(object state)
        {
            CloudQueueMessage msg = state as CloudQueueMessage;

            try
            {
                // Log and delete if this is a "poison" queue message (repeatedly processed
                // and always causes an error that prevents processing from completing).
                // Production applications should move the "poison" message to a "dead message"
                // queue for analysis rather than deleting the message.
                if (msg.DequeueCount > 5)
                {
                    Trace.TraceError("Deleting poison message: message {0} Role Instance {1}.",
                                     msg.ToString(), GetRoleInstance());
                    DataModel.StorageFactory.Instance.IpcAzureAppWorkerJobQueue.DeleteMessage(msg);
                    return;
                }

                RmsCommand rmsCommand = new RmsCommand(msg.AsString);
                switch (rmsCommand.RmsOperationCommand)
                {
                case RmsCommand.Command.GetTemplate:
                {
                    ServicePrincipalModel   sp           = ServicePrincipalModel.GetFromStorage(rmsCommand.Parameters.First <object>().ToString());
                    RMS.RmsContentPublisher rmsPublisher = RMS.RmsContentPublisher.Create(sp.TenantId, sp.AppId, sp.Key);
                    var templates = rmsPublisher.GetRmsTemplates();
                    List <TemplateModel> templateEntityList = new List <TemplateModel>();
                    foreach (var temp in templates)
                    {
                        TemplateModel templateEntity = new TemplateModel();
                        templateEntity.TenantId            = sp.TenantId;
                        templateEntity.TemplateId          = temp.TemplateId;
                        templateEntity.TemplateName        = temp.Name;
                        templateEntity.TemplateDescription = temp.Description;
                        templateEntityList.Add(templateEntity);
                    }
                    TemplateModel.SaveToStorage(templateEntityList);
                    break;
                }

                case RmsCommand.Command.PublishTemplate:
                {
                    PublishModel          publishJob       = PublishModel.GetFromStorage(rmsCommand.Parameters[0].ToString(), rmsCommand.Parameters[1].ToString());
                    ServicePrincipalModel sp               = ServicePrincipalModel.GetFromStorage(rmsCommand.Parameters[0].ToString());
                    CloudBlockBlob        originalFileBlob = DataModel.StorageFactory.Instance.IpcAzureAppFileBlobContainer.GetBlockBlobReference(publishJob.OriginalFileBlobRef);

                    Stream sinkStream   = null;
                    string tempFilePath = null;

                    try
                    {
                        //if file length is less than 100,000 bytes, keep it in memory
                        if (publishJob.OriginalFileSizeInBytes < 100000)
                        {
                            sinkStream = new MemoryStream();
                        }
                        else
                        {
                            tempFilePath = Path.GetRandomFileName();
                            sinkStream   = new FileStream(tempFilePath, FileMode.Create);
                        }


                        using (Stream sourceStream = originalFileBlob.OpenRead())
                            using (sinkStream)
                            {
                                RMS.RmsContent rmsContent = new RMS.RmsContent(sourceStream, sinkStream);
                                rmsContent.RmsTemplateId = publishJob.TemplateId;
                                rmsContent.OriginalFileNameWithExtension = publishJob.OriginalFileName;
                                RMS.RmsContentPublisher rmsContentPublisher = RMS.RmsContentPublisher.Create(sp.TenantId, sp.AppId, sp.Key);
                                rmsContentPublisher.PublishContent(rmsContent);

                                publishJob.PublishedFileName = rmsContent.PublishedFileNameWithExtension;
                                sinkStream.Flush();
                                sinkStream.Seek(0, SeekOrigin.Begin);

                                //published file is uploaded to blob storage.
                                //Note: This sample code doesn't manage lifetime of this original and published file blob
                                //Actual code must manage the lifetime as appropriate
                                CloudBlockBlob destFileBlob = DataModel.StorageFactory.Instance.IpcAzureAppFileBlobContainer.GetBlockBlobReference(publishJob.PublishedFileBlobRef);
                                using (CloudBlobStream blobStream = destFileBlob.OpenWrite())
                                {
                                    int    tempSize   = 1024;
                                    byte[] tempBuffer = new byte[tempSize];
                                    while (true)
                                    {
                                        int readSize = sinkStream.Read(tempBuffer, 0, tempSize);
                                        if (readSize <= 0)
                                        {
                                            break;
                                        }

                                        blobStream.Write(tempBuffer, 0, readSize);
                                    }
                                    blobStream.Flush();
                                }
                            }

                        publishJob.JState = PublishModel.JobState.Completed.ToString();
                        publishJob.SaveToStorage();
                        break;
                    }
                    finally
                    {
                        if (!string.IsNullOrWhiteSpace(tempFilePath) && File.Exists(tempFilePath))
                        {
                            File.Delete(tempFilePath);
                        }
                    }
                }
                }

                //delete the message from the queue
                DataModel.StorageFactory.Instance.IpcAzureAppWorkerJobQueue.DeleteMessage(msg);
            }
            catch (Exception ex)
            {
                Process p   = Process.GetCurrentProcess();
                string  a   = p.ProcessName;
                string  b   = p.MainModule.FileName;
                string  err = ex.Message;
                if (ex.InnerException != null)
                {
                    err += " Inner Exception: " + ex.InnerException.Message;
                }
                if (msg != null)
                {
                    err += " Last queue message retrieved: " + msg.AsString;
                }
                Trace.TraceError(err);
            }
        }