/// <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;
     }
 }
Exemplo n.º 2
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);
        }
 /// <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;
     }
 }
Exemplo n.º 4
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;
            }
        }
Exemplo n.º 6
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;
            }
        }
Exemplo n.º 7
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;
            }
        }
Exemplo n.º 8
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);
 }