Пример #1
0
        public static void RunEventSchedule()
        {
            try
            {
                ScheduleHistory objScheduleHistory = null;

                List <Schedule> schedules = SchedulerController.GetTasks();

                foreach (Schedule schedule in schedules)
                {
                    objScheduleHistory = new ScheduleHistory(schedule);
                    //check &&  (objScheduleHistory.Status == false || objScheduleHistory.Status == null) &&
                    if (!IsInQueue(objScheduleHistory) && !IsInProgress(objScheduleHistory) && (objScheduleHistory.Status == false || objScheduleHistory.Status == null) && objScheduleHistory.IsEnable)
                    {
                        objScheduleHistory.Status = null;
                        AddToScheduleQueue(objScheduleHistory);
                    }
                }

                while (GetScheduleQueueCount() > 0)
                {
                    SetScheduleStatus(ScheduleStatus.RUNNING_EVENT_SCHEDULE);

                    //Fire off the events that need running.
                    if (GetScheduleQueueCount() > 0)
                    {
                        FireEvents(true);
                    }


                    if (_writerTimeouts > 20 || _readerTimeouts > 20)
                    {
                        //Wait for 10 minutes so we don't fill up the logs
                        Thread.Sleep(TimeSpan.FromMinutes(10));
                    }
                    else
                    {
                        //Wait for 10 seconds to avoid cpu overutilization
                        Thread.Sleep(TimeSpan.FromSeconds(10));
                    }

                    if (GetScheduleQueueCount() == 0)
                    {
                        return;
                    }
                }
            }
            catch (Exception exc)
            {
                ErrorLogger.SchedulerProcessException(exc);
            }
        }
Пример #2
0
        public static void SchedulerProcessException(Exception exc)
        {
            string strIPaddress = string.Empty;
            string strPageUrl   = string.Empty;

            if (HttpContext.Current != null && HttpContext.Current.Request != null && HttpContext.Current.Request.UserHostAddress != string.Empty)
            {
                strIPaddress = HttpContext.Current.Request.UserHostAddress;
            }

            if (HttpContext.Current != null && HttpContext.Current.Request != null && HttpContext.Current.Request.RawUrl != string.Empty)
            {
                strPageUrl = HttpContext.Current.Request.RawUrl;
            }
            SageFrameConfig sfConfig = new SageFrameConfig();

            SchedulerController.AddSchedulerException(11, 11, exc.Message, exc.ToString(),
                                                      strIPaddress, strPageUrl, true, sfConfig.GetPortalID, sfConfig.GetUsername);
        }
Пример #3
0
        public static void TaskStarted(TaskBase task)
        {
            bool ActiveThreadCountIncremented = false;

            try
            {
                task.TaskRecordItem.ThreadID = Thread.CurrentThread.GetHashCode();
                if (task.TaskRecordItem.Status != true)
                {
                    task.TaskRecordItem.Status = false;
                }
                task.TaskRecordItem.ReturnText = "Process Started";

                IPHostEntry entries = Dns.GetHostByName(Dns.GetHostName());
                task.TaskRecordItem.Server = entries.AddressList[0].ToString();

                RemoveFromScheduleQueue(task.TaskRecordItem);
                AddToScheduleInProgress(task.TaskRecordItem);

                Interlocked.Increment(ref ActiveThreadCount);
                ActiveThreadCountIncremented = true;

                task.TaskRecordItem.HistoryStartDate  = DateTime.Now;
                task.TaskRecordItem.ScheduleHistoryID = SchedulerController.AddTaskHistory(task.TaskRecordItem);
            }
            catch (Exception exc)
            {
                if (ActiveThreadCountIncremented)
                {
                    Interlocked.Decrement(ref ActiveThreadCount);
                }
                if (task.TaskRecordItem != null)
                {
                    ErrorLogger.SchedulerProcessException(exc);
                }
            }
        }
Пример #4
0
        public static void TaskCompleted(TaskBase task)
        {
            try
            {
                ScheduleHistory objScheduleHistory = task.TaskRecordItem;
                objScheduleHistory = task.TaskRecordItem;

                RemoveFromScheduleInProgress(objScheduleHistory);
                Interlocked.Decrement(ref ActiveThreadCount);

                objScheduleHistory.HistoryEndDate = DateTime.Now;

                objScheduleHistory.Status    = true;
                objScheduleHistory.NextStart = SchedulerController.GetNextStart(objScheduleHistory);

                task.TaskRecordItem.ReturnText = "Successfully completed";

                SchedulerController.UpdateTaskHistory(objScheduleHistory);

                if (!string.IsNullOrEmpty(objScheduleHistory.NextStart))
                {
                    objScheduleHistory.HistoryStartDate = null;
                    objScheduleHistory.HistoryEndDate   = null;
                    objScheduleHistory.ReturnText       = "";
                    objScheduleHistory.ProcessGroup     = -1;
                    objScheduleHistory.Status           = false;
                    AddToScheduleQueue(objScheduleHistory);
                }
            }
            catch (Exception exc)
            {
                if (task.TaskRecordItem != null)
                {
                    ErrorLogger.SchedulerProcessException(exc);
                }
            }
        }
