/// <summary>
 /// Schedule next job for the specified campaign.
 /// </summary>
 /// <param name="schema">The <see cref="Terrasoft.Core.Campaign.CampaignSchema"/> object for wich Job plans.</param>
 /// <param name="fireTimeConfig">Fire time config for job.</param>
 public void ScheduleJob(CoreCampaignSchema schema, CampaignFireTimeConfig fireTimeConfig)
 {
     try {
         schema.CheckArgumentNull(nameof(schema));
         fireTimeConfig.CheckArgumentNull(nameof(fireTimeConfig));
         Guid campaignId        = schema.EntityId;
         var  campaignJobName   = GetCampaignJobName(campaignId, fireTimeConfig.Time);
         var  sysUserConnection = _userConnection.AppConnection.SystemUserConnection;
         var  parameters        = new Dictionary <string, object> {
             { "CampaignSchemaUId", schema.UId },
             { "ScheduledUtcFireTime", fireTimeConfig.Time },
             { "SchemaGeneratorStrategy", fireTimeConfig.ExecutionStrategy },
             { "ScheduledAction", (int)fireTimeConfig.ScheduledAction }
         };
         IJobDetail job = AppScheduler.CreateClassJob <CampaignJobExecutor>(campaignJobName,
                                                                            CampaignConsts.CampaignJobGroupName, sysUserConnection, parameters);
         ITrigger trigger = new SimpleTriggerImpl(campaignJobName + "Trigger",
                                                  CampaignConsts.CampaignJobGroupName, fireTimeConfig.Time)
         {
             MisfireInstruction = MisfireInstruction.IgnoreMisfirePolicy
         };
         AppScheduler.Instance.ScheduleJob(job, trigger);
         LogScheduledJob(schema.EntityId, fireTimeConfig);
     } catch (Exception ex) {
         string message = CampaignHelper.GetLczStringValue(nameof(CampaignJobDispatcher), "ScheduleException");
         Logger.ErrorFormat(message, ex, schema == null ? Guid.Empty : schema.EntityId);
         throw;
     }
 }
        private void AddToCampaignAudienceOrSendEmail(Guid campaignId, Guid landingStepId, Guid contactId,
                                                      int contactRId, Guid?nextStepId, string contactEmail)
        {
            Guid bulkEmailId;
            int  bulkEmailRId;

            try {
                if (IsSendNow(nextStepId, contactEmail, out bulkEmailId, out bulkEmailRId))
                {
                    var jobParameters = new Dictionary <string, object> {
                        { "ApplicationUrl", ApplicationUrl },
                        { "CampaignId", campaignId },
                        { "BulkEmailId", bulkEmailId },
                        { "BulkEmailRId", bulkEmailRId },
                        { "BulkEmailStepId", nextStepId },
                        { "ContactId", contactId },
                        { "ContactRId", contactRId },
                        { "ContactEmail", contactEmail }
                    };
                    CampaignHelper.CreateSendEmailImmediatelyJob(UserConnection, jobParameters);
                }
                else
                {
                    CampaignHelper.MergeContactIntoCampaign(UserConnection, campaignId, landingStepId, contactId);
                }
            } catch (Exception e) {
                _log.ErrorFormat("[CampaignLeadQualificationHelper.AddToCampaignAudienceOrSendEmail].", e);
            }
        }
Пример #3
0
        private void StopCampaignByScheduledDate(CoreCampaignSchema campaignSchema)
        {
            var campaignInfo = CampaignHelper.GetCampaignInfo(campaignSchema.EntityId);

            if (campaignInfo.CampaignStatusId == CampaignConsts.RunCampaignStatusId)
            {
                CampaignEventFacade.Finalize(_userConnection, campaignSchema);
                LogAction(campaignSchema.EntityId, CampaignConsts.CampaignLogTypeStoppedBySchedule);
                CampaignEventFacade.Stop(_userConnection, campaignSchema);
            }
        }
