/// <summary> /// Handles the Merchant Email job /// </summary> /// <param name="emailCargo">Merchant Email cargo</param> public void Handle(EmailCargo emailCargo) { MerchantReportEmailCargo merchantReportEmailCargo = emailCargo as MerchantReportEmailCargo; if (merchantReportEmailCargo != null) { EmailData emailData = _emailContentCreator.GetContent(merchantReportEmailCargo); if (emailData != null) { bool isTest = (merchantReportEmailCargo.Hints != null) && merchantReportEmailCargo.Hints.IsTest; EmailContent emailContent = new EmailContent { From = isTest ? this._emailFromAddressTestAccount : this._merchantEmailFromAddress, FromDisplay = this._emailFromDisplay, Subject = emailData.Subject, HtmlBody = emailData.HtmlBody, TextBody = emailData.TextBody, Category = merchantReportEmailCargo.Campaign }; SendEmailRequest request = new SendEmailRequest { Content = emailContent, ToList = new List <string> { merchantReportEmailCargo.EmailAddress }, IsTest = isTest }; // Send the email Log.Verbose("Sending email for the email job : {0}", emailCargo.ToString()); this._userServicesClient.SendEmail(merchantReportEmailCargo.Id, request, null); } JobFetcher.CompleteJob(merchantReportEmailCargo.Id); } else { Log.Error("Invalid handler for the email job : {0}", emailCargo.ToString()); } }
/// <summary> /// The is send time window valid. /// </summary> /// <param name="lastEmailEntity"> /// The last email entity. /// </param> /// <param name="emailCargo"> /// The job. /// </param> /// <returns> /// The <see cref="bool"/>. /// </returns> private bool IsSendTimeWindowValid(UserEmailEntity lastEmailEntity, EmailCargo emailCargo) { bool isSendTimeWindowValid = true; if (lastEmailEntity != null && lastEmailEntity.EmailDate > (DateTime.UtcNow - this.windowsBetweenUserEmails)) { Log.Error( "Can't send deals email due to time windows constraint. Job Details=[{0}]; Last Email Date={1}; Window Between Emails={2}", emailCargo.ToString(), lastEmailEntity.EmailDate, this.windowsBetweenUserEmails); isSendTimeWindowValid = false; } return(isSendTimeWindowValid); }
/// <summary> /// Dequeues the job from email jobs queue and passes it to the appropriate job handler /// </summary> private bool ProcessRegularEmailJob() { EmailCargo emailCargo = null; bool foundJob = false; try { if (this._jobsQueue.TryDequeue(out emailCargo)) { foundJob = true; if (_jobHandlers.ContainsKey(emailCargo.GetType())) { object handler = _jobHandlers[emailCargo.GetType()]; Log.Verbose("Start Processing Job. Agent Id: {0}; JobInfo : {1} ", this._agentId, emailCargo.ToString()); if (handler is IEmailJobHandler) { (handler as IEmailJobHandler).Handle(emailCargo); Log.Verbose("Completed Processing Job. Agent Id: {0}; JobInfo : {1} ", this._agentId, emailCargo.ToString()); } else { Log.Error(string.Format("Invalid handler associated with the Email Job of type {0}", emailCargo.GetType())); } } else { Log.Error(string.Format("No handler associated with the Email Job of type {0}", emailCargo.GetType())); } } else { // No jobs in the queue. Log.Verbose("No jobs in the email jobs queue"); } } catch (ModelContentException modelContentException) { Log.Warn(string.Format("Can't generate email model. Agent Id={0}; Job Details=[{1}]; Error={2}", this._agentId, emailCargo, modelContentException)); } catch (StorageException storageClientException) { this.HandleError(EventCode.EmailAgentStorageAccessError, storageClientException, "Error Access jobs or outbound message store", this._agentId, emailCargo); } catch (JsonException jsonException) { this.HandleError(EventCode.EmailAgentJsonSerializationError, jsonException, "Serialization Error", this._agentId, emailCargo); } catch (TemplateRenderException renderException) { this.HandleError(EventCode.EmailAgentTemplateRenderingError, renderException, "Couldn't Render the data into the template", this._agentId, emailCargo); } catch (Exception exp) { this.HandleError(EventCode.EmailAgentUnexpectedError, exp, "Unexpected Error", this._agentId, emailCargo); } return(foundJob); }