Пример #5
0
        public static void TaskErrored(TaskBase task, Exception exception)
        {
            try
            {
                ScheduleHistory objScheduleHistoryItem = task.TaskRecordItem;

                RemoveFromScheduleInProgress(objScheduleHistoryItem);

                Interlocked.Decrement(ref ActiveThreadCount);

                objScheduleHistoryItem.HistoryEndDate = DateTime.Now;

                DateTime startedTime = objScheduleHistoryItem.HistoryStartDate ?? DateTime.Now;

                if (objScheduleHistoryItem.RetryTimeLapse > 0)
                {
                    switch (objScheduleHistoryItem.RetryFrequencyUnit)
                    {
                    case 1:
                        startedTime.AddSeconds(objScheduleHistoryItem.RetryTimeLapse);
                        break;

                    case 2:
                        startedTime.AddMinutes(objScheduleHistoryItem.RetryTimeLapse);
                        break;

                    case 3:
                        startedTime.AddHours(objScheduleHistoryItem.RetryTimeLapse);
                        break;

                    case 4:
                        startedTime.AddDays(objScheduleHistoryItem.RetryTimeLapse);
                        break;
                    }
                    objScheduleHistoryItem.NextStart = Convert.ToString(startedTime);
                }

                objScheduleHistoryItem.Status = false;
                string error = "";

                StringBuilder errorlog = new StringBuilder();

                errorlog.AppendLine("Error");
                if ((exception != null))
                {
                    error = exception.Message;
                }
                errorlog.AppendLine(error);

                task.TaskRecordItem.ReturnText = errorlog.ToString();

                SchedulerController.UpdateTaskHistory(objScheduleHistoryItem);

                if (!string.IsNullOrEmpty(objScheduleHistoryItem.NextStart) && objScheduleHistoryItem.RetryTimeLapse > 0)
                {
                    objScheduleHistoryItem.HistoryStartDate = null;
                    objScheduleHistoryItem.HistoryEndDate   = null;
                    objScheduleHistoryItem.ReturnText       = "";
                    objScheduleHistoryItem.ProcessGroup     = -1;
                    AddToScheduleQueue(objScheduleHistoryItem);
                }
            }
            catch (Exception exc)
            {
                if (task.TaskRecordItem != null)
                {
                    ErrorLogger.SchedulerProcessException(exc);
                }
            }
        }
Пример #6
0
        public static void Start()
        {
            for (int i = 0; i <= 30; i++)
            {
                Thread.Sleep(1000);
            }

            try
            {
                ActiveThreadCount = 0;



                while (KeepRunning)
                {
                    try
                    {
                        ForceReloadSchedule = false;
                        ScheduleHistory objScheduleHistory = null;

                        List <Schedule> schedules = SchedulerController.GetTasks();

                        foreach (Schedule schedule in schedules)
                        {
                            objScheduleHistory = new ScheduleHistory(schedule);
                            //check &&  (objScheduleHistory.Status == false || objScheduleHistory.Status == null) &&
                            if (!IsInQueue(objScheduleHistory) && !IsInProgress(objScheduleHistory) && (objScheduleHistory.Status == false || objScheduleHistory.Status == null) && objScheduleHistory.IsEnable)
                            {
                                objScheduleHistory.Status = null;
                                AddToScheduleQueue(objScheduleHistory);
                            }
                        }

                        DateTime lastQueueRefresh     = DateTime.Now;
                        bool     refreshQueueSchedule = false;

                        while (FreeThreads > 0 && KeepRunning && !ForceReloadSchedule)
                        {
                            if (GetScheduleQueueCount() > 0)
                            {
                                FireEvents(true);
                            }
                            if (KeepThreadAlive == false)
                            {
                                return;
                            }


                            if (_writerTimeouts > 20 || _readerTimeouts > 20)
                            {
                                if (KeepRunning)
                                {
                                    Thread.Sleep(TimeSpan.FromMinutes(10));
                                }
                                else
                                {
                                    return;
                                }
                            }
                            else
                            {
                                if (KeepRunning)
                                {
                                    Thread.Sleep(TimeSpan.FromSeconds(10));
                                }
                                else
                                {
                                    return;
                                }

                                if ((lastQueueRefresh.AddMinutes(10) <= DateTime.Now || ForceReloadSchedule) && FreeThreads == _maxThreadCount)
                                {
                                    refreshQueueSchedule = true;
                                    break;
                                }
                            }
                        }

                        if (KeepRunning)
                        {
                            if (refreshQueueSchedule == false)
                            {
                                SetScheduleStatus(ScheduleStatus.WAITING_FOR_OPEN_THREAD);
                                // Thread.Sleep(10000); //sleep for 10 seconds
                            }
                        }
                        else
                        {
                            return;
                        }
                    }
                    catch (Exception exc)
                    {
                        ErrorLogger.SchedulerProcessException(exc);
                        //sleep for 10 minutes
                        //Thread.Sleep(600000);
                    }
                }
            }
            finally
            {
                SetScheduleStatus(ScheduleStatus.STOPPED);
                KeepRunning       = false;
                ActiveThreadCount = 0;
            }
        }