Пример #4
0
        private IEnumerable <string> GetReadableParts(TimeSpan span)
        {
            int days = (int)Math.Floor(span.TotalDays);

            yield return(days != 0 ? string.Format(CampaignHelper
                                                   .GetLczStringValue("CampaignJobExecutor", "DayPartInPeriod"), days) : string.Empty);

            yield return(span.Hours != 0 ? string.Format(CampaignHelper
                                                         .GetLczStringValue("CampaignJobExecutor", "HourPartInPeriod"), span.Hours) : string.Empty);

            yield return(span.Minutes != 0 ? string.Format(CampaignHelper
                                                           .GetLczStringValue("CampaignJobExecutor", "MinutePartInPeriod"), span.Minutes) : string.Empty);
        }
Пример #5
0
        private void CreateNotification(Guid campaignId, string notification)
        {
            var campaign = CampaignHelper.GetCampaign(campaignId, "Owner");
            var config   = new RemindingConfig(campaign.Schema.UId)
            {
                AuthorId    = _userConnection.CurrentUser.ContactId,
                ContactId   = campaign.OwnerId,
                SubjectId   = campaignId,
                Description = notification
            };

            RemindingUtilities.CreateReminding(_userConnection, config);
        }
 /// <summary>
 /// Schedule next job for the specified campaign.
 /// </summary>
 /// <param name="campaignSchemaUId">The unique identifier of campaign schema instance.</param>
 /// <param name="fireTimeConfig">Fire time config for job.</param>
 public void ScheduleJob(Guid campaignSchemaUId, CampaignFireTimeConfig fireTimeConfig)
 {
     try {
         var schemaManager = (CampaignSchemaManager)_userConnection.GetSchemaManager("CampaignSchemaManager");
         CoreCampaignSchema campaignSchema = schemaManager.GetSchemaInstance(campaignSchemaUId);
         ScheduleJob(campaignSchema, fireTimeConfig);
     } catch (Exception ex) {
         string message = CampaignHelper.GetLczStringValue(nameof(CampaignJobDispatcher),
                                                           "ScheduleBySchemaUIdException");
         Logger.ErrorFormat(message, ex, campaignSchemaUId);
         throw;
     }
 }
Пример #7
0
 /// <summary>
 /// Schedules next campaign job if it is possible.
 /// </summary>
 /// <param name="userConnection">The user connection.</param>
 /// <param name="campaignSchemaUId">Unique identifier of campaign schema.</param>
 /// <param name="scheduledDate">Time of the previous campaign run.
 /// It can be null when need calculate next fire time relatively to current time.</param>
 /// <returns>Returns true when campaign job is scheduled.</returns>
 public virtual bool TryScheduleNextJob(UserConnection userConnection, Guid campaignSchemaUId,
                                        DateTime?scheduledDate)
 {
     _userConnection = userConnection;
     try {
         var schemaManager =
             (CampaignSchemaManager)_userConnection.GetSchemaManager(nameof(CampaignSchemaManager));
         CoreCampaignSchema campaignSchema = schemaManager.GetSchemaInstance(campaignSchemaUId);
         return(TryScheduleNextJob(campaignSchema, scheduledDate));
     } catch (Exception e) {
         string message = string.Format(CampaignHelper
                                        .GetLczStringValue(nameof(CampaignJobExecutor), "ScheduleNextJobError"), campaignSchemaUId);
         Logger.Error(message, e);
         return(false);
     }
 }
        /// <summary>
        /// Remove scheduled jobs for the specified campaign.
        /// </summary>
        /// <param name="campaignId">The <see cref="Terrasoft.Core.Campaign.CampaignSchema"/> UId which job should be deleted.</param>
        public void RemoveJobs(Guid campaignId)
        {
            try {
                var groupMatcher = GroupMatcher <JobKey> .GroupContains(CampaignConsts.CampaignJobGroupName);

                var jobKeys      = AppScheduler.Instance.GetJobKeys(groupMatcher);
                var campaignJobs = jobKeys.Where(x => x.Name.Contains(campaignId.ToString()));
                foreach (var job in campaignJobs)
                {
                    AppScheduler.RemoveJob(job.Name, CampaignConsts.CampaignJobGroupName);
                }
            } catch (Exception ex) {
                string message = CampaignHelper.GetLczStringValue(nameof(CampaignJobDispatcher), "RemoveJobException");
                Logger.ErrorFormat(message, ex, campaignId);
                throw;
            }
        }
