/// <summary>
        /// Returns query for audience of campaign that should be moved to bulk email.
        /// </summary>
        /// <param name="userConnection">Instance of the <see cref="Terrasoft.Core.UserConnection"/>.</param>
        /// <param name="campaignId">Unique identifier of the Campaign.</param>
        /// <param name="bulkEmailId">Unique identifier of the BulkEmail.</param>
        /// <param name="stepId">Unique identifier of the CampaignStep.</param>
        public static Select GetCampaignAudienceToBulkEmailSelect(UserConnection userConnection, Guid campaignId,
                                                                  Guid bulkEmailId, Guid stepId)
        {
            var bulkEmailRId             = BulkEmailQueryHelper.GetBulkEmailRId(bulkEmailId, userConnection);
            var unixTimestamp            = Utilities.ConvertDateTimeToTimestamp(DateTime.UtcNow);
            var unsubscribeSubSelect     = GetBulkEmailUnsubscribeSelect(userConnection, bulkEmailId);
            var existingRecipientsSelect = GetExistingRecipientsSelect(userConnection);
            var mainQuery = new Select(userConnection)
                            .Column("main", "BulkEmailRId")
                            .Column("main", "ContactRId")
                            .Column("main", "EmailAddress")
                            .Column(Column.Const(unixTimestamp)).As("Timestamp")
                            .From(new Select(userConnection)
                                  .Column(Column.Const(bulkEmailRId)).As("BulkEmailRId")
                                  .Column("C", "Id").As("ContactId")
                                  .Column("C", "RId").As("ContactRId")
                                  .Column("C", "Email").As("EmailAddress")
                                  .Column(Column.Const(unixTimestamp)).As("Timestamp")
                                  .From("CampaignTarget").As("CT")
                                  .InnerJoin("Contact").As("C").On("CT", "ContactId").IsEqual("C", "Id")
                                  .Where("CT", "CampaignId").IsEqual(Column.Parameter(campaignId))
                                  .And("CT", "NextStepId").IsEqual(Column.Parameter(stepId)) as Select).As("main")
                            .Where().Not().Exists(existingRecipientsSelect)
                            .And().Not().Exists(unsubscribeSubSelect) as Select;

            mainQuery.SpecifyNoLockHints();
            return(mainQuery);
        }
        private void SetInvalidBulkEmailCounters(Guid bulkEmailId, int invalidAddresseeCount)
        {
            var actualInvalidAdresseeExpression =
                MailingDbUtilities.GetAddCountColumnExpression("InvalidAddresseeCount", invalidAddresseeCount);

            BulkEmailQueryHelper.UpdateBulkEmail(bulkEmailId, UserConnection,
                                                 new KeyValuePair <string, object>("SendDueDate", Column.Const(null)),
                                                 new KeyValuePair <string, object>("InvalidAddresseeCount", actualInvalidAdresseeExpression));
        }
