Ejemplo n.º 1
0
            public string ToReportingLine()
            {
                StringBuilder sb = new StringBuilder();

                sb.Append(this.Task.GetType().Name);
                if (this.Task is ScheduledTask && (this.Task as ScheduledTask).Pipeline.Count > 1)
                {
                    sb.Append($" (pipeline of {(this.Task as ScheduledTask).Pipeline.Count} items)");
                }
                sb.Append($", schedule: {CrontabSchedule.GetDescription((Task as ScheduledTask).Schedule)}");
                if (IsRunning)
                {
                    sb.Append(", ** is running **");
                }
                else
                {
                    sb.Append($", will next run at {NextRunTime.ToLocalTime().ToDefaultWithTime()}");
                }
                return(sb.ToString());
            }
Ejemplo n.º 2
0
        private void InitialiseScheduledTasks()
        {
            IEnumerable <IScheduledTask> scheduledTasks = sp.GetServices <ScheduledTask>();

            foreach (var st in scheduledTasks)
            {
                var scheduledTask   = st as ScheduledTask;
                var serviceSchedule = options.Schedules?.FirstOrDefault(sc => string.Compare(sc.Name, scheduledTask.GetType().Name) == 0);
                if (serviceSchedule == null)
                {
                    log.Error($"{scheduledTask.GetType().Name}, service schedule not found - check SchedulerOptions");
                    continue;
                }
                if (serviceSchedule.Enabled)
                {
                    var referenceTime = DateTime.UtcNow;
                    scheduledTask.Schedule = serviceSchedule.Schedule;
                    var schedule = CrontabSchedule.Parse(scheduledTask.Schedule);
                    var times    = schedule.GetNextOccurrences(referenceTime, referenceTime.AddYears(5));
                    if (times.Count() < 2 || (times.Take(1).First() - times.First()).TotalDays > 13 * 7)
                    {
                        log.Error($"{scheduledTask.GetType().Name} schedule not supported: {CrontabSchedule.GetDescription(scheduledTask.Schedule)}");
                    }
                    else
                    {
                        if (scheduledTask.StartAfter > TimeSpan.Zero)
                        {
                            // set next run time to that specified by the user
                            referenceTime += scheduledTask.StartAfter;
                        }
                        else
                        {
                            // set next run time to the second occurrence time
                            // so that it doesn't get run immediately
                            referenceTime = times.Take(1).First();
                        }

                        _scheduledTasks.Add(new ScheduledTaskWrapper
                        {
                            ManualStartOnly = serviceSchedule.ManualStartOnly,
                            Schedule        = schedule,
                            Task            = scheduledTask,
                            NextRunTime     = referenceTime
                        });

                        var msg = $"{scheduledTask.GetType().Name} scheduled: {CrontabSchedule.GetDescription(scheduledTask.Schedule)}";
                        msg = $"{msg}, starts at {referenceTime.ToLocalTime().ToDefaultWithTime()}";
                        if (scheduledTask is ScheduledTask)
                        {
                            log.Information(msg);
                        }
                    }
                }
                else
                {
                    log.Warning($"{scheduledTask.GetType().Name} is not enabled");
                }
            }
            if (options.SuspendScheduling)
            {
                log.Information($"Task scheduling is suspended");
            }
        }