Пример #9
0
        private void LogMisfiredRun(CoreCampaignSchema campaignSchema,
                                    CampaignExecutionLatenessConfig latenessConfig, DateTime scheduledDate)
        {
            string message;

            switch (latenessConfig.Lateness)
            {
            case CampaignExecutionLateness.MisfiredTimeConditionElements: {
                string errorText = CampaignHelper
                                   .GetLczStringValue(nameof(CampaignJobExecutor), "MisfiredTimeConditionElementError");
                message = string.Format(errorText, scheduledDate.ToString("g"),
                                        TimeSpanToString(latenessConfig.LatenessTime),
                                        GetElementsNames(latenessConfig.MisfiredTimeConditionElements));
                LogError(campaignSchema.EntityId, CampaignConsts.CampaignLogTypeSkippedElement, message);
                break;
            }

            case CampaignExecutionLateness.Critical: {
                string errorText = CampaignHelper
                                   .GetLczStringValue(nameof(CampaignJobExecutor), "MisfiredCampaignExecutionError");
                message = string.Format(errorText, scheduledDate.ToString("g"),
                                        TimeSpanToString(latenessConfig.LatenessTime));
                LogError(campaignSchema.EntityId, CampaignConsts.CampaignLogTypeSkippedRun, message);
                break;
            }

            case CampaignExecutionLateness.CriticalAndMisfiredTimeConditionElements: {
                string errorText = CampaignHelper
                                   .GetLczStringValue(nameof(CampaignJobExecutor), "CriticalExecutionLatenessError");
                message = string.Format(errorText, TimeSpanToString(latenessConfig.LatenessTime),
                                        TimeSpanToString(TimeSpan.FromMinutes(campaignSchema.CriticalExecutionLateness)),
                                        GetElementsNames(latenessConfig.MisfiredTimeConditionElements));
                LogError(campaignSchema.EntityId, CampaignConsts.CampaignLogTypeCriticalLateness, message);
                string notificationText = CampaignHelper
                                          .GetLczStringValue(nameof(CampaignJobExecutor), "CriticalLatenessNotification");
                string notification = string.Format(notificationText, campaignSchema.Caption,
                                                    TimeSpanToString(TimeSpan.FromMinutes(campaignSchema.CriticalExecutionLateness)));
                CreateNotification(campaignSchema.EntityId, notification);
                break;
            }

            default:
                break;
            }
        }
        private bool IsSendNow(Guid?nextStepId, string contactEmail, out Guid bulkEmailId, out int bulkEmailRId)
        {
            bool result = false;

            if (nextStepId.HasValue)
            {
                KeyValuePair <Guid, int> bulkEmailInfo = CampaignHelper.GetImmediatelyTriggerBulkEmailIdentifierByStepId(
                    UserConnection, nextStepId.Value);
                bulkEmailId  = bulkEmailInfo.Key;
                bulkEmailRId = bulkEmailInfo.Value;
                result       = !bulkEmailId.Equals(Guid.Empty);
            }
            else
            {
                bulkEmailId  = Guid.Empty;
                bulkEmailRId = -1;
            }
            return(result);
        }
