Beispiel #1
0
 public static void TaskProgressing(TaskBase task)
 {
     try
     {
     }
     catch (Exception exc)
     {
         ErrorLogger.SchedulerProcessException(exc);
     }
 }
Beispiel #2
0
        public static void FireEvents(bool Asynchronous)
        {
            using (ScheduleQueue.GetReadLock(LockTimeout))
            {
                int numToRun = ScheduleQueue.Count;
                int numRun   = 0;

                foreach (ScheduleHistory objSchedule in ScheduleQueue)
                {
                    if (!KeepRunning)
                    {
                        return;
                    }

                    int ProcessGroup = GetProcessGroup();

                    //if (objSchedule.NextStart <= DateTime.Now &&
                    //    objSchedule.Enabled &&
                    //    !IsInProgress(objSchedule) &&
                    //    numRun < numToRun)
                    //{


                    if (objSchedule.StartDate != null)
                    {
                        string   strStartDate = objSchedule.StartDate.Substring(0, objSchedule.StartDate.LastIndexOf("/") + 5);
                        DateTime?tmpDate      = objSchedule.HistoryStartDate == null?
                                                Convert.ToDateTime(strStartDate + " " + objSchedule.StartHour + ":" + objSchedule.StartMin + ":00") :
                                                    objSchedule.HistoryStartDate;

                        if (((string.IsNullOrEmpty(objSchedule.NextStart) && objSchedule.HistoryStartDate == null && tmpDate <= DateTime.Now) ||
                             (DateTime.Parse(objSchedule.NextStart) <= DateTime.Now && (string.IsNullOrEmpty(objSchedule.EndDate) ||
                                                                                        DateTime.Parse(objSchedule.EndDate) > DateTime.Now))) &&
                            !IsInProgress(objSchedule) && numRun < numToRun && objSchedule.Status != true)
                        {
                            objSchedule.ProcessGroup = ProcessGroup;

                            // if (Asynchronous)
                            arrProcessGroup[ProcessGroup].AddQueueUserWorkItem(objSchedule);
                            // else
                            // arrProcessGroup[ProcessGroup].RunSingleTask(objSchedule);

                            numRun += 1;
                        }
                    }
                    else
                    {
                        ErrorLogger.SchedulerProcessException(new Exception("Not started for task" + objSchedule.ScheduleName));
                    }
                }
            }
        }
Beispiel #3
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);
            }
        }
Beispiel #4
0
 private static bool ScheduleQueueContains(ScheduleHistory objScheduleItem)
 {
     try
     {
         using (ScheduleQueue.GetReadLock(LockTimeout))
         {
             return(ScheduleQueue.Any(si => si.ScheduleID == objScheduleItem.ScheduleID));
         }
     }
     catch (ApplicationException ex)
     {
         Interlocked.Increment(ref _readerTimeouts);
         ErrorLogger.SchedulerProcessException(ex);
         return(false);
     }
 }
Beispiel #5
0
 private static bool IsInProgress(Schedule scheduleItem)
 {
     try
     {
         using (ScheduleInProgress.GetReadLock(LockTimeout))
         {
             return(ScheduleInProgress.Any(si => si.ScheduleID == scheduleItem.ScheduleID));
         }
     }
     catch (ApplicationException ex)
     {
         Interlocked.Increment(ref _readerTimeouts);
         ErrorLogger.SchedulerProcessException(ex);
         return(false);
     }
 }
Beispiel #6
0
 public static void RemoveFromScheduleQueueByID(int ScheduleID)
 {
     try
     {
         using (ScheduleQueue.GetWriteLock(LockTimeout))
         {
             ScheduleHistory item = ScheduleQueue.Where(si => si.ScheduleID == ScheduleID).SingleOrDefault();
             if (item != null)
             {
                 ScheduleQueue.Remove(item);
             }
         }
     }
     catch (ApplicationException ex)
     {
         Interlocked.Increment(ref _writerTimeouts);
         ErrorLogger.SchedulerProcessException(ex);
     }
 }
Beispiel #7
0
 private static void RemoveFromScheduleInProgress(Schedule scheduleItem)
 {
     try
     {
         using (ScheduleInProgress.GetWriteLock(LockTimeout))
         {
             ScheduleHistory item = ScheduleInProgress.Where(si => si.ScheduleID == scheduleItem.ScheduleID).SingleOrDefault();
             if (item != null)
             {
                 ScheduleInProgress.Remove(item);
             }
         }
     }
     catch (ApplicationException ex)
     {
         Interlocked.Increment(ref _writerTimeouts);
         ErrorLogger.SchedulerProcessException(ex);
     }
 }
Beispiel #8
0
 public static void SetScheduleStatus(ScheduleStatus newStatus)
 {
     try
     {
         StatusLock.AcquireWriterLock(LockTimeout);
         try
         {
             _status = newStatus;
         }
         finally
         {
             StatusLock.ReleaseWriterLock();
         }
     }
     catch (ApplicationException ex)
     {
         Interlocked.Increment(ref _writerTimeouts);
         ErrorLogger.SchedulerProcessException(ex);
     }
 }
Beispiel #9
0
        public static TaskList <ScheduleHistory> GetScheduleInProgress()
        {
            var c = new TaskList <ScheduleHistory>();

            try
            {
                using (ScheduleInProgress.GetReadLock(LockTimeout))
                {
                    foreach (ScheduleHistory item in ScheduleInProgress)
                    {
                        c.Add(item);
                    }
                }
            }
            catch (ApplicationException ex)
            {
                Interlocked.Increment(ref _readerTimeouts);
                ErrorLogger.SchedulerProcessException(ex);
            }
            return(c);
        }
Beispiel #10
0
 private static void AddToScheduleInProgress(ScheduleHistory scheduleHistoryItem)
 {
     if (!(ScheduleInProgressContains(scheduleHistoryItem)))
     {
         try
         {
             using (ScheduleInProgress.GetWriteLock(LockTimeout))
             {
                 if (!(ScheduleInProgressContains(scheduleHistoryItem)))
                 {
                     ScheduleInProgress.Add(scheduleHistoryItem);
                 }
             }
         }
         catch (ApplicationException ex)
         {
             // The writer lock request timed out.
             Interlocked.Increment(ref _writerTimeouts);
             ErrorLogger.SchedulerProcessException(ex);
         }
     }
 }
Beispiel #11
0
 public static void AddToScheduleQueue(ScheduleHistory objScheduleHistory)
 {
     if (!ScheduleQueueContains(objScheduleHistory))
     {
         try
         {
             using (ScheduleQueue.GetWriteLock(LockTimeout))
             {
                 if (!ScheduleQueueContains(objScheduleHistory) &&
                     !IsInProgress(objScheduleHistory))
                 {
                     ScheduleQueue.Add(objScheduleHistory);
                 }
             }
         }
         catch (ApplicationException ex)
         {
             // The writer lock request timed out.
             Interlocked.Increment(ref _writerTimeouts);
             ErrorLogger.SchedulerProcessException(ex);
         }
     }
 }
Beispiel #12
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);
                }
            }
        }
Beispiel #13
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);
                }
            }
        }
Beispiel #14
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);
                }
            }
        }
Beispiel #15
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;
            }
        }