Ejemplo n.º 1
0
        public static void ScheduleTasks()
        {
            nextSchedule = SchedulerController.GetNextSchedule();

            // set timer
            if (nextSchedule != null)
            {
                if (nextSchedule.ScheduleInfo.NextRun <= DateTime.Now)
                {
                    // this will put the timer to sleep
                    scheduleTimer.Change(Timeout.Infinite, Timeout.Infinite);

                    // run immediately
                    RunNextSchedule(null);
                }
                else
                {
                    // set timer
                    TimeSpan ts = nextSchedule.ScheduleInfo.NextRun.Subtract(DateTime.Now);
                    if (ts < TimeSpan.Zero)
                    {
                        ts = TimeSpan.Zero; // cannot be negative !
                    }
                    // invoke after the timespan
                    scheduleTimer.Change((long)ts.TotalMilliseconds, Timeout.Infinite);
                }
            }
        }
Ejemplo n.º 2
0
        public static int StopSchedule(int scheduleId)
        {
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo);

            if (accountCheck < 0)
            {
                return(accountCheck);
            }

            SchedulerJob schedule = GetScheduleComplete(scheduleId);

            if (schedule == null)
            {
                return(0);
            }

            foreach (BackgroundTask task in TaskController.GetScheduleTasks(scheduleId))
            {
                task.Status = BackgroundTaskStatus.Stopping;

                TaskController.UpdateTask(task);
            }

            return(0);
        }
Ejemplo n.º 3
0
        static void RunSchedule(SchedulerJob schedule, bool changeNextRun)
        {
            // update next run (if required)
            if (changeNextRun)
            {
                SchedulerController.CalculateNextStartTime(schedule.ScheduleInfo);
            }

            // disable run once task
            if (schedule.ScheduleInfo.ScheduleType == ScheduleType.OneTime)
            {
                schedule.ScheduleInfo.Enabled = false;
            }

            Dictionary <int, BackgroundTask> scheduledTasks = TaskManager.GetScheduledTasks();

            if (!scheduledTasks.ContainsKey(schedule.ScheduleInfo.ScheduleId))
            {
                // this task should be run, so
                // update its last run
                schedule.ScheduleInfo.LastRun = DateTime.Now;
            }

            // update schedule
            SchedulerController.UpdateSchedule(schedule.ScheduleInfo);

            // skip execution if the current task is still running
            if (scheduledTasks.ContainsKey(schedule.ScheduleInfo.ScheduleId))
            {
                return;
            }

            // run the schedule in the separate thread
            schedule.Run();
        }
Ejemplo n.º 4
0
        public static void ScheduleTasks()
        {
            nextSchedule = SchedulerController.GetNextSchedule();

            // set timer
            if (nextSchedule != null)
            {
                if (nextSchedule.ScheduleInfo.NextRun <= DateTime.Now)
                {
                    // this will put the timer to sleep
                    scheduleTimer.Change(Timeout.Infinite, Timeout.Infinite);

                    // run immediately
                    RunNextSchedule(null);
                }
                else
                {
                    // set timer
                    TimeSpan ts = nextSchedule.ScheduleInfo.NextRun.Subtract(DateTime.Now);
                    if (ts < TimeSpan.Zero)
                        ts = TimeSpan.Zero; // cannot be negative !

                    // invoke after the timespan
                    scheduleTimer.Change((long)ts.TotalMilliseconds, Timeout.Infinite);
                }
            }
        }
Ejemplo n.º 5
0
        public static int StartSchedule(int scheduleId)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo);

            if (accountCheck < 0)
            {
                return(accountCheck);
            }

            SchedulerJob schedule = GetScheduleComplete(scheduleId);

            if (schedule == null)
            {
                return(0);
            }

            if (TaskController.GetScheduleTasks(scheduleId).Any(x => x.Status == BackgroundTaskStatus.Run ||
                                                                x.Status == BackgroundTaskStatus.Starting))
            {
                return(0);
            }

            var parameters = schedule.ScheduleInfo.Parameters.Select(
                prm => new BackgroundTaskParameter(prm.ParameterId, prm.ParameterValue)).ToList();

            var userInfo = PackageController.GetPackageOwner(schedule.ScheduleInfo.PackageId);

            var backgroundTask = new BackgroundTask(
                Guid.NewGuid(),
                Guid.NewGuid().ToString("N"),
                userInfo.OwnerId == 0 ? userInfo.UserId : userInfo.OwnerId,
                userInfo.UserId,
                "SCHEDULER",
                "RUN_SCHEDULE",
                schedule.ScheduleInfo.ScheduleName,
                schedule.ScheduleInfo.ScheduleId,
                schedule.ScheduleInfo.ScheduleId,
                schedule.ScheduleInfo.PackageId,
                schedule.ScheduleInfo.MaxExecutionTime, parameters)
            {
                Status = BackgroundTaskStatus.Starting
            };

            TaskController.AddTask(backgroundTask);

            // update next run (if required)
            CalculateNextStartTime(schedule.ScheduleInfo);

            // disable run once task
            if (schedule.ScheduleInfo.ScheduleType == ScheduleType.OneTime)
            {
                schedule.ScheduleInfo.Enabled = false;
            }

            schedule.ScheduleInfo.LastRun = DateTime.Now;
            UpdateSchedule(schedule.ScheduleInfo);

            return(0);
        }