Пример #11
0
        /// <summary>
        /// Processes job for campaign action.
        /// <param name="userConnection">The user connection.</param>
        /// <param name="parameters">Job parameters.</param>
        /// </summary>
        public void Execute(UserConnection userConnection, IDictionary <string, object> parameters)
        {
            try {
                _userConnection = userConnection;
                var schemaUid               = GetTypedParameter <Guid>("CampaignSchemaUId", parameters);
                var scheduledFireTime       = GetTypedParameter <DateTime>("ScheduledUtcFireTime", parameters);
                var action                  = (CampaignScheduledAction)GetTypedParameter <int>("ScheduledAction", parameters);
                var schemaGeneratorStrategy =
                    GetTypedParameter <CampaignSchemaExecutionStrategy>("SchemaGeneratorStrategy", parameters);
                var schemaManager =
                    (CampaignSchemaManager)_userConnection.GetSchemaManager("CampaignSchemaManager");
                CoreCampaignSchema campaignSchema = schemaManager.GetSchemaInstance(schemaUid);
                switch (action)
                {
                case CampaignScheduledAction.ScheduledStart:
                    StartCampaign(campaignSchema, scheduledFireTime);
                    break;

                case CampaignScheduledAction.Start:
                    RunCampaign(campaignSchema, schemaGeneratorStrategy, scheduledFireTime);
                    break;

                case CampaignScheduledAction.Run:
                    RunCampaignWithInProgress(campaignSchema, schemaGeneratorStrategy, scheduledFireTime);
                    break;

                case CampaignScheduledAction.Stop:
                    CampaignEventFacade.Stop(_userConnection, campaignSchema);
                    break;

                case CampaignScheduledAction.ScheduledStop:
                    StopCampaignByScheduledDate(campaignSchema);
                    break;

                default:
                    break;
                }
            } catch (Exception ex) {
                string message = CampaignHelper.GetLczStringValue(nameof(CampaignJobExecutor), "ExecutionException");
                Logger.Error(message, ex);
                throw;
            }
        }
Пример #12
0
 private void RunCampaign(CoreCampaignSchema campaignSchema,
                          CampaignSchemaExecutionStrategy schemaGeneratorStrategy, DateTime scheduledFireTime)
 {
     try {
         DateTime?stopDate       = GetScheduledStopDate(campaignSchema.EntityId);
         var      latenessConfig = CampaignTimeScheduler.GetLatenessConfig(campaignSchema, scheduledFireTime);
         LogMisfiredRun(campaignSchema, latenessConfig, scheduledFireTime);
         if (stopDate <= DateTime.UtcNow)
         {
             LogAction(campaignSchema.EntityId, CampaignConsts.CampaignLogTypeStoppedBySchedule);
         }
         if (latenessConfig.Lateness == CampaignExecutionLateness.CriticalAndMisfiredTimeConditionElements ||
             stopDate <= DateTime.UtcNow)
         {
             CampaignEventFacade.Finalize(_userConnection, campaignSchema);
             CampaignEventFacade.Stop(_userConnection, campaignSchema);
             return;
         }
         if (latenessConfig.Lateness == CampaignExecutionLateness.NoMisfire)
         {
             campaignSchema.CampaignConfiguration["ScheduledUtcFireTime"] = scheduledFireTime;
             var config = new CampaignExecutionConfig {
                 CurrentFireTime   = scheduledFireTime,
                 ExecutionStrategy = schemaGeneratorStrategy
             };
             CampaignEngine.Run(campaignSchema, config);
         }
         else
         {
             scheduledFireTime = DateTime.UtcNow;
         }
     } catch (Exception e) {
         string message = CampaignHelper.GetLczStringValue(nameof(CampaignJobExecutor), "ExecutionException");
         Logger.Error(message, e);
     }
     TryScheduleNextJob(campaignSchema, scheduledFireTime);
 }
        protected virtual void AddLeadContactToCampaigns(Guid leadId)
        {
            Guid   landingId;
            Guid   contactId;
            int    contactRId;
            string contactEmail;

            if (IsLeadFromLandingAndHaveContact(leadId, out landingId, out contactId, out contactRId,
                                                out contactEmail))
            {
                Dictionary <Guid, Guid> campaignList =
                    CampaignHelper.GetCampaignListByLandingId(UserConnection, landingId);
                var campaignStepsHandler = ClassFactory.Get <CampaignStepsHandler>(
                    new ConstructorArgument("userConnection", UserConnection));
                foreach (var campaign in campaignList)
                {
                    Guid campaignId    = campaign.Key;
                    Guid landingStepId = campaign.Value;
                    Guid?nextStepId    = campaignStepsHandler.GetCampaignSecondStepId(campaignId);
                    AddToCampaignAudienceOrSendEmail(campaignId, landingStepId, contactId, contactRId, nextStepId,
                                                     contactEmail);
                }
            }
        }