예제 #3
0
        private Select ApplyBulkEmailInfoToAudienceSelect(Select audienceSelect, Guid bulkEmailId, Guid sessionUId)
        {
            var bulkEmailRId = BulkEmailQueryHelper.GetBulkEmailRId(bulkEmailId, _userConnection);
            var select       = audienceSelect
                               .Column(Column.Parameter(bulkEmailRId)).As("BulkEmailRId")
                               .Column(Column.Parameter(sessionUId)).As("SessionUId")
                               .Column(Column.Const(false)).As("IsSent")
                               .Column(Column.Parameter(Utilities.ConvertDateTimeToTimestamp(DateTime.UtcNow))).As("Timestamp");

            return(select);
        }
        protected void ExecuteSendMessagePostProcessing(SendMessageData sendMessageData,
                                                        List <TypedCounter <MailingResponseCode> > sendResults, SendMessageTaskData sendMessageTaskData)
        {
            var bulkEmail                     = sendMessageData.BulkEmail;
            var sessionId                     = sendMessageData.SessionId;
            var blankEmailCount               = sendResults.Sum("BlankEmail");
            var doNotUseEmailCount            = sendResults.Sum("DoNotUseEmail");
            var incorrectEmailCount           = sendResults.Sum("IncorrectEmail");
            var unreachableEmailCount         = sendResults.Sum("UnreachableEmail");
            var communicationLimitCount       = sendResults.Sum("CommunicationLimit");
            var duplicateEmailCount           = sendResults.Sum("DuplicateEmail");
            var templateNotFoundCount         = sendResults.Sum("TemplateNotFound");
            var sendersDomainNotVerifiedCount = sendResults.Sum("SendersDomainNotVerified");
            var sendersNameNotValidCount      = sendResults.Sum("SendersNameNotValid");
            var invalidAddresseeCount         = sendResults.Sum("InvalidAddressee");

            if (sendMessageTaskData.IsBreaking)
            {
                SetBulkEmailStatus(bulkEmail.Id, MailingConsts.BulkEmailStatusStoppedId);
                BulkEmailEventLogger.LogInfo(bulkEmail.Id, DateTime.UtcNow, GetLczStringValue("MailingStoppedManually"),
                                             GetLczStringValue("MailingStoppedManuallyDescription"), UserConnection.CurrentUser.ContactId);
                SetInvalidBulkEmailCounters(bulkEmail.Id, invalidAddresseeCount);
                CreateReminding(bulkEmail, "CESStoppedMailingMsg");
                MailingUtilities.Log.InfoFormat("BulkEmail with Id: {0} Was stopped manually.", bulkEmail.Id);
                return;
            }
            BulkEmailQueryHelper.UpdateBulkEmail(bulkEmail.Id, UserConnection,
                                                 new KeyValuePair <string, object>("SendDueDate", DateTime.UtcNow),
                                                 new KeyValuePair <string, object>("BlankEmailCount", blankEmailCount),
                                                 new KeyValuePair <string, object>("DoNotUseEmailCount", doNotUseEmailCount),
                                                 new KeyValuePair <string, object>("IncorrectEmailCount", incorrectEmailCount),
                                                 new KeyValuePair <string, object>("UnreachableEmailCount", unreachableEmailCount),
                                                 new KeyValuePair <string, object>("CommunicationLimitCount", communicationLimitCount),
                                                 new KeyValuePair <string, object>("DuplicateEmailCount", duplicateEmailCount),
                                                 new KeyValuePair <string, object>("TemplateNotFoundCount", templateNotFoundCount),
                                                 new KeyValuePair <string, object>("SendersDomainNotVerifiedCount", sendersDomainNotVerifiedCount),
                                                 new KeyValuePair <string, object>("SendersNameNotValidCount", sendersNameNotValidCount),
                                                 new KeyValuePair <string, object>("InvalidAddresseeCount", invalidAddresseeCount));
            MailingUtilities.Log.InfoFormat(
                "[CESMaillingProvider.ExecuteSendMessage]: Finished: BulkEmail.Id: {0}, SessionId: {1}", bulkEmail.Id,
                sessionId);
            if (sendMessageTaskData.ProcessedGroupsCounter == 0)
            {
                SetBulkEmailStatus(bulkEmail.Id, MailingConsts.BulkEmailStatusErrorId);
                CreateReminding(bulkEmail, "CESNoRecipientsMsg");
                MailingUtilities.Log.InfoFormat("BulkEmail with Id: {0} Has no recipients.", bulkEmail.Id);
            }
        }
        public override MailingResponse ExecuteState()
        {
            var bulkEmailEntity = Context.BulkEmailEntity;
            var bulkEmailId     = bulkEmailEntity.PrimaryColumnValue;

            SetBulkEmailStatus(bulkEmailId, MailingConsts.BulkEmailStatusWaitingBeforeSendId);
            PerformWaitBeforeSend();
            var currentBulkEmailStatus = BulkEmailQueryHelper.GetBulkEmailStatus(bulkEmailId, Context.UserConnection);

            if (currentBulkEmailStatus == MailingConsts.BulkEmailStatusStoppedId)
            {
                MailingUtilities.Log.InfoFormat(
                    "[CESMaillingProvider.ExecuteSendMessageTask]: Break iterations. " +
                    "bulkEmilId {0} has status 'Stopped'.", bulkEmailId);
                return(new MailingResponse {
                    Success = true
                });
            }
            InitializeAudienceDataSource(bulkEmailId);
            MailingUtilities.Log.InfoFormat(
                "[CESMaillingProvider.CreateSendAction]: Start action for send. " + "bulkEmilId: {0}", bulkEmailId);
            try {
                var bulkEmail = (BulkEmail)bulkEmailEntity;
                ResolveRecipientsReplica(bulkEmailId);
                var templateReplicas = CreateTemplates(bulkEmail, BulkEmailMacroParser).ToList();
                foreach (var template in templateReplicas)
                {
                    SaveTemplate(template, bulkEmail);
                }
                PrepareRecipientsMacros(bulkEmail, templateReplicas);
            } catch (Exception e) {
                SetBulkEmailStatus(bulkEmailId, MailingConsts.BulkEmailStatusErrorId);
                MailingUtilities.Log.ErrorFormat(
                    "[CESMaillingProvider.ExecuteSendMessage]: " + "Error while saving template. BulkEmailId {0}.", e,
                    bulkEmailId);
                BulkEmailEventLogger.LogError(bulkEmailId, DateTime.UtcNow, GetLczStringValue("BatchSendEvent"), e,
                                              GetLczStringValue("TemplateSaveErrorMsg"), Context.UserConnection.CurrentUser.ContactId);
                CreateReminding(Context.BulkEmailEntity, "CESTemplateFailsMsg");
                throw;
            }
            return(new MailingResponse {
                Success = true
            });
        }
        private void InitBulkEmailCounters(Guid bulkEmailId, bool isResend)
        {
            var recipientCount = 0;

            try {
                recipientCount = BulkEmailQueryHelper.GetRecipientsInQueueCount(bulkEmailId, UserConnection);
                BulkEmailQueryHelper.UpdateBulkEmail(bulkEmailId, UserConnection,
                                                     new KeyValuePair <string, object>("InQueueCount", recipientCount),
                                                     new KeyValuePair <string, object>("RecipientCount", recipientCount),
                                                     new KeyValuePair <string, object>("NotDeliveredCount", 0),
                                                     new KeyValuePair <string, object>("TemplateNotFoundCount", 0),
                                                     new KeyValuePair <string, object>("CanceledCount", 0));
            } catch (Exception e) {
                MailingUtilities.Log.ErrorFormat(
                    "[CESMaillingProvider.InitBulkEmailCounters]: Error while init BulkEmail counters with Id: {0}," +
                    " isResent={1}, recipientsCount={2}", e, bulkEmailId, isResend, recipientCount);
                throw;
            }
        }
        private void ExecuteSendMessageTask(SendMessageData sourceData)
        {
            var bulkEmail           = sourceData.BulkEmail;
            var bulkEmailRId        = sourceData.BulkEmailRId;
            var sessionId           = sourceData.SessionId;
            var culture             = Thread.CurrentThread.CurrentCulture;
            var sendMessageTaskData = new SendMessageTaskData();
            var sendResults         = new List <TypedCounter <MailingResponseCode> >();

            _audienceDataSource.PageNumber = 0;
            while (sendMessageTaskData.HasDataToProceed)
            {
                _audienceDataSource.PageNumber++;
                sendMessageTaskData.BatchId = _audienceDataSource.PageNumber;
                MailingUtilities.Log.InfoFormat(
                    "[CESMaillingProvider.ExecuteSendMessageTask]: Set BatchId - {0}. " +
                    "BulkEmail.Id {1}, Session {2}.", sendMessageTaskData.BatchId, bulkEmail.Id, sessionId);
                var messageData = sourceData.Clone();
                messageData.BulkEmail = sourceData.BulkEmail;
                var email = InitDCEmailMessage(messageData);
                messageData.BatchId     = sendMessageTaskData.BatchId;
                messageData.BatchLength = MassMailingRecipientsLimit;
                List <IMessageRecipientInfo> recipients;
                var nextBatchLength = 0;
                try {
                    email.InitRecipientVariable();
                    var startGetRecipientTime = DateTime.UtcNow;
                    recipients = GetRecipients();
                    RecipientsValidator.Validate(recipients);
                    LogRecipientState(recipients, messageData.BulkEmail.Id, messageData.BatchId, startGetRecipientTime);
                    var validRecipients       = GetValidRecipient(recipients);
                    var recipientEmailAddress = CreateEmailAddresses(validRecipients);
                    email.to        = recipientEmailAddress;
                    nextBatchLength = recipients.Count;
                    MailingUtilities.Log.InfoFormat(
                        "[CESMaillingProvider.ExecuteSendMessageTask]: Prepared {0} " +
                        "recipients. BulkEmail.Id {1}, BatchId {2}, SessionId: {3}", nextBatchLength, bulkEmail.Id,
                        sendMessageTaskData.BatchId, sessionId);
                } catch (SqlException e) {
                    MailingUtilities.Log.ErrorFormat(
                        "[CESMaillingProvider.GetRecipients]: Group processing fails. BulkEmailRId: {0}, " +
                        "SessionId: {1}. GroupId: {2}.", e, messageData.BulkEmailRId, messageData.SessionId,
                        messageData.BatchId);
                    BulkEmailEventLogger.LogError(messageData.BulkEmail.Id, DateTime.UtcNow,
                                                  GetLczStringValue("AudienceAddEvent"), e, GetLczStringValue("BatchProcessingErrorMsg"),
                                                  UserConnection.CurrentUser.ContactId, messageData.BatchId);
                    sendMessageTaskData.RetryMessageProcessing();
                    if (!sendMessageTaskData.HasDataToProceed)
                    {
                        MailingUtilities.Log.InfoFormat(
                            "[CESMaillingProvider.ExecuteSendMessage]: Terminated. Failed groups limit reached. " +
                            "BulkEmail.Id: {0}, SessionId: {1}", bulkEmailRId, sessionId);
                    }
                    continue;
                } catch (Exception exception) {
                    MailingUtilities.Log.ErrorFormat(
                        "[CESMaillingProvider.GetRecipients]: Group processing fails. BulkEmailRId: {0}, " +
                        "SessionId: {1}. GroupId: {2}.", exception, messageData.BulkEmailRId, messageData.SessionId,
                        messageData.BatchId);
                    BulkEmailEventLogger.LogError(messageData.BulkEmail.Id, DateTime.UtcNow,
                                                  GetLczStringValue("AudienceAddEvent"), exception, GetLczStringValue("BatchProcessingErrorMsg"),
                                                  UserConnection.CurrentUser.ContactId, messageData.BatchId);
                    throw;
                }
                sendMessageTaskData.LastFailedGroupId = 0;
                sendMessageTaskData.CheckMessageCanProceed(nextBatchLength);
                BulkEmailQueryHelper.ResetBulkEmailCounters(sourceData.BulkEmailRId, UserConnection);
                if (sendMessageTaskData.HasDataToProceed)
                {
                    messageData.MailingStartTS = GetMailingStartTs(bulkEmail);
                    MailingUtilities.Log.InfoFormat(
                        "[CESMaillingProvider.ExecuteSendMessageTask]: Has data to " +
                        "proceed and start new thread. BulkEmail.Id {0}, BatchId {1}, SessionId: {2}", bulkEmail.Id,
                        sendMessageTaskData.BatchId, sessionId);
                    try {
                        Thread.CurrentThread.CurrentCulture = culture;
                        var resultsCounter = CreateResultsCounter();
                        messageData.EmailMessage = email;
                        var sentResult = true;
                        if (email.to.Any())
                        {
                            sentResult = SendBatch(messageData);
                        }
                        if (!sentResult)
                        {
                            recipients.Where(x => x.InitialResponseCode == (int)BulkEmailResponseCode.Sent).ForEach(y =>
                                                                                                                    y.InitialResponseCode = (int)MailingResponseCode.RequestFailed);
                        }
                        UpdateSendEmailIterationResult(messageData, recipients, resultsCounter);
                        sendResults.Add(resultsCounter);
                    } catch (Exception e) {
                        MailingUtilities.Log.ErrorFormat(
                            "[CESMaillingProvider.ExecuteSendMessageTask]: Error occurs when sends batch." +
                            " BulkEmailRId: {0}, SessionId: {1}. GroupId: {2}.", e, messageData.BulkEmailRId,
                            messageData.SessionId, messageData.BatchId);
                    }
                }
                else
                {
                    MailingUtilities.Log.InfoFormat(
                        "[CESMaillingProvider.ExecuteSendMessageTask]: Has no data to " +
                        "proceed and BatchSendThreadPool released. BulkEmail.Id {0}, BatchId {1}, SessionId: {2}",
                        bulkEmail.Id, sendMessageTaskData.BatchId, sessionId);
                }
            }
            ExecuteSendMessagePostProcessing(sourceData, sendResults, sendMessageTaskData);
        }