Ejemplo n.º 6
0
        public static void StopSchedule(SchedulerJob schedule)
        {
            Dictionary<int, BackgroundTask> scheduledTasks = TaskManager.GetScheduledTasks();
            if (!scheduledTasks.ContainsKey(schedule.ScheduleInfo.ScheduleId))
                return;

            BackgroundTask activeTask = scheduledTasks[schedule.ScheduleInfo.ScheduleId];
            TaskManager.StopTask(activeTask.TaskId);
        }
Ejemplo n.º 7
0
        public static void StartSchedule(SchedulerJob schedule)
        {
            Dictionary<int, BackgroundTask> scheduledTasks = TaskManager.GetScheduledTasks();
            if (scheduledTasks.ContainsKey(schedule.ScheduleInfo.ScheduleId))
                return;

            // run schedule
            RunSchedule(schedule, false);
        }
Ejemplo n.º 8
0
        public static void StartSchedule(SchedulerJob schedule)
        {
            Dictionary <int, BackgroundTask> scheduledTasks = TaskManager.GetScheduledTasks();

            if (scheduledTasks.ContainsKey(schedule.ScheduleInfo.ScheduleId))
            {
                return;
            }

            // run schedule
            RunSchedule(schedule, false);
        }
Ejemplo n.º 9
0
        public static void StopSchedule(SchedulerJob schedule)
        {
            Dictionary <int, BackgroundTask> scheduledTasks = TaskManager.GetScheduledTasks();

            if (!scheduledTasks.ContainsKey(schedule.ScheduleInfo.ScheduleId))
            {
                return;
            }

            BackgroundTask activeTask = scheduledTasks[schedule.ScheduleInfo.ScheduleId];

            TaskManager.StopTask(activeTask.TaskId);
        }
Ejemplo n.º 10
0
        public static void ScheduleTasks()
        {
            RunManualTasks();

            nextSchedule = SchedulerController.GetNextSchedule();

            if (nextSchedule != null)
            {
                if (nextSchedule.ScheduleInfo.NextRun <= DateTime.Now)
                {
                    RunNextSchedule(null);
                }
            }
        }
Ejemplo n.º 11
0
        public static void ScheduleTasks()
        {
            RunManualTasks();

            nextSchedule = SchedulerController.GetNextSchedule();

            if (nextSchedule != null)
            {
                if (nextSchedule.ScheduleInfo.NextRun <= DateTime.Now)
                {
                    RunNextSchedule(null);
                }
            }
        }
Ejemplo n.º 12
0
        public static int StopSchedule(int scheduleId)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo);

            if (accountCheck < 0)
            {
                return(accountCheck);
            }

            SchedulerJob schedule = GetScheduleComplete(scheduleId);

            if (schedule == null)
            {
                return(0);
            }

            Scheduler.StopSchedule(schedule);

            return(0);
        }
Ejemplo n.º 13
0
        internal static SchedulerJob CreateCompleteScheduleFromDataSet(DataSet ds)
        {
            if (ds.Tables[0].Rows.Count == 0)
            {
                return(null);
            }

            SchedulerJob schedule = new SchedulerJob();

            // schedule info
            schedule.ScheduleInfo = ObjectUtils.FillObjectFromDataView <ScheduleInfo>(ds.Tables[0].DefaultView);

            // task info
            schedule.Task = ObjectUtils.FillObjectFromDataView <ScheduleTaskInfo>(ds.Tables[1].DefaultView);

            // parameters info
            List <ScheduleTaskParameterInfo> parameters = new List <ScheduleTaskParameterInfo>();

            ObjectUtils.FillCollectionFromDataView <ScheduleTaskParameterInfo>(
                parameters, ds.Tables[2].DefaultView);
            schedule.ScheduleInfo.Parameters = parameters.ToArray();

            return(schedule);
        }
Ejemplo n.º 14
0
        static void RunSchedule(SchedulerJob schedule, bool changeNextRun)
        {
            // update next run (if required)
            if (changeNextRun)
            {
                SchedulerController.CalculateNextStartTime(schedule.ScheduleInfo);
            }

            // disable run once task
            if (schedule.ScheduleInfo.ScheduleType == ScheduleType.OneTime)
                schedule.ScheduleInfo.Enabled = false;

            Dictionary<int, BackgroundTask> scheduledTasks = TaskManager.GetScheduledTasks();
            if (!scheduledTasks.ContainsKey(schedule.ScheduleInfo.ScheduleId))
                // this task should be run, so
                // update its last run
                schedule.ScheduleInfo.LastRun = DateTime.Now;

            // update schedule
            SchedulerController.UpdateSchedule(schedule.ScheduleInfo);

            // skip execution if the current task is still running
            if (scheduledTasks.ContainsKey(schedule.ScheduleInfo.ScheduleId))
                return;

            // run the schedule in the separate thread
            schedule.Run();
        }
