Пример #1
0
        protected void TasksScheduledThread()
        {
            while (true)
            {
                log.TraceFormat("{0:S} - Check for tasks to schedule", this.ToString());

                try
                {
                    using (SqlConnection conn = new SqlConnection(ConnectionString))
                    {
                        conn.Open();
                        using (SqlTransaction trans = conn.BeginTransaction(System.Data.IsolationLevel.Serializable))
                        {
                            // Get enabled schedules
                            var lSchedules = Schedule.GetAndLockEnabledNotRunning(conn, trans);

                            #region look for schedules to fire
                            lSchedules.ForEach(sched =>
                            {
                                NCrontab.CrontabSchedule cs = NCrontab.CrontabSchedule.Parse(sched.Cron);
                                if (cs.GetNextOccurrence(LastScheduleCheck) < DateTime.Now && (sched.LastFired < cs.GetNextOccurrence(LastScheduleCheck)))
                                {
                                    var task = YoctoScheduler.Core.Database.Task.GetByID <Task>(conn, trans, sched.TaskID);
                                    log.InfoFormat("Starting schedulation {0:S} due to cron {1:S}", task.ToString(), sched.ToString());

                                    // save schedule fire time
                                    sched.LastFired = DateTime.Now;
                                    Schedule.Update(conn, trans, sched);

                                    ExecutionQueueItem eqi = new ExecutionQueueItem()
                                    {
                                        TaskID     = task.ID,
                                        Priority   = Priority.Normal,
                                        ScheduleID = sched.ID,
                                        Inserted   = DateTime.Now
                                    };
                                    ExecutionQueueItem.Insert(conn, trans, eqi);
                                    log.InfoFormat("Execution enqueued {0:S}", eqi.ToString());
                                }
                            });
                            #endregion

                            trans.Commit();
                        }
                    }

                    LastScheduleCheck = DateTime.Now;

                    Thread.Sleep(int.Parse(Configuration["SERVER_POLL_TASK_SCHEDULER_SLEEP_MS"]));
                }
                catch (Exception exce) // catch all to keep the thread alive
                {
                    log.ErrorFormat("Unhandled exception during TasksScheduledThread: {0:S}", exce.ToString());
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Validates the schedule.
        /// </summary>
        /// <param name="schedule">The schedule.</param>
        /// <returns></returns>
        public bool ValidateSchedule(string schedule)
        {
            NCrontab.CrontabSchedule cronSchedule = NCrontab.CrontabSchedule.TryParse(schedule);
            if (cronSchedule == null)
            {
                this.ErrorMessages.Add("Schedule is not valid");
                return(false);
            }

            return(true);
        }
        private bool MakePlanning(string cron, string task, string taskDescr,
                                  SortedDic <String, clsTaskList> frequentlyDic,
                                  SortedDic <String, clsTaskList> hourlyDic,
                                  SortedDic <String, clsTaskList> dailyDic,
                                  SortedDic <String, clsTaskList> weeklyDic,
                                  SortedDic <String, clsTaskList> monthlyDic, ref string errMsg)
        {
            errMsg = "";
            NCrontab.CrontabSchedule s = null;
            try
            {
                s = NCrontab.CrontabSchedule.Parse(cron);
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                return(false);
            }

            var cronL = cron.ToLower();

            var firstDay = DateTime.Today.FirstDayOfMonth();

            var    isRecurrent = false;
            string taskDescrL  = taskDescr.ToLower();

            if (taskDescrL.Contains("every"))
            {
                isRecurrent = true;
            }
            if (taskDescrL.Contains("tous"))
            {
                isRecurrent = true;                              // French
            }
            if (taskDescrL.Contains("toutes"))
            {
                isRecurrent = true;                                // French
            }
            bool mon      = cronL.Contains(" mon");
            bool tue      = cronL.Contains(" tue");
            bool wed      = cronL.Contains(" wed");
            bool thu      = cronL.Contains(" thu");
            bool fri      = cronL.Contains(" fri");
            bool sat      = cronL.Contains(" sat");
            bool sun      = cronL.Contains(" sun");
            bool isWeekly = mon || tue || wed || thu || fri || sat || sun;

            bool onDay = taskDescrL.Contains("on day") ||
                         taskDescrL.Contains("le");           // French
            bool ofTheMonth = taskDescrL.Contains("of the month") ||
                              taskDescrL.Contains("du mois"); // French
            bool isMonthly = onDay && ofTheMonth;

            if (isRecurrent && !isWeekly && !isMonthly)
            {
                var hourlyStartDate   = firstDay;
                var hourlyEndDate     = hourlyStartDate.AddHours(24);
                var hourlyOccurrences = s.GetNextOccurrences(hourlyStartDate, hourlyEndDate);
                foreach (DateTime date in hourlyOccurrences)
                {
                    string key = taskDescr;
                    if (!hourlyDic.ContainsKey(key))
                    {
                        var lst = new List <clsTask> {
                            new clsTask(date, task, taskDescr)
                        };
                        hourlyDic.Add(key, new clsTaskList(date, lst));
                    }
                    else
                    {
                        var lst = hourlyDic[key];
                        lst.taskList.Add(new clsTask(date, task, taskDescr));
                    }

                    key = date.ToString(formatHM);
                    if (!frequentlyDic.ContainsKey(key))
                    {
                        var lst = new List <clsTask> {
                            new clsTask(date, task, taskDescr)
                        };
                        frequentlyDic.Add(key, new clsTaskList(date, lst));
                    }
                    else
                    {
                        var lst = frequentlyDic[key];
                        lst.taskList.Add(new clsTask(date, task, taskDescr));
                    }
                }
            }

            if (!isRecurrent && !isWeekly && !isMonthly)
            {
                var dailyStartDate   = firstDay;
                var dailyEndDate     = dailyStartDate.AddDays(1);
                var dailyOccurrences = s.GetNextOccurrences(dailyStartDate, dailyEndDate);
                foreach (DateTime date in dailyOccurrences)
                {
                    string key = taskDescr;
                    if (hourlyDic.ContainsKey(key))
                    {
                        continue;
                    }
                    if (!dailyDic.ContainsKey(key))
                    {
                        var lst = new List <clsTask> {
                            new clsTask(date, task, taskDescr)
                        };
                        dailyDic.Add(key, new clsTaskList(date, lst));
                    }
                    else
                    {
                        var lst = dailyDic[key];
                        lst.taskList.Add(new clsTask(date, task, taskDescr));
                    }
                }
            }

            if (!isRecurrent && isWeekly && !isMonthly)
            {
                //var weeklyStartDate = getMondayBefore(firstDay);
                var weeklyStartDate   = firstDay.GetMondayBefore();
                var weeklyEndDate     = weeklyStartDate.AddDays(7);
                var weeklyOccurrences = s.GetNextOccurrences(weeklyStartDate, weeklyEndDate);
                foreach (DateTime date in weeklyOccurrences)
                {
                    string key = taskDescr;
                    if (hourlyDic.ContainsKey(key))
                    {
                        continue;
                    }
                    if (dailyDic.ContainsKey(key))
                    {
                        continue;
                    }
                    if (!weeklyDic.ContainsKey(key))
                    {
                        var lst = new List <clsTask> {
                            new clsTask(date, task, taskDescr)
                        };
                        weeklyDic.Add(key, new clsTaskList(date, lst));
                    }
                    else
                    {
                        var lst = weeklyDic[key];
                        lst.taskList.Add(new clsTask(date, task, taskDescr));
                    }
                }
            }

            if (!isRecurrent && !isWeekly && isMonthly)
            {
                var monthlyStartDate   = firstDay;
                var monthlyEndDate     = monthlyStartDate.AddMonths(1);
                var monthlyOccurrences = s.GetNextOccurrences(monthlyStartDate, monthlyEndDate);
                foreach (DateTime date in monthlyOccurrences)
                {
                    string key = taskDescr;
                    if (hourlyDic.ContainsKey(key))
                    {
                        continue;
                    }
                    if (dailyDic.ContainsKey(key))
                    {
                        continue;
                    }
                    if (weeklyDic.ContainsKey(key))
                    {
                        continue;
                    }
                    if (!monthlyDic.ContainsKey(key))
                    {
                        var lst = new List <clsTask> {
                            new clsTask(date, task, taskDescr)
                        };
                        monthlyDic.Add(key, new clsTaskList(date, lst));
                    }
                    else
                    {
                        var lst = monthlyDic[key];
                        lst.taskList.Add(new clsTask(date, task, taskDescr));
                    }
                }
            }
            return(true);
        }