示例#1
0
        /// <summary>
        /// Executes the confirmation email job
        /// </summary>
        /// <param name="priorityEmailCargo">Cargo for confirmation email</param>
        public void Handle(PriorityEmailCargo priorityEmailCargo)
        {
            ConfirmationEmailCargo confirmationEmailCargo = priorityEmailCargo as ConfirmationEmailCargo;

            if (confirmationEmailCargo != null)
            {
                Log.Verbose("Starting Confirmation email job : {0}", confirmationEmailCargo.ToString());
                EnvironmentType azureEnvironment;
                if (Enum.TryParse(_environment, true, out azureEnvironment))
                {
                    EmailConfirmationContract emailConfirmationContract = new EmailConfirmationContract();
                    EmailRenderingClient <EmailConfirmationContract> emailRenderingClient = new EmailRenderingClient <EmailConfirmationContract>();
                    switch (confirmationEmailCargo.EntityType)
                    {
                    case EntityType.AuthenticatedEmailAddress:
                        emailConfirmationContract.ConfirmationUrl     = ConfirmationLinkGenerator.GetLink(confirmationEmailCargo.UserIdHash, confirmationEmailCargo.ConfirmationCode, EntityType.AuthenticatedEmailAddress, azureEnvironment).AbsoluteUri;
                        emailRenderingClient.EmailRenderingServiceUrl = this._authenticatedEmailConfirmationTemplateUrl;
                        break;

                    case EntityType.UnAuthenticatedEmailAddress:
                        emailConfirmationContract.ConfirmationUrl     = ConfirmationLinkGenerator.GetLink(confirmationEmailCargo.UserIdHash, confirmationEmailCargo.ConfirmationCode, EntityType.UnAuthenticatedEmailAddress, azureEnvironment).AbsoluteUri;
                        emailRenderingClient.EmailRenderingServiceUrl = this._unauthenticatedEmailConfirmationTemplateUrl;
                        break;

                    case EntityType.AccountLink:
                        emailConfirmationContract.ConfirmationUrl     = ConfirmationLinkGenerator.GetLink(confirmationEmailCargo.UserIdHash, confirmationEmailCargo.ConfirmationCode, EntityType.AccountLink, azureEnvironment).AbsoluteUri;
                        emailRenderingClient.EmailRenderingServiceUrl = this._linkEmailAccountTemplateUrl;
                        break;

                    default:
                        throw new InvalidEntityTypeException("Unknown entity type in Confirmation Emailcargo");
                    }
                    EmailContent emailContent = new EmailContent
                    {
                        From        = this._confirmationFromAddress,
                        FromDisplay = this._confirmationFromDisplayName,
                        Subject     = confirmationEmailCargo.EntityType == EntityType.AccountLink ? this._alinkConfirmationSubject : this._confirmationEmailSubject,
                        HtmlBody    = emailRenderingClient.RenderHtml(emailConfirmationContract),
                        Category    = this._confirmationEmailCategory
                    };
                    SendEmailRequest request = new SendEmailRequest
                    {
                        Content = emailContent,
                        ToList  = new List <string> {
                            confirmationEmailCargo.EmailAddress
                        }
                    };

                    Log.Verbose("Sending email for the email job : {0}", confirmationEmailCargo.ToString());

                    // Send the email
                    this._userServicesClient.SendEmail(confirmationEmailCargo.Id, request, null);

                    Log.Verbose("Successfully sent email for the email job : {0}", confirmationEmailCargo.ToString());
                }
            }
            else
            {
                Log.Error("Error in executing confirmation email job : Invalid priority email cargo");
            }
        }
        /// <summary>
        /// Dequeues the job from priority email jobs queue and passes it to the appropriate job handler
        /// </summary>
        private bool ProcessPriorityEmailJob()
        {
            PriorityEmailCargo   priorityEmailCargo   = null;
            PriorityQueueMessage priorityQueueMessage = null;
            bool foundJob = false;

            try
            {
                if (this._priorityEmailJobsQueue.TryDequeue(out priorityQueueMessage))
                {
                    foundJob = true;
                    Type emailCargoType = priorityQueueMessage.EmailCargo.GetType();
                    if (_jobHandlers.ContainsKey(emailCargoType))
                    {
                        priorityEmailCargo = priorityQueueMessage.EmailCargo;
                        object handler = _jobHandlers[emailCargoType];
                        Log.Verbose("Start Processing Job. Agent Id: {0}; JobInfo : {1} ", this._agentId,
                                    priorityEmailCargo.ToString());

                        if (handler is IPriorityEmailJobHandler)
                        {
                            (handler as IPriorityEmailJobHandler).Handle(priorityQueueMessage.EmailCargo);
                            Log.Verbose("Completed Processing Job. Agent Id: {0}; JobInfo : {1} ", this._agentId,
                                        priorityEmailCargo.ToString());

                            //Delete the job from the priority email jobs queue
                            this._priorityEmailJobsQueue.Delete(priorityQueueMessage.MessageId, priorityQueueMessage.PopReceipt);
                            Log.Verbose("Removed message from the priority-email jobs queue. Message Info : {0} ",
                                        priorityQueueMessage.ToString());
                        }
                        else
                        {
                            Log.Error(string.Format("Invalid handler associated with the Email Job of type {0}",
                                                    emailCargoType));
                        }
                    }
                    else
                    {
                        Log.Error(string.Format("No handler associated with the Email Job of type {0}", emailCargoType));
                    }
                }
                else
                {
                    // No jobs in the queue.
                    Log.Verbose("No jobs in the priority email jobs queue.");
                }
            }
            catch (InvalidEntityTypeException entityTypeException)
            {
                if (priorityQueueMessage != null)
                {
                    //If it's an invalid entity in the message, we will never be able to process this.
                    //Delete the job from the priority email jobs queue
                    this._priorityEmailJobsQueue.Delete(priorityQueueMessage.MessageId, priorityQueueMessage.PopReceipt);
                }
                this.HandleError(EventCode.EmailAgentUnexpectedError, entityTypeException, "Unexpected Error", this._agentId, priorityEmailCargo);
            }
            catch (JsonException jsonException)
            {
                this.HandleError(EventCode.EmailAgentJsonSerializationError, jsonException, "Serialization Error", this._agentId, priorityEmailCargo);
            }
            catch (Exception exp)
            {
                this.HandleError(EventCode.EmailAgentUnexpectedError, exp, "Unexpected Error", this._agentId, priorityEmailCargo);
            }

            return(foundJob);
        }