Пример #1
0
        private TaskDescription UpdateTask(TaskDescription taskDescription, TaskRequest taskRequest)
        {
            using (var db = new Db.DataContext())
            {
                var taskDb = db.Task.Where(x => x.UniqueKey == taskRequest.UniqueKey).Include(x => x.TaskSchedules).FirstOrDefault();
                if (taskDb == null)
                {
                    taskDb = new Db.Task()
                    {
                        Name          = taskRequest.Name,
                        Description   = taskRequest.Description,
                        IsEnabled     = null,
                        TaskSchedules = new List <Db.TaskSchedule>(),
                        UniqueKey     = taskRequest.UniqueKey
                    };
                    db.Task.Add(taskDb);
                    db.SaveChanges();
                }

                taskDb.Name        = taskRequest.Name;
                taskDb.Description = taskRequest.Description;

                if (!taskRequest.TaskOptions.HasFlag(TaskOptions.AllowDisabling))
                {
                    taskDb.IsEnabled = null;
                }

                if (!taskRequest.TaskOptions.HasFlag(TaskOptions.AllowManualSchedule) && taskDb.TaskSchedules.Count > 0)
                {
                    db.TaskSchedule.RemoveRange(taskDb.TaskSchedules);
                    taskDb.TaskSchedules.Clear();
                }
                db.SaveChanges();

                var schedules = new List <TaskSchedule>();
                foreach (var scheduleDb in taskDb.TaskSchedules)
                {
                    TaskSchedule taskSchedule = null;
                    if (scheduleDb.DateTimeFixed.HasValue)
                    {
                        taskSchedule = new TaskFixedTimeSchedule(new DateTimeOffset(scheduleDb.DateTimeFixed.Value.Ticks, TimeSpan.Zero));
                    }
                    else if (!string.IsNullOrEmpty(scheduleDb.Cron))
                    {
                        taskSchedule = new TaskCronSchedule(scheduleDb.Cron);
                    }

                    if (taskSchedule == null)
                    {
                        continue;
                    }
                    taskSchedule.IsEnabled = scheduleDb.IsEnabled;
                    schedules.Add(taskSchedule);
                }

                taskDescription.Id              = taskDb.Id;
                taskDescription.Name            = taskRequest.Name;
                taskDescription.Description     = taskRequest.Description;
                taskDescription.ExecutionLambda = taskRequest.ExecutionLambda;
                taskDescription.IsConfirmed     = true;
                taskDescription.UniqueKey       = taskRequest.UniqueKey;
                taskDescription.IsEnabled       = taskDb.IsEnabled ?? taskRequest.IsEnabled;
                taskDescription.TaskOptions     = taskRequest.TaskOptions;
                taskDescription.Schedules       = new ReadOnlyCollection <TaskSchedule>(taskRequest.Schedules ?? new List <TaskSchedule>());
                taskDescription.ManualSchedules = new ReadOnlyCollection <TaskSchedule>(schedules.GroupBy(x => x.GetUniqueKey()).Select(x => x.First()).ToList());
                taskDescription.JournalOptions  = taskRequest.JournalOptions;
            }
            return(taskDescription);
        }
Пример #2
0
        /// <summary>
        /// Возвращает список задач.
        /// </summary>
        /// <param name="onlyConfirmed">Если равно true, то возвращает только подтвержденные задачи (см. <see cref="TaskDescription.IsConfirmed"/>.</param>
        public List <TaskDescription> GetTaskList(bool onlyConfirmed)
        {
            try
            {
                var list = _taskList.Values.ToDictionary(x => x.Id, x => x);
                if (!onlyConfirmed)
                {
                    using (var db = new Db.DataContext())
                    {
                        var query    = db.Task.Include(x => x.TaskSchedules);
                        var taskList = query.ToList();
                        foreach (var taskDb in taskList)
                        {
                            if (list.ContainsKey(taskDb.Id))
                            {
                                continue;
                            }

                            var schedules = new List <TaskSchedule>();
                            foreach (var scheduleDb in taskDb.TaskSchedules)
                            {
                                TaskSchedule taskSchedule = null;
                                if (scheduleDb.DateTimeFixed.HasValue)
                                {
                                    taskSchedule = new TaskFixedTimeSchedule(new DateTimeOffset(scheduleDb.DateTimeFixed.Value.Ticks, TimeSpan.Zero));
                                }
                                else if (!string.IsNullOrEmpty(scheduleDb.Cron))
                                {
                                    taskSchedule = new TaskCronSchedule(scheduleDb.Cron);
                                }

                                if (taskSchedule == null)
                                {
                                    continue;
                                }
                                taskSchedule.IsEnabled = scheduleDb.IsEnabled;
                                schedules.Add(taskSchedule);
                            }

                            var taskDescription = new TaskDescription
                            {
                                Id              = taskDb.Id,
                                Name            = taskDb.Name,
                                Description     = taskDb.Description,
                                ExecutionLambda = null,
                                IsConfirmed     = false,
                                UniqueKey       = taskDb.UniqueKey,
                                IsEnabled       = taskDb.IsEnabled ?? false,
                                TaskOptions     = TaskOptions.None,
                                Schedules       = new ReadOnlyCollection <TaskSchedule>(new List <TaskSchedule>()),
                                ManualSchedules = new ReadOnlyCollection <TaskSchedule>(schedules)
                            };
                            list[taskDb.Id] = taskDescription;
                        }
                    }
                }
                return(list.Values.ToList());
            }
            catch (Exception ex)
            {
                this.RegisterEvent(Journaling.EventType.CriticalError, "Ошибка во время получения списка задач", null, ex);
                throw new Exception("Неожиданная ошибка во время получения списка задач.");
            }
        }