Пример #7
0
        /// <summary>
        /// Obtain next start time.
        /// </summary>
        /// <param name="objScheduleHistory">Object of Schedule class.</param>
        /// <returns>Next start.</returns>
        public static string GetNextStart(Schedule objScheduleHistory)
        {
            if (objScheduleHistory.RunningMode != RunningMode.Once)
            {
                if (string.IsNullOrEmpty(objScheduleHistory.NextStart))
                {
                    objScheduleHistory.NextStart = Convert.ToString(DateTime.Parse(objScheduleHistory.StartDate).ToShortDateString() + " " + objScheduleHistory.StartHour + ":" + objScheduleHistory.StartMin + ":00");
                }
                else if (!objScheduleHistory.CatchUpEnabled)
                {
                    objScheduleHistory.NextStart = Convert.ToString(DateTime.Now);
                }
            }
            switch (objScheduleHistory.RunningMode)
            {
            case RunningMode.Hourly:

                objScheduleHistory.NextStart = Convert.ToString(DateTime.Parse(objScheduleHistory.NextStart).AddHours(objScheduleHistory.EveryHours).AddMinutes(objScheduleHistory.EveryMin));
                break;

            case RunningMode.Daily:
                objScheduleHistory.NextStart = Convert.ToString(DateTime.Parse(objScheduleHistory.NextStart).AddDays(objScheduleHistory.RepeatDays));
                break;

            case RunningMode.Weekly:
                int      day = GetNextScheduleWeekDay(objScheduleHistory.ScheduleID, (int)DateTime.Parse(objScheduleHistory.NextStart).DayOfWeek + 1);
                DateTime now = DateTime.Parse(DateTime.Now.ToShortDateString() + "  " + objScheduleHistory.StartHour + ":" + objScheduleHistory.StartMin + ":00");

                int nextday         = 0;
                int currentstartday = (int)now.DayOfWeek + 1;

                if (day > currentstartday)
                {
                    nextday = day - currentstartday;
                }
                else
                {
                    nextday = 7 - currentstartday + day;
                }

                objScheduleHistory.NextStart = Convert.ToString(now.AddDays(nextday));
                break;

            case RunningMode.WeekNumber:

                day = GetNextScheduleWeekDay(objScheduleHistory.ScheduleID, (int)DateTime.Parse(objScheduleHistory.NextStart).DayOfWeek + 1);
                now = DateTime.Parse(DateTime.Now.ToShortDateString() + "  " + objScheduleHistory.StartHour + ":" + objScheduleHistory.StartMin + ":00");

                nextday         = 0;
                currentstartday = (int)now.DayOfWeek + 1;
                int nextMonth = now.Month;
                if (day > currentstartday)
                {
                    nextday = now.Day + day - currentstartday;
                }
                else
                {
                    nextMonth = SchedulerController.ScheduleDateGetNextMonth(objScheduleHistory.ScheduleID, DateTime.Parse(objScheduleHistory.NextStart).Month);

                    nextday = objScheduleHistory.RepeatWeeks * 7 - 7 + day;
                }

                DateTime nextDate = new DateTime(now.Year, nextMonth, nextday);

                objScheduleHistory.NextStart = Convert.ToString(nextDate);
                break;

            case RunningMode.Calendar:

                objScheduleHistory.NextStart = Convert.ToString(GetNextScheduleDate(objScheduleHistory.ScheduleID, objScheduleHistory.NextStart));
                //objScheduleHistory.NextStart = Convert.ToString(DateTime.Parse(objScheduleHistory.NextStart).AddDays(objScheduleHistory.RepeatDays));

                break;

            case RunningMode.Once:
                objScheduleHistory.NextStart = string.Empty;
                break;
            }
            if (objScheduleHistory.NextStart != string.Empty && DateTime.Parse(objScheduleHistory.NextStart) > DateTime.Parse(objScheduleHistory.EndDate))
            {
                objScheduleHistory.NextStart = string.Empty;
            }
            return(objScheduleHistory.NextStart);
        }