Ejemplo n.º 15
0
        static void RunSchedule(SchedulerJob schedule, bool changeNextRun)
        {
            try
            {
                // update next run (if required)
                if (changeNextRun)
                {
                    SchedulerController.CalculateNextStartTime(schedule.ScheduleInfo);
                }

                // disable run once task
                if (schedule.ScheduleInfo.ScheduleType == ScheduleType.OneTime)
                    schedule.ScheduleInfo.Enabled = false;

                Dictionary<int, BackgroundTask> scheduledTasks = TaskManager.GetScheduledTasks();
                if (!scheduledTasks.ContainsKey(schedule.ScheduleInfo.ScheduleId))
                    // this task should be run, so
                    // update its last run
                    schedule.ScheduleInfo.LastRun = DateTime.Now;

                // update schedule
                int MAX_RETRY_COUNT = 10;
                int counter = 0;
                while (counter < MAX_RETRY_COUNT)
                {
                    try
                    {
                        SchedulerController.UpdateSchedule(schedule.ScheduleInfo);
                        break;
                    }
                    catch (SqlException)
                    {
                        System.Threading.Thread.Sleep(1000);
                    }

                    counter++;
                }
                if (counter == MAX_RETRY_COUNT)
                    return;

                // skip execution if the current task is still running
                scheduledTasks = TaskManager.GetScheduledTasks();
                if (!scheduledTasks.ContainsKey(schedule.ScheduleInfo.ScheduleId))
                {
                    // run the schedule in the separate thread
                    schedule.Run();
                }
            }
            catch (Exception Ex)
            {
                try
                {
                    TaskManager.WriteError(string.Format("RunSchedule Error : {0}", Ex.Message));
                }
                catch (Exception)
                {
                }
            }
        }
Ejemplo n.º 16
0
        internal static SchedulerJob CreateCompleteScheduleFromDataSet(DataSet ds)
        {
            if (ds.Tables[0].Rows.Count == 0)
                return null;

            SchedulerJob schedule = new SchedulerJob();

            // schedule info
            schedule.ScheduleInfo = ObjectUtils.FillObjectFromDataView<ScheduleInfo>(ds.Tables[0].DefaultView);

            // task info
            schedule.Task = ObjectUtils.FillObjectFromDataView<ScheduleTaskInfo>(ds.Tables[1].DefaultView);

            // parameters info
            List<ScheduleTaskParameterInfo> parameters = new List<ScheduleTaskParameterInfo>();
            ObjectUtils.FillCollectionFromDataView<ScheduleTaskParameterInfo>(
                parameters, ds.Tables[2].DefaultView);
            schedule.ScheduleInfo.Parameters = parameters.ToArray();

            return schedule;
        }
Ejemplo n.º 17
0
        static void RunSchedule(SchedulerJob schedule, bool changeNextRun)
        {
            try
            {
                // update next run (if required)
                if (changeNextRun)
                {
                    SchedulerController.CalculateNextStartTime(schedule.ScheduleInfo);
                }

                // disable run once task
                if (schedule.ScheduleInfo.ScheduleType == ScheduleType.OneTime)
                {
                    schedule.ScheduleInfo.Enabled = false;
                }

                Dictionary <int, BackgroundTask> scheduledTasks = TaskManager.GetScheduledTasks();
                if (!scheduledTasks.ContainsKey(schedule.ScheduleInfo.ScheduleId))
                {
                    // this task should be run, so
                    // update its last run
                    schedule.ScheduleInfo.LastRun = DateTime.Now;
                }

                // update schedule
                int MAX_RETRY_COUNT = 10;
                int counter         = 0;
                while (counter < MAX_RETRY_COUNT)
                {
                    try
                    {
                        SchedulerController.UpdateSchedule(schedule.ScheduleInfo);
                        break;
                    }
                    catch (SqlException)
                    {
                        System.Threading.Thread.Sleep(1000);
                    }

                    counter++;
                }
                if (counter == MAX_RETRY_COUNT)
                {
                    return;
                }

                // skip execution if the current task is still running
                scheduledTasks = TaskManager.GetScheduledTasks();
                if (!scheduledTasks.ContainsKey(schedule.ScheduleInfo.ScheduleId))
                {
                    // run the schedule in the separate thread
                    schedule.Run();
                }
            }
            catch (Exception Ex)
            {
                try
                {
                    TaskManager.WriteError(string.Format("RunSchedule Error : {0}", Ex.Message));
                }
                catch (Exception)
                {
                }